News:

Support the VirtueMart project and become a member

Main Menu

Price for parent product from child products

Started by jarlu, January 29, 2020, 09:49:11 AM

Previous topic - Next topic

jarlu

Hello,
I have parent products with child products. Parent products have only descriptions (without prices), but child products have own prices. Now is shown only button "Ask for price" in product detail page and price is shown only when customer select child product. But I need show the lowest price form child products in parent products or their prices range (price from - to). Is it possible? Thank you

PRO


On 1 of my sites I use this code to display a price range for parent.


function price_table($pid){
$productModel = VmModel::getModel('product');
$ids = $productModel->getProductChildIds($pid);
$prods = $productModel->getProducts($ids);
$customfieldsModel = VmModel::getModel ('Customfields');
$prods->customfields = $customfieldsModel->getCustomEmbeddedProductCustomFields ($product->allIds,0,-1, true);
$html='';
if (!empty($prods)){
$prices=Array();
foreach ($prods as $p){
$prices[]=$p->prices['salesPrice'];
}
if (count($prices>1)){
$html.='<p class="redtext bold">Price from $'.min($prices).' - $'.max($prices).'</p>';
$html.='<p class="greentext">Choose Options to See Price</p>';
}

}

echo $html;
}


echo price_table($this->product->virtuemart_product_id);
echo shopFunctionsF::renderVmSubLayout('prices',array('product'=>$this->product,'currency'=>$this->currency));

You can see the display here.
https://www.glowgizmo.com/bars/fb-size





jarlu


jarlu

#3
Hello,
I have issue with this code. when I copy this code to prices.php in my template, everything is OK in product. But in category view (there I have more then one product without price of parent products), scprit is stopped and page is not loaded correctly.
In attachments I send screens of correct view in product detail and problem in category view, where is more than one product where parent products don't have price. If is only one product without parent product price in category view, everithing is ok.
My code is:

function price_table($pid){
$productModel = VmModel::getModel('product');
$ids = $productModel->getProductChildIds($pid);
$prods = $productModel->getProducts($ids);
$customfieldsModel = VmModel::getModel ('Customfields');
$prods->customfields = $customfieldsModel->getCustomEmbeddedProductCustomFields ($product->allIds,0,-1, true);
$html='';
if (!empty($prods)){
$prices=Array();
foreach ($prods as $p){
$prices[]=$p->prices['salesPrice'];
}
if (count($prices>1)){

$mincena = min($prices);

$maxcena = max($prices);
$mincena = number_format($mincena, 2, ',', ' ');;
  $maxcena = number_format($maxcena, 2, ',', ' ');;
 
 
if (min($prices) == max($prices) || ($mincena == "0,00") ) {

  $html.='<div class="PricesalesPrice vm-display vm-price-value" data-mh="vm-product-price">
               <div class="product-price">
   <div class="vm-basic-prices clearfix"><div class="PricesalesPrice vm-display vm-price-value"><span class="vm-price-desc">Predajná cena</span><span class="PricesalesPrice">'.$maxcena.' €</span></div></div></div>        </div>';
}
 
else {
$html.='<div class="PricesalesPrice vm-display vm-price-value" data-mh="vm-product-price">
               <div class="product-price">
   <div class="vm-basic-prices clearfix"><div class="PricesalesPrice vm-display vm-price-value"><span class="vm-price-desc">Predajná cena</span><span class="PricesalesPrice">od '.$mincena.' €</br> do '.$maxcena.' €</span></div></div></div>        </div>';

}
}

}

echo $html;
}


echo price_table($product->virtuemart_product_id);

Studio 42

Certainly, it's because you redeclare function price_tableThis is not possible in PHP.
Enable PHP debug so you see why you have an error

jarlu

Thank you, I copied function() to router.php and now, it is OK.

pinochico

Why do you think, then file router.php is the right place?
www.minijoomla.org  - new portal for Joomla!, Virtuemart and other extensions
XML Easy Feeder - feeds for FB, GMC,.. from products, categories, orders, users, articles, acymailing subscribers and database table
Virtuemart Email Manager - customs email templates
Import products for Virtuemart - from CSV and XML
Rich Snippets - Google Structured Data
VirtueMart Products Extended - Slider with products, show Others bought, Products by CF ID and others filtering products

jarlu

Where is right place for declarate function()?

PRO


For product details do it at the top of


default.php
or make a functions.php file in the same folder, and do
include("functions.php");



Then, you will also need to add the function for category view
top of products.php


FOR category view.
You need to add it to top of products.php


ofcourse if you setup your own library, you could use the function anywhere throughout Joomla

jjk

There is also a possibility to do that without code changes. Settings for the parent product would be:
1. Delete the Base price
2. In the Custom Fields tab uncheck 'Display parent as option' and 'Parent orderable'.
3. In the VM Configuration > Pricing tab > Price Configuration uncheck 'Show call for price, when the price is empty'.
4. Save the product settings.
Non-English Shops: Are your language files up to date?
http://virtuemart.net/community/translations

jarlu

Quote from: PRO on February 03, 2020, 15:52:41 PM

For product details do it at the top of


default.php
or make a functions.php file in the same folder, and do
include("functions.php");



Then, you will also need to add the function for category view
top of products.php


FOR category view.
You need to add it to top of products.php


ofcourse if you setup your own library, you could use the function anywhere throughout Joomla
Hello, but if I want use include(), I need copy this function to lot of files (everywhere where is price of product displayed). Is it possible paste include() only in one php file for use at all VM web? Thank you

PRO

Quote from: jarlu on February 04, 2020, 19:29:13 PM
Hello, but if I want use include(), I need copy this function to lot of files (everywhere where is price of product displayed). Is it possible paste include() only in one php file for use at all VM web? Thank you

No,
thats why I use a library.


Studio 42

#12
 ----

Studio 42

The best way is to use a system plugin.
Then you add all function needed in the class
Eg

static class plgsystemcustom {
static function price_table($pid){
  //mycode
}
static function otherFc(){
  //mycode
}
}


Then you use plgsystemcustom::price_table(); or plgsystemcustom::otherFc();

jarlu

Do you have some manual for making very simple system plugin? Because I don't' have any experience.