Magento - Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray

gpcola picture gpcola · Jul 18, 2011 · Viewed 9.2k times · Source

I'm getting the following error when I hit a configurable product using the saveRow method from /app/code/local/Mage/Catalog/Model/Convert/Adapter/Productimport.php:

[05-Jul-2011 18:12:32] PHP Fatal error:  Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray() in /home/gp/public_html/app/code/local/Mage/Catalog/Model/Convert/Adapter/Productimport.php on line 107

My problem is a little different to others I've come across online perhaps in that I'm calling saveRow() from my own script that is building and maintaining a list of products, downloaded from my supplier by xml feed, in a temporary database before then using magento to add or update them in my site's catalog.

require_once($_SERVER['DOCUMENT_ROOT']."/app/code/local/Mage/Catalog/Model/Convert/Adapter/Productimport.php");
$MageProducts = new Mage_Catalog_Model_Convert_Adapter_Productimport();
...
...
foreach($products as $product) {
    $result = $MageProducts->saveRow($product);
}

The first time I hit a configurable product I get this error but if I immediately hit refresh the script runs right past that product and all the way through to the end, passing many simple/configurable product sets on its way, without failing.

Line 107 of ProductImport.php is this line

$cspa  = $product->getTypeInstance()->getConfigurableAttributesAsArray($product);

For some reason the $product->getTypeInstance is returning Mage_Catalog_Model_Product_Type_Simple but only the first time in a session?!

If I add print_r($product->getTypeInstance()) just before that line I get the following for a configurable product

Mage_Catalog_Model_Product_Type_Simple Object ( [_product:protected] => Mage_Catalog_Model_Product Object ( [_cacheTag:protected] => catalog_product [_eventPrefix:protected] => catalog_product [_eventObject:protected] => product [_canAffectOptions:protected] => [_typeInstance:protected] => Mage_Catalog_Model_Product_Type_Simple Object *RECURSION* [_typeInstanceSingleton:protected] => Mage_Catalog_Model_Product_Type_Configurable Object ( [_usedProductAttributeIds:protected] => _cache_instance_used_product_attribute_ids....

Which is clearly wrong...

I want to be able to use my script through cron, but this error is stopping me from doing that so I desperately need some help to fix - can anyone offer some advice?

Answer

Manik Thakur picture Manik Thakur · Feb 10, 2015

Before this:

$product->getTypeInstance()->getConfigurableAttributesAsArray($product);

Check $product->isConfigurable();. If it is, only run:

$product->getTypeInstance()->getConfigurableAttributesAsArray($product);