Magento ->getSku() 或 getData(’sku’) 返回空字符串
我有 Magento 1.3.2,但有一个奇怪的问题:
当我在 list.phtml
中时,我尝试使用 getSku()
或 获取 SKU getData('sku')
我得到空字符串。 getName()
确实有效。但是,当我从其他页面执行此操作时,效果很好。
我对其进行了 var_dump
编辑,但未显示任何 SKU。
什么会导致这种情况?
I have Magento 1.3.2 and I have weird issue:
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.
I var_dump
-ed it and no SKU is shown.
What can cause this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我很惊讶没有人给你最简单、最正确的答案:
转到你的管理员,目录>>>属性>>管理属性。然后编辑'sku'属性。将“用于产品列表”从“否”更改为“是”。然后,您可以使用
->getSku()
从list.phtml
中的产品对象访问它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()
另一个选项是使用您已有的产品 ID 重新加载 list.phtml 中的产品对象。代码读起来有点像:
请注意,$_product 是您已经在集合中获取的内容,并注意 getSku 区分大小写(所有 Magento 属性 getter/setter 也是如此)。
@Prattski 的解决方案更可取,因为您真的不想弄乱加载/操作对象,但听起来您的集合有点混乱。 SKU 是
catalog_product_entity
基表中存在的核心字段之一,因此不加载是不正常的。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:
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.检索集合时,sku 可能不会添加到属性列表中。我假设您正在谈论文件 /template/catalog/product/list.phtml。如果是这样,那么您需要扩展相应的代码文件(/app/code/core/Mage/Catalog/Block/Product/List.php)。
我认为最好的选择是重载 getLoadedProductCollection() 方法:
这可能不起作用,我无法测试它,因为在我的商店中,sku 和所有其他属性都可以在 list.phtml 模板文件中访问。
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:
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:
我也遇到了同样的问题,但尝试了 $_product['sku']
这对我有用
I had same problem too but tried $_product['sku']
it works for me
$_product["sku"];足以获得产品 sku。
$_product["sku"]; enough to get the product sku.