VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product pricing => Topic started by: jarlu on January 29, 2020, 09:49:11 AM

Title: Price for parent product from child products
Post by: jarlu on January 29, 2020, 09:49:11 AM
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
Title: Re: Price for parent product from child products
Post by: PRO on January 29, 2020, 14:09:31 PM

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




Title: Re: Price for parent product from child products
Post by: jarlu on January 29, 2020, 14:39:25 PM
Thank you very much :)
Title: Re: Price for parent product from child products
Post by: jarlu on January 31, 2020, 09:50:14 AM
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);
Title: Re: Price for parent product from child products
Post by: Studio 42 on January 31, 2020, 23:52:29 PM
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
Title: Re: Price for parent product from child products
Post by: jarlu on February 01, 2020, 08:44:28 AM
Thank you, I copied function() to router.php and now, it is OK.
Title: Re: Price for parent product from child products
Post by: pinochico on February 02, 2020, 17:09:06 PM
Why do you think, then file router.php is the right place?
Title: Re: Price for parent product from child products
Post by: jarlu on February 03, 2020, 11:30:22 AM
Where is right place for declarate function()?
Title: Re: Price for parent product from child products
Post by: 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
Title: Re: Price for parent product from child products
Post by: jjk on February 03, 2020, 15:58:14 PM
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.
Title: Re: Price for parent product from child products
Post by: jarlu on February 04, 2020, 19:29:13 PM
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
Title: Re: Price for parent product from child products
Post by: PRO on February 04, 2020, 21:25:55 PM
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.

Title: Re: Price for parent product from child products
Post by: Studio 42 on February 05, 2020, 18:34:08 PM
 ----
Title: Re: Price for parent product from child products
Post by: Studio 42 on February 05, 2020, 18:36:27 PM
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();
Title: Re: Price for parent product from child products
Post by: jarlu on February 05, 2020, 21:14:39 PM
Do you have some manual for making very simple system plugin? Because I don't' have any experience.
Title: Re: Price for parent product from child products
Post by: PRO on February 05, 2020, 22:10:54 PM
some more information here


http://forum.virtuemart.net/index.php?topic=141917.0

https://docs.joomla.org/Using_own_library_in_your_extensions


Title: Re: Price for parent product from child products
Post by: Studio 42 on February 05, 2020, 23:05:51 PM
See for eg JOOMLAROOT/plugins/system/log/
the xml is the declaration of the plugin
you have a naming convention for this exemple class PlgSystemLog because it's a "system" plugin in folder "log"
Title: Re: Price for parent product from child products
Post by: pinochico on February 07, 2020, 18:05:11 PM
You don't need system plugin, it is only one of possibility, only one way, which uses different programmers.

You need to put the custom function on right place, that is the right way.

(A lot of system plugins only slow down the website).