News:

Support the VirtueMart project and become a member

Main Menu

The lowest price of the child product

Started by andrejk32, April 09, 2024, 07:18:04 AM

Previous topic - Next topic

andrejk32

Hello,

VM 4.2.4, Joomla 4.4.3, PHP 8.0

I need your help. How do I select the lowest price of a child in the product template?
Something like $this->product->getChildren($this->product->virtuemart_product_id)->price;
$min = min(.....);



Best regards
Andrej

Milbo

Do you mean the lowest price of a product? or do you mean of all variants organised as children the lowest price first?
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Dud3

Something likes this...

    $product = $this->product;

    $productModel = VmModel::getModel('product');
    $calculator = clone calculationHelper::getInstance();
   
    $children = $productModel->getProductChildIds($product->virtuemart_product_id);

    $cheapestPrice = $product->prices;

    foreach ($children as $child)
    {
        $child = $productModel->getProduct($child);
       
        if (!$child->published)
        {
            continue;
        }
       
        $childPrices = $calculator->getProductPrices($child, true, 1);
       
        if (!empty($childPrices['product_price']) && (is_null($product->prices['product_price']) || $product->prices['product_price'] > $childPrices['product_price']))
        {
            $cheapestPrice = $childPrices;
        }
    }

    echo $cheapestPrice['product_price'];

Milbo

We have only an hidden config, to determine if the product should choose the highest entered price, or lowest entered price. This handles just the case, if your product has more than one price.

But your function looks quite correct, if you want the lowest price of all variants organised as children.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

andrejk32

Hello,

thank you all for your reply.
Quote from: Dud3 on May 07, 2024, 16:34:35 PMI've slightly added your code and it works ;-)

Best regards
Andrej