【问题标题】:magento 2 how to get the product object in product detail tabmagento 2如何在产品详细信息选项卡中获取产品对象
【发布时间】:2020-06-17 11:44:48
【问题描述】:

谁能指导我如何在详细信息选项卡上加载或获取产品对象 https://prnt.sc/t1dpay
我想通过加载产品对象来在详细信息页面上显示数据 供应商/magento/module-catalog/view/frontend/templates/product/view/details.phtml

提前致谢

【问题讨论】:

标签: php magento magento2


【解决方案1】:

在 Magento 中有 Magento\Framework\Registry 类,其中包含(除其他外)“当前产品”,如果产品详细信息页面将是页面上显示的产品。

Magento\Framework\Registry 已弃用,但我还没有找到好的替代方案。因为它在大多数情况下都被弃用了,所以我创建一个只做一件事的类,从注册表中获取所需的值。它是直接使用已弃用的类本身或“重写它”之间的中间地带,它封装了已弃用的类,并且可以在将来必要时非常容易地换出(整个类或特别是 get() 的实现)。

use Magento\Framework\Registry;

class MyCurrentProduct {
    protected $registry;

    public function get(): ProductInterface {
        return $registry->get('current_product');
    }
}

我在示例中遗漏了一些内容,因为我不喜欢发布可复制粘贴的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多