自定義內容,只須要依照原路經在本身模塊下新建相同的路徑內容,php
step1.新建/xxx/xxx/Block/Product/View/Extra.phphtml
<?php /** * Created by PhpStorm. * User: denson * Date: 16-9-19 * Time: 下午15:51 */ namespace xxx\xxx\Block\Product\View; use Magento\Catalog\Block\Product\AbstractProduct; class Extra extends AbstractProduct //注意繼承對象 { public function getSomething() { return 'something'; } }
step2.新建/xxx/xxx/view/frontend/layout/catalog_product_view.xmlfrontend
<?xml version="1.0"?> <body> <referenceContainer name="product.info.main"> <block class="xxx\xxx\Block\Product\View\Extra" name="product.view.extra" template="xxx_xxx::product/view/extra.phtml" after="product.info.overview"> </block> </referenceContainer> </body>
step3.新建/xxx/xxx/view/frontend/templates/product/view/extra.phtmlui
<?php $_product = $block->getProduct(); echo $_product->getName(); ?> <h3>This is Add Custom Content in Catalog Product View Page</h3>
打開前臺,能夠看到效果spa
繼續在catalog_product_view.xml中添加 代碼,以下code
<?xml version="1.0"?> <page layout="2columns-left" 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="xxx\xxx\Block\Product\View\Extra" name="product.view.extra" template="xxx_xxx::product/view/extra.phtml" after="product.info.overview"> </block> </referenceContainer> <!--詳情頁添加Tab選項--> <referenceBlock name="product.info.details"> <block class="Magento\Catalog\Block\Product\View" name="demo.tab" template="xxx_xxx::demo_tab.phtml" group="detailed_info" > <arguments> <argument translate="true" name="title" xsi:type="string">Demo Tab</argument> </arguments> </block> </referenceBlock> </body> </page>
接在在veiw/templates/下建立tab_demo.phtml文件orm
<?php // get current product $product = $block->getProduct(); echo $product->getName(); // Use product according to your requirement ?>