1

I'm looking to have a pricing table (on the frontpage) for one specific product, but 3 different (predefined) quantities.

Showing the product and having the correct add to cart links on the page was relatively easy:

$_productId = '498';
$_product   = Mage::getModel('catalog/product')->load($_productId);
$_url       = Mage::helper('checkout/cart')->getAddUrl($_product);

Getting the Qty 1 price including tax, currency and some formatting was easy as well:

$_price = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice()));

This results in <span class="price">€15,00</span>

However, my goal is to have a pricing table like:

┌───────────┬───────────┬───────────┐
│  Single   │ ValuePack │ FamilyPack│
├───────────┼───────────┼───────────┤
│   €15     │    €10    │    €7.50  │
├───────────┼───────────┼───────────┤
│  Qty 1    │   Qty 5   │   Qty 10  │ 
└───────────┴───────────┴───────────┘ 

Getting the Qty * X price wasn't hard either:

$_qty = 5; 
$_price = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice($_qty)));

As expected, this results in <span class="price">€10,00</span>

However, even though I change $_qty, the output of the other 2 prices is always the same as the first.

Illustration:

$_price_1 = Mage::helper('core .... $_product->getFinalPrice(1))); results in €15
$_price_2 = Mage::helper('core .... $_product->getFinalPrice(5))); results in €15 as well
$_price_3 = Mage::helper('core .... $_product->getFinalPrice(10)); results in €15 as well

I'm assuming this has something to do with Magento caching the product price after the first lookup.

Anybody got a clue on how to create this table the correct way?

2
  • Maybe this is because you set $_qty = 5, but you use $_Qty (or is this a typo) Commented Jun 12, 2015 at 14:31
  • @VladimirKerkhoff - just a (now corrected typo.
    – Ottonet
    Commented Jun 12, 2015 at 14:33

1 Answer 1

1

Turns out getting the tier-pricing was not that hard either as this is available in an Array $_product->getFormatedTierPrice() (note the incorrect spelling!).

This array looks like this:

Array
(
    [32000-2] => Array
        (
            [price_id] => 50
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 21.95
            [price_qty] => 2.0000
            [website_price] => 24.95
            [formated_price] => € 18,95
        )

    [32000-4] => Array
        (
            [price_id] => 52
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 23.95
            [price_qty] => 4.0000
            [website_price] => 23.95
            [formated_price] => € 17,95
        )

    [32000-10] => Array
        (
            [price_id] => 56
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 3200024.95
            [price] => 22.95
            [price_qty] => 10.0000
            [website_price] => 22.95
            [formated_price] => € 16,95
        )

    [32000-25] => Array
        (
            [price_id] => 57
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 19.95
            [price_qty] => 25.0000
            [website_price] => 21.95
            [formated_price] => € 15,95
        )

    [32000-50] => Array
        (
            [price_id] => 58
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 19.95
            [price_qty] => 50.0000
            [website_price] => 19.95
            [formated_price] => € 14,95
        )

)

I only require 2 items from this multidimensional array: [price_qty] and [formated_price].

So I ended up with the following code:

$_productId = '498';
$_product   = Mage::getModel('catalog/product')->load($_productId);
$_single = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice())); // Just the single product price
$_prices = array_column($_product->getFormatedTierPrice(), 'formated_price', 'price_qty'); // Extract price_qty and formated_price from Array of formatted tier prices

And in my pricing table I now have:

<li class="price">
  <?php 
    $_qty = '10.0000'; 
    if (array_key_exists($_qty, $_prices)) { 
      echo $_prices[$_qty]; 
    } else { 
    echo $_single; 
    } 
  ?>
</li>

With else { echo $_single;} I ensure that if someone changed tier qty in the backend (tier qty's are currently hardcoded in my phtml) I at least output the single product price instead of no price at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.