VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: modernmagic on August 22, 2025, 01:07:20 AM

Title: Display lowest price based on product pricing (quantity range)
Post by: modernmagic on August 22, 2025, 01:07:20 AM
I want to display "As low as $184".  The 184 is the price for the highest quantity range. 

Something like this mockup:
Screenshot 2025-08-21 at 4.09.07 PM.jpg
Screenshot 2025-08-21 at 3.57.37 PM.jpgScreenshot 2025-08-21 at 4.00.15 PM.jpg
Is there a setting, plugin, or custom solution?
Title: Re: Display lowest price based on product pricing (quantity range)
Post by: vmfyelloq19 on August 22, 2025, 12:01:35 PM
/templates/(your-template)/html/com_virtuemart/productdetails/default.php

$allPrices_key_found = array_reduce(
    array_keys($this->product->allPrices), // iterate over allPrices' keys
    function ($key_best_so_far, $key_current) {
        if($this->product->allPrices[$key_current]['product_price'] < $key_current[$key_best_so_far]['product_price']) {
            return $key_current;
        } else {
            return $key_best_so_far;
        }
    },
    array_key_first($this->product->allPrices) // start with first allPrices' keys
);
echo "Lowest price:<br>";
print_r($this->product->allPrices[$allPrices_key_found]);

Change the if-statement operator from < to > to get most expensive price's item.
Title: Re: Display lowest price based on product pricing (quantity range)
Post by: modernmagic on August 23, 2025, 21:52:50 PM
Thank you so much for the code!

Unfortunately, this is the result:
Screenshot 2025-08-23 at 3.30.27 PM.jpg

This is the code:
Screenshot 2025-08-23 at 12.46.36 PM.jpg

I'm hoping to display:  Lowest price: $184 
Title: Re: Display lowest price based on product pricing (quantity range)
Post by: modernmagic on August 24, 2025, 00:39:42 AM
I ran your code through Ai and got it to work:

// Make sure we have an array to work with.
$allPrices = (isset($this->product->allPrices) && is_array($this->product->allPrices))
    ? $this->product->allPrices
    : [];
// Polyfill for array_key_first for older PHP (<7.3).
if (!function_exists('array_key_first')) {
    function array_key_first(array $arr) {
        foreach ($arr as $k => $_) { return $k; }
        return null;
    }
}
echo "Lowest price:<br>";
if (!empty($allPrices)) {
    // Initialize with the first key.
    $lowestKey = array_key_first($allPrices);
    // Walk the array to find the lowest product_price.
    foreach ($allPrices as $k => $row) {
        if (!isset($row['product_price'])) {
            continue;
        }
        if (
            !isset($allPrices[$lowestKey]['product_price']) ||
            (float)$row['product_price'] < (float)$allPrices[$lowestKey]['product_price']
        ) {
            $lowestKey = $k;
        }
    }
    $lowestPrice = isset($allPrices[$lowestKey]['product_price'])
        ? (float)$allPrices[$lowestKey]['product_price']
        : null;
    echo $lowestPrice !== null ? number_format($lowestPrice, 2) : 'N/A';
} else {
    echo 'N/A';
}

THANK YOU for your help!