2

自定义内容,只需要依照原路经在自己模块下新建相同的路径内容,

添加自定义内容

step1.新建/xxx/xxx/Block/Product/View/Extra.php


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


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


<?php
$_product = $block->getProduct();
      echo $_product->getName();
?>
<h3>This is Add Custom Content in Catalog Product View Page</h3>

打开前台,可以看到效果

图片描述

添加Tab选项

继续在catalog_product_view.xml中添加 代码,如下


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


<?php
// get current product
$product = $block->getProduct();
echo $product->getName();
// Use product according to your requirement
?>

图片描述


denson
366 声望57 粉丝