VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: andrejk32 on April 09, 2024, 07:18:04 AM

Title: The lowest price of the child product
Post by: andrejk32 on April 09, 2024, 07:18:04 AM
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
Title: Re: The lowest price of the child product
Post by: Milbo on April 18, 2024, 10:51:58 AM
Do you mean the lowest price of a product? or do you mean of all variants organised as children the lowest price first?
Title: Re: The lowest price of the child product
Post by: Dud3 on May 07, 2024, 16:34:35 PM
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'];
Title: Re: The lowest price of the child product
Post by: Milbo on May 12, 2024, 20:40:25 PM
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.
Title: Re: The lowest price of the child product
Post by: andrejk32 on May 13, 2024, 11:24:15 AM
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