magento2 如何给前台产品价格区域添加说明文本

2.87K 浏览开发笔记

magento2 如何给前台产品价格区域添加说明文本

开发中经常需要对价格区域进行调整,增加一些说明文本。

直接改模版 也可以,但是不是太灵活。

我们直接用php plugin扩展MagentoCatalogPricingRenderFinalPriceBox.php类的toHtml方法即可。

因为价格区域的html是通过FinalPriceBox.php的toHtml方法输出的。

假设你已有插件,我的插件为Zou_Demo,代码写在Zou_Demo插件里。

1,在di.xml里定义

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"     <type name="MagentoCatalogPricingRenderFinalPriceBox">         <plugin name="beforeFinalPriceBox" type="ZouDemoPluginCatalogPricingRenderFinalPriceBox"/>     </type> </config>

2,创建ZouDemoPluginCatalogPricingRenderFinalPriceBox.php

<?php  namespace ZouDemoPluginCatalogPricingRender; class FinalPriceBox {     public $helper;     protected $_request;     public function __construct(         MagentoFrameworkAppRequestInterface $request,         ZouDemoHelperData $helper         )     {         $this->helper = $helper;         $this->_request = $request;     }           public function afterToHtml($subject, $result)      {         $product = $subject->getSaleableItem();         $priceType = $subject->getPrice()->getPriceCode();         if($priceType != &#39;final_price&#39;){             return $result;         }         //echo get_class($product);         $result = &#39;<div class="product-unit">&#39;.__(&#39;Price&#39;).&#39; alk. / m2</div>&#39;.$result;         return $result;      }      }

这样就ok了。

最后于 7月前 被admin编辑 ,原因:
0