php Magento ->getSku() 或 getData('sku') 返回空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3541273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento ->getSku() or getData(’sku’) returns empty string
提问by HimberHyman
I have Magento 1.3.2 and I have weird issue:
我有 Magento 1.3.2,我有奇怪的问题:
When I am in list.phtml
, 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.
当我在 中时list.phtml
,我尝试通过使用来获取 SKU,getSku()
否则getData('sku')
我会得到空字符串。getName()
确实有效。但是,当我从其他页面执行此操作时,效果很好。
I var_dump
-ed it and no SKU is shown.
我编辑了var_dump
它,但没有显示 SKU。
What can cause this?
什么会导致这种情况?
回答by Prattski
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()
转到您的管理员,目录 >> 属性 >> 管理属性。然后编辑“sku”属性。将“用于产品列表”从“否”更改为“是”。然后,您将拥有从产品对象访问它list.phtml
与->getSku()
回答by Jonathan Day
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:
另一种选择是使用您已有的产品 ID 在 list.phtml 中重新加载产品对象。代码读起来有点像:
$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).
请注意, $_product 是您已经在集合中获得的内容,并注意 getSku 区分大小写(所有 Magento 属性 getter/setter 也是如此)。
@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.
@Prattski 的解决方案是可取的,因为您真的不想弄乱加载/操作对象,但听起来好像您的收藏有点混乱。SKU 是基catalog_product_entity
表中存在的核心字段之一,因此不加载是不寻常的。
回答by silvo
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).
检索集合时,可能未将 sku 添加到属性列表中。我假设您正在谈论文件 /template/catalog/product/list.phtml。如果是,那么就需要扩展相应的代码文件(/app/code/core/Mage/Catalog/Block/Product/List.php)。
I think your best bet is to overload the getLoadedProductCollection() method to:
我认为最好的办法是重载 getLoadedProductCollection() 方法:
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.
这可能不起作用,我无法对其进行测试,因为在我的商店中,可以在 list.phtml 模板文件中访问 sku 和所有其他属性。
回答by silvo
Try this:
尝试这个:
<?php
$current_product = Mage::registry('current_product');
if($current_product) {
$sku = $current_product->getSku();
// output sku
echo $sku;
}
?>
回答by Raza
I had same problem too but tried $_product['sku'] it works for me
我也有同样的问题,但试过 $_product['sku'] 对我有用
回答by Kapil Kumar
$_product["sku"]; enough to get the product sku.
$_product["sku"]; 足以获得产品sku。