简体   繁体   English

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

[英]how to add custom phtml file on the configurable product page on magento 2

i am creating a custom module and i want to include a custom phtml file above the attributes on the configurable product page...我正在创建一个自定义模块,我想在可配置产品页面上的属性上方包含一个自定义 phtml 文件...

Click here to see the requirements点击这里查看要求

Please take a look on the module files i have created and what i have done after some google search -请查看我创建的模块文件以及我在谷歌搜索后所做的工作 -

  • etc/di.xml等/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视图/前端/布局/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视图/模板/产品/视图/类型/选项/configurable.phtml

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

but i am not able to get the file on configurable product view page, What's wrong i am doing, please let me know.但我无法在可配置的产品视图页面上获取文件,我在做什么错,请告诉我。

thanks谢谢

I have achieved by this way.我通过这种方式实现了。

view/frontened/layout/catalog_product_view_type_configurable.xml视图/前端/布局/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查看/模板/extra.phtml

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

create file as创建文件为

app/code/Vendor/Module/view/frontend/layout/catalog_product_view.xml应用程序/代码/供应商/模块/视图/前端/布局/catalog_product_view.xml

file: 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>

in PHTML file在 PHTML 文件中

app/code/Vendor/Module/view/frontend/templates/product/view/cusrom.phtml应用程序/代码/供应商/模块/视图/前端/模板/产品/视图/cusrom.phtml

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

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

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