简体   繁体   English

list.phtml中特定产品的Magento呼叫块

[英]Magento call block for specific product in list.phtml

I want to print some additional data for each product on list.phtml that will depend on the product. 我想在list.phtml上为每种产品打印一些其他数据,这将取决于产品。 For example, print html with New and Sale labels for each product (this is just an example). 例如,为每个产品打印带有新标签和销售标签的html(这只是一个示例)。 I want to somehow separate logic from .phtml file and remain in phtml just a call for it. 我想以某种方式将逻辑与.phtml文件分开,并仅将其保留在phtml中。 What is the best way to achieve it? 最好的方法是什么? Also I want to minimize overriding core files and make it more independent. 我也想最小化覆盖的核心文件并使它更加独立。 My ideas are: 我的想法是:

  1. Create my own block, create and call it in .phtml and pass there my product with setData: 创建我自己的块,在.phtml中创建并调用它,然后将带有setData的产品传递到那里:

     $this->getLayout()->createBlock("namespace/block")-> setTemplate("path/to/template")->setData('product', $_product) 

    and then call getProduct() inside my block .php file. 然后在我的块.php文件中调用getProduct()。 But for some reasons that doesn't work. 但是由于某些原因,这是行不通的。

  2. The best-looking solution for me, but not sure if it's allowed. 对我来说最好的解决方案,但不确定是否允许。 Create new block in layout files and call it with getChildHtml('block_name') . 在布局文件中创建新块,然后使用getChildHtml('block_name')调用。 But I don't know how to pass there current product or how to make it to be able to access through $this inside my .php file. 但是我不知道如何传递当前产品或如何使其能够通过我的.php文件中的$ this访问。

  3. Override product block and add my own methods like getRibbons(). 覆盖产品块并添加我自己的方法,例如getRibbons()。 The worst solution for me because this will requre html writing in .php block and will override core block. 对我而言,最糟糕的解决方案是,因为这将需要在.php块中编写html并覆盖核心块。

I'm pretty new to magento, maybe I am missing some basic concepts? 我是magento的新手,也许我缺少一些基本概念?

Extend your Product Block PHP and add new methods you want in it. 扩展您的Product Block PHP,并在其中添加所需的新方法。 Then in your module.xml where you are referencing core Product Block, replace it with your newly created Block. 然后在要引用核心产品模块的module.xml中,将其替换为新创建的模块。

Now, from PHTML files you can easily call the PHP block's methods using $this 现在,从PHTML文件中,您可以使用$this轻松调用PHP块的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM