2

I want to display the minimal tier price on my product page and wherever I load my product model. I am using the basic function to get price data:

$this->getPriceHtml($product, true);

When this is called on the product collection (such as a category page) it works fine. When I call getPriceHtml on a product model instance (loaded with Mage::getModel('catalog/product')->load($id), if I dump the product data then getData('minimal_price') does not exist.

I'm thinking that when loading the collection,

 ->addMinimalPrice()

is added to the collection, but this is missing from the single entity load. Therefore my question is, do I need to include some other method/helper/model when loading product in order for the minimal price to also be loaded?

5

I didn't find a simple method to load the index for a single product, the safest bet should be using a collection.

This helper method worked for me:

/**
 * Loads minimal price from index in single product entity
 *
 * @param Mage_Catalog_Model_Product $product
 * @return $this
 */
public function loadPriceIndex(Mage_Catalog_Model_Product $product)
{
    $productCollection = $product->getCollection();
    $productCollection->addAttributeToSelect([])
        ->addAttributeToFilter('entity_id', $product->getId())
        ->addPriceData();
    $product->addData($productCollection->getFirstItem()->getData());
    return $this;
}

Don't worry about database load, this results in a single query with only one join on the price index table like this:

SELECT `e`.*, price_index.price AS `indexed_price`, `price_index`.`price`, `price_index`.`final_price`,
    IF(`price_index`.`tier_price`,
        LEAST(`price_index`.`min_price`, `price_index`.`tier_price`), 
        `price_index`.`min_price`
    ) AS `minimal_price`,
    `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_index_price` AS `price_index`
ON price_index.entity_id = e.entity_id
    AND price_index.website_id = '5'
    AND price_index.customer_group_id = 0
WHERE (`e`.`entity_id` = '2026') 
| improve this answer | |
2

It seems that the getMinimalPrice() method only exists to call on the indexed value. The method belongs to this class:

Mage_CatalogIndex_Model_Resource_Data_Grouped

Therefore it's not possible to get this for a single product instance as the index table isn't used. However it is possible to do the work by getting out the tier prices (as an array) and returning the last tier price, which should be your cheapest. Like this:

$tier_price = end($_product->getTierPrice());
echo $tier_price['price'];
| improve this answer | |
  • This only works partially. Tier prices are not the only thing that has effect on the minimal price. Customer group prices should be taken into account as well and for configurable and bundle products you need to look for the cheapest selection. – Fabian Schmengler Feb 5 '15 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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