【问题标题】:how to add custom phtml file on the configurable product page on magento 2如何在 magento 2 的可配置产品页面上添加自定义 phtml 文件
【发布时间】:2018-09-28 06:26:31
【问题描述】:

我正在创建一个自定义模块,我想在可配置产品页面的属性上方包含一个自定义 phtml 文件...

Click here to see the requirements

请查看我创建的模块文件以及在谷歌搜索后我做了什么 -

  • etc/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">
    <preference for="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" type="Bay20\WarehouseConfigurable\Block\Rewrite\Product\View" />  
</config>
  • view/frontened/layout/catalog_product_view_type_configurable.xml

<?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <attribute name="class" value="page-product-configurable"/>
            <referenceBlock name="product.info.options.wrapper">
                <block class="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" name="product.info.options.configurable" as="options_configurable" before="-" template="product/view/type/options/configurable.phtml"/>
            </referenceBlock>
            
        </body>
  • view/templates/product/view/type/options/configurable.phtml

 <?php
    
    /** @var $block \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable*/
    //$_product    = $block->getProduct();
    //$_attributes = $block->decorateArray($block->getAllowAttributes());
    
    ?>
    
    <p>Hello there</p>

但我无法在可配置产品视图页面上获取文件,我在做什么错了,请告诉我。

谢谢

【问题讨论】:

    标签: php module magento2


    【解决方案1】:

    我已经通过这种方式实现了。

    view/frontened/layout/catalog_product_view_type_configurable.xml

    <?xml version="1.0"?>
     <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.main">
            <block after="product.info.price" class="Test\Mymodule\Block\Posts" name="extra" template="Test_Mymodule::extra.phtml" /> 
        </referenceBlock>        
    </body>
    

    查看/模板/extra.phtml

    <?php echo "<p>Extra Block</p>";
    

    【讨论】:

      【解决方案2】:

      创建文件为

      app/code/Vendor/Module/view/frontend/layout/catalog_product_view.xml

      文件:catalog_product_view.xml

      <?xml version="1.0"?>
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
              <referenceContainer name="product.info.main">
                  <block class="Magento\Catalog\Block\Product\View" after="product.info.price" 
                  as="custom"
                      template="Vendor_Module::product/view/custom.phtml"/>
              </referenceContainer>
          </body>
      </page>
      

      在 PHTML 文件中

      app/code/Vendor/Module/view/frontend/templates/product/view/cusrom.phtml

      <div class='custom-box'>
          <span class="custom">
                  hii from custom.phtml file
          </span>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2023-03-25
        • 1970-01-01
        相关资源
        最近更新 更多