VirtueMart Forum

VirtueMart 2 + 3 => 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
Code: [Select]
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

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
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
Code: [Select]
static class plgsystemcustom {
 static function price_table($pid){
  //mycode
 }
 static function otherFc(){
  //mycode
 }
}

Then you use
Code: [Select]
plgsystemcustom::price_table(); or
Code: [Select]
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).