Magento:属性始终在目录视图中返回默认值,在产品视图中工作正常
我为产品创建了一个新的是/否属性。我已经扩展了产品模型来执行一些自定义逻辑,并且自定义函数在任何地方都可以工作。
当我最初尝试获取自定义属性值时,遇到了一些问题。 Magento 没有为我加载它,因此调用 $product->getMyAttributeName() 没有执行任何操作。在产品视图中,我让它与这个附加功能一起工作:
public function getAttrVal($attr_name)
{
return $this->getResource()->getAttribute($attr_name)->getFrontend()->getValue($this);
}
所以在前端查看产品时效果很好。如果设置了,它将获取分配的值,如果没有设置,它将获取默认值。
当我查看任何类别(该类别中所有产品的网格)时,都会执行相同的代码。但我的 getAttrVal() 函数始终返回默认值,即使我已为我的产品显式设置此值。
我一生都无法弄清楚为什么该属性在“产品”视图中正确加载,但“类别”视图始终获取默认值,尽管运行相同的代码。有什么想法吗?
I've created a new Yes/No attribute for products. I've extended the Product model to do some custom logic and the custom functions are working everywhere.
When I initially tried getting the custom attribute value, I ran into some issue. Magento wasn't loading it for me, so calls to $product->getMyAttributeName() did nothing. In the product views, I got it working with this additional function:
public function getAttrVal($attr_name)
{
return $this->getResource()->getAttribute($attr_name)->getFrontend()->getValue($this);
}
So that worked great when viewing a product on the frontend. It would fetch the assigned value if set, or the default if not.
When I view any Category (grid of all products in that category), the same exact code is being executed. But my getAttrVal() function always returns the default value, even if I've explicitly set this value for my product.
I can't, for the life of me, figure out why the attribute loads correctly in the Product view but the Category view always grabs the default value, despite running the same exact code. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Magento 将 EAV 模型用于其可扩展目录属性,因此并非每个属性都可以转移到每个页面上。尝试摆弄属性上的“显示在目录中”和其他相关变量,它可能会解决您的问题。如果没有,请说出来,我们可以尝试其他方法。
希望有帮助!
谢谢,
乔
Because Magento uses an EAV model for its extensible catalog attributes, not every attribute may be carried over onto every page. Try fiddling with the "displayed in catalog" and other related variables on the attribute, and it may fix your problem. If not, say so and we can try other things.
Hope that helps!
Thanks,
Joe