News:

Looking for documentation? Take a look on our wiki

Main Menu

Display lowest price based on product pricing (quantity range)

Started by modernmagic, August 22, 2025, 01:07:20 AM

Previous topic - Next topic

modernmagic

I want to display "As low as $184".  The 184 is the price for the highest quantity range. 

Something like this mockup:
You cannot view this attachment.
You cannot view this attachment.You cannot view this attachment.
Is there a setting, plugin, or custom solution?

vmfyelloq19

/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.

modernmagic

Thank you so much for the code!

Unfortunately, this is the result:
You cannot view this attachment.

This is the code:
You cannot view this attachment.

I'm hoping to display:  Lowest price: $184 

modernmagic

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!

Milbo

Maybe I misunderstand the problem, but there is a simple setting for it. In the vmconfig tab prices, there is "Order of multiple prices". So you can decide if the product should show mainly the lowest, or highest price. No resort code in the template needed.

But in anyway thank you for the idea vmfyelloq19. And great that you managed it modernmagic.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/