1

We have an observer that modifies final price, that's triggered on the event catalog_product_get_final_price. Problem is, when you retrieve products via collection with addAttributeToSelect("*"), they come back with a final_price already set, and the product getFinalPrice function merely returns what's in the database instead of recalculating (and running the observers for) the final price.

Mage_Catalog_Model_Product

public function getFinalPrice($qty=null)
{
    // *** Collection items already have a 'final_price' from flat tables
    //     but it's wrong because observers haven't run yet ***
    $price = $this->_getData('final_price');
    if ($price !== null) {
        return $price;
    }
    return $this->getPriceModel()->getFinalPrice($qty, $this);
}

I've also noticed that all of our custom attributes are also not being interpreted (we save things as JSON but decode into objects them on product load before using them).

This behavior doesn't happen when you load the product directly -- all the observers and models are interpreted when the product is loaded.

Is there a way to ensure the data we're retrieving from a collection matches in format and value the data we would retrieve if we load the product directly?

I know I can just load the product individually to trigger all of the events that go along with it, but in the case of 3rd party software, that might not always be the case and the final price is wrong.

We're on version 1.7 if it helps.

1 Answer 1

1

The results you get from a collection load and an entity load are not the same and they should not be the same.
Each collection load has its own events that you can use to replicate what you need that already happens on the load call.
There is no need to get the same results if you don't need the same behavior.
Getting the same results will get you into performance issues.
load is a very expensive process, resource wise and trying to get the same values for a collection for sure it will not scale. The more elements you have in the collection, the longer it will take to get every bit of data you need.
In conclusion, use events like catalog_product_collection_load_before and catalog_product_collection_load_after (for product collections), catalog_category_collection_load_before|after for category collections.
And so on. Look in the collection you need for a proper event to use.

2
  • I understand the reasoning, but the issue is that calling getFinalPrice on an item retrieved via collection gives you the wrong final price -- that's a big problem. If the final price saved in the table was the correct final price, none of this would be an issue. But we modify the final price for a reason, and we need that to be reflected in every single call to getFinalPrice. Commented Aug 26, 2015 at 19:51
  • try calling ->addMinimalPrice()->addFinalPrice() on your collection before loading it and also observe the events I just mentioned and in these events add your modification to the final price. you can create a separate method in an observer and call that method for each of the events you need.
    – Marius
    Commented Aug 26, 2015 at 20:00

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.