如何在magento 2上的可配置产品页面上添加自定义phtml文件

时间:2018-09-28 06:26:31

标签: php module magento2

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

Click here to see the requirements

请查看我创建的模块文件以及经过Google搜索后的操作-

  • 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>

  • 视图/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>

  • 视图/模板/产品/视图/类型/选项/configurable.phtml

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

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

谢谢

2 个答案:

答案 0 :(得分: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>

  

view / templates / extra.phtml

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

答案 1 :(得分:0)

创建文件为

应用/代码/供应商/模块/视图/前端/布局/catalog_product_view.xml

文件:catalog_product_view.xml

[_|T2]

在PHTML文件中

应用程序/代码/供应商/模块/视图/前端/模板/产品/视图/cusrom.phtml

<?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>