Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get simple products belonging to configurable product AFTER configurable product save

Tags:

magento

In magento, it is possible to get the simple products associated to a configurable product by using the following call:

$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);

I am trying to call this function after the configurable product is saved, so that I can get the new list of simple products it uses. So I am making the above call from a method that is triggered by the catalog_product_save_after event. However, after the call $childProducts stores the simple products that were associated to $product BEFORE the save operation, not AFTER it.

How could I get the simple products associated to $product after the save operation?

Thanks in advance, any suggestion is appreciated.

like image 918
Epicurus Avatar asked Jul 05 '11 12:07

Epicurus


1 Answers

Magento's OOP system is very good, and this goodness sometimes creates problems for those who haven't yet gone deep into its structure.

If you closely follow the method "getUsedProducts()" in the class "Mage_Catalog_Model_Product_Type_Configurable", you will see that there are some "if" logics provided, along with the usage of its properties (like "_usedProducts", "_configurableAttributes"). These obstruct you from getting the actual result, but the fault is not of Magento, instead the fault is because of the lack of Magento documentation.

Let me clear you about the first few lines of this method:-

Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
if (!$this->getProduct($product)->hasData($this->_usedProducts)) {
    if (is_null($requiredAttributeIds) and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
        // If used products load before attributes, we will load attributes.
        $this->getConfigurableAttributes($product);
        // After attributes loading products loaded too.
        Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
        return $this->getProduct($product)->getData($this->_usedProducts);
    }
    ....

This method has 2 arguments - "$requiredAttributeIds" (Configurable Attribute IDs) & "$product" (configurable product object).

When calling this method, you are passing "null" for the parameter "$requiredAttributeIds", but you are providing the correct Configurable Product object "$product".

This class has a property "_usedProducts" (for maintaining the data of child simple products), which is set for every Configurable Product object. If this value has earlier been set, then Magento will return you the already available values to you. This is the main reason why you are getting the child products before the configurable product was updated.

So, what you can do is you can clear the full Cache Storage, along with refreshing all the Cache processes. May be then your result will work, because internally Magento stores all these used products data in cache.

Hope it helps.

like image 78
Knowledge Craving Avatar answered Oct 22 '22 03:10

Knowledge Craving