开发者

Magento ->getSku() or getData(’sku’) returns empty string

I have Magento 1.3.2 and I have weird issue:

When I am in list.phtml开发者_如何学Python, and I try to fetch the SKU by using getSku() or getData('sku') I get empty string. getName() does work. However, when I do that from other pages, it works well.

I var_dump-ed it and no SKU is shown.

What can cause this?


I'm surprised nobody has given you the easiest and most proper answer yet:

Go to your admin, Catalog >> Attributes >> Manage Attributes. Then edit the 'sku' attribute. Change the "Used in Product Listing" from 'No' to 'Yes'. You will then have access to it from the product object in list.phtml with ->getSku()


The other option is to re-load the product object in the list.phtml using the ID of the product you already have. The code reads something a little like:

$sku = Mage::getModel('catalog/product')->load($_product->getId())->getSku();

Note that $_product is what you are getting in your collection already, and note that getSku is case sensitive (as are all Magento attributes getter/setters).

@Prattski's solution is preferable as you don't really want to be messing with loading/manipulating the objects, but it sounds as though your collection is a little messed up. SKU is one of the core fields that exists in the base catalog_product_entity table, so would be unusual not to be loaded.


Probably sku is not added to the list of attributes when a collection is retrieved. I assume you are talking a bout the file /template/catalog/product/list.phtml. If so, then you need to extend the corresponding code file (/app/code/core/Mage/Catalog/Block/Product/List.php).

I think your best bet is to overload the getLoadedProductCollection() method to:

public function getLoadedProductCollection()
    {
        return $this->_getProductCollection()->addAttributeToSelect('sku');
    }

This might not work, I have not been able to test it, as in my store the sku and all other attributes are accessible in the list.phtml template file.


Try this:

<?php 
    $current_product = Mage::registry('current_product');
    if($current_product) {
        $sku = $current_product->getSku();
        // output sku
        echo $sku;
    }
?>


I had same problem too but tried $_product['sku'] it works for me


$_product["sku"]; enough to get the product sku.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜