Search This Blog

Jun 25, 2012

Magento: Fatal Error: getSku() on Configurable Products

This is relatively small issue but it will definitely annoy your customers. This issue is related to customer’s account when your customer added an items in their cart and drop the session without finishing the transaction. When they drop, those items in the cart are saved on the database and available to view in their corresponding customer’s account account. Information like skus, titles and quantities are some of the information that have been saved. The issue comes when you edit those products that your customers added in your cart. Example, if you edit the sku's or type of those products, what will happen to those item on their cart? The answer is it will cause a fatal error and you will not be able to view their accounts. Why? It is because those products on their cart are not affected when you do the changes in your catalog. With that said, Magento can't get the exact products when your customers resume their session.

Fix:


To solve the issue, please follow the following steps:
1. Copy the configurable.php located in /app/code/core/Mage/Catalog/Model/Product/Type/ directory.

2. Make a copy of the director in your local folder. So, it should be /app/code/local/Mage/Catalog/Model/Product/Type/


3. Paste the configurable.php that you copied on the core files and open it.
4. Go somewhere in line 766 until 777 and look for the getSku method.

  public function getSku($product = null)
    {
        $sku = $this->getProduct($product)->getData('sku');
        if ($simpleOption = $this->getProduct($product)->getCustomOption('simple_product')) {
            $simple_sku = $simpleOption->getProduct($product)->getSku();
            $sku = parent::getOptionSku($product, $simple_sku);
        } else {
            $sku = parent::getSku($product);
        }

        return $sku;
    }


5. And then override it with this code below.

public function getSku($product = null)
    {
     $sku = $this->getProduct($product)->getData('sku');
     if ($simpleOption = $this->getProduct($product)->getCustomOption('simple_product')) {
      $simpleSkuProduct = $simpleOption->getProduct($product);
      if(!is_object($simpleSkuProduct)) {
       return parent::getSku($product);
      }
      $simple_sku = $simpleOption->getProduct($product)->getSku();
      $sku = parent::getOptionSku($product, $simple_sku);
     }else{
      $sku = parent::getSku($product);
     }
    
     return $sku;
    }

6. Save it, clear your cache and try to access the customer’s account that having an error. It should display the account information and not the fatal error.

0 comments: