Author Topic: Price for parent product from child products  (Read 4409 times)

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Price for parent product from child products
« 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

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10404
  • VirtueMart Version: 3+
Re: Price for parent product from child products
« Reply #1 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




J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #2 on: January 29, 2020, 14:39:25 pm »
Thank you very much :)

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #3 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);

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4330
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Price for parent product from child products
« Reply #4 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

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #5 on: February 01, 2020, 08:44:28 am »
Thank you, I copied function() to router.php and now, it is OK.

pinochico

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 384
    • MiniJoomla
  • VirtueMart Version: 3
Re: Price for parent product from child products
« Reply #6 on: February 02, 2020, 17:09:06 pm »
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 from products, orders and database table
Virtuemart Email Manager - customs email templates

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #7 on: February 03, 2020, 11:30:22 am »
Where is right place for declarate function()?

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10404
  • VirtueMart Version: 3+
Re: Price for parent product from child products
« Reply #8 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
J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

jjk

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 3648
  • using Matomo instead of Google Analytics
Re: Price for parent product from child products
« Reply #9 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.
Non-English Shops: Are your language files up to date?
http://virtuemart.net/community/translations

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #10 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

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10404
  • VirtueMart Version: 3+
Re: Price for parent product from child products
« Reply #11 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.

J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4330
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Price for parent product from child products
« Reply #12 on: February 05, 2020, 18:34:08 pm »
 ----

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4330
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Price for parent product from child products
« Reply #13 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();

jarlu

  • Beginner
  • *
  • Posts: 30
  • A beginner
Re: Price for parent product from child products
« Reply #14 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.