根据产品属性判断前端是否开启购买

2.58K 浏览开发笔记问题

根据产品属性判断前端是否开启购买

需求,有些产品仅做于展示,有些产品则可以加入销售,这个时候需要在后天产品管理里加一个属性来做判断是否开启购买。话不多说上步骤。

一 通过setup新建产品属性

app\code\CP\Lessohomeppw\Setup\UpgradeData.php

<?php<br /><br />namespace CP\Lessohomeppw\Setup;<br /><br />use Magento\Eav\Setup\EavSetup;<br />use Magento\Eav\Setup\EavSetupFactory;<br />use Magento\Framework\Setup\UpgradeDataInterface;<br />use Magento\Framework\Setup\ModuleContextInterface;<br />use Magento\Framework\Setup\ModuleDataSetupInterface;<br /><br />class UpgradeData implements UpgradeDataInterface<br />{<br />    private $eavSetupFactory;<br />    <em>/**<br />    * Init<br />    *<br />    * </em><strong><em>@param </em></strong><em>EavSetupFactory $eavSetupFactory<br />    */<br />    </em>public function __construct(EavSetupFactory $eavSetupFactory)<br />    {<br />        $this->eavSetupFactory = $eavSetupFactory;<br />    }<br />    <em>/**<br />     * </em><strong><em>@param </em></strong><em>ModuleDataSetupInterface $setup<br />     * </em><strong><em>@param </em></strong><em>ModuleContextInterface $context<br />     */<br />    </em>// @codingStandardsIgnoreStart     <br />    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) <br />    {<br />        <em>/** </em><strong><em>@var </em></strong><em>EavSetup $eavSetup */<br />        </em>$setup->startSetup();<br />        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);<br />        <em>/**<br />        * Add attributes is_newproduct to the eav/attribute<br />        */<br />            </em>if (version_compare($context->getVersion(), '1.0.1') < 0) {<br />                $eavSetup->addAttribute(<br />                    \Magento\Catalog\Model\Product::<em>ENTITY</em>,<br />                    'pdp_add_cart',<br />                    [<br />                        'group' => 'General',<br />                        'type' => 'int',<br />                        'backend' => '',<br />                        'frontend' => '',<br />                        'label' => 'Product Add To Cart',<br />                        'input' => 'select',<br />                        'class' => '',<br />                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',<br />                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::<em>SCOPE_GLOBAL</em>,<br />                        'visible' => true,<br />                        'required' => false,<br />                        'user_defined' => false,<br />                        'default' => '0',<br />                        'searchable' => false,<br />                        'filterable' => false,<br />                        'comparable' => false,<br />                        'visible_on_front' => false,<br />                        'used_in_product_listing' => true,<br />                        'unique' => false,<br />                        'apply_to' => 'simple,configurable,virtual,bundle,downloadable'<br />                    ]<br />                );        <br />            }<br />        $setup->endSetup();<br />    }<br />}<br /><br />

二 通过observer控制显示xml

app\code\CP\Lessohomeppw\Observer\ProductViewLayout.php

<?php<br /><em>/**<br /> * Copyright © Magento, Inc. All rights reserved.<br /> * See COPYING.txt for license details.<br /> */<br /></em>namespace CP\Lessohomeppw\Observer;<br />use Magento\Framework\Event\ObserverInterface;<br /><br /><br />class ProductViewLayout implements ObserverInterface<br />{<br />    protected $_resultPageFactory;<br /><br />    public function __construct(\Magento\Framework\View\Result\PageFactory $resultPageFactory) {<br />        $this->_resultPageFactory = $resultPageFactory;<br />    }<br />    public function execute(\Magento\Framework\Event\Observer $observer)<br />    {<br />        $objectManager = \Magento\Framework\App\ObjectManager::<em>getInstance</em>();<br />        $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($observer->getProduct()->getId());<br />      $is_add_to_cart = $_product->getData('pdp_add_cart');<br />        if(isset($is_add_to_cart)&&$is_add_to_cart==1){<br />            $page = $this->_resultPageFactory->create();<br />            $page->addHandle('catalog_product_custom_layout');<br />        }<br />        return $this;<br />    }<br />}

三 添加event.xml

app\code\CP\Lessohomeppw\etc\frontend\events.xml

<?xml version="1.0"?><br /><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"><br />    <event name="catalog_controller_product_view"><br />        <observer name="custom_product_view_layout" instance="CP\Lessohomeppw\Observer\ProductViewLayout" /><br />    </event><br /></config>

四 添加配置xml文件 这里就是对应开启购买显示的xml配置,因为默认配置文件是不予显示购买,所以这里配置有购买功能的xml

app\code\CP\Lessohomeppw\view\frontend\layout\catalog_product_custom_layout.xml

<?xml version="1.0"?><br /><!--<br />/**<br /> * Copyright © 2016 Magento. All rights reserved.<br /> * See COPYING.txt for license details.<br /> */<br />--><br /><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><br />    <body><br />        <referenceBlock name="logo"><br />            <arguments><br />                <argument name="logo_file" xsi:type="string">images/logo.svg</argument><br />            </arguments><br />        </referenceBlock><br />        <!-- <referenceBlock name="catalog.topnav" remove="true" /> --><br />        <referenceBlock name="top.links" remove="true" /><br />        <referenceBlock name="footer_links" remove="true" /><br />        <referenceBlock name="form.subscribe" remove="true" /><br />        <referenceBlock name="header.panel.wrapper" remove="true"/><br />        <referenceContainer name="header.container"><br />            <referenceBlock name="minicart" remove="true"/><br />         <block class="CP\Category\Block\Categories" name="menu.category" template="CP_Category::categories.phtml" after="logo" /><br />        </referenceContainer><br />        <referenceBlock name="report.bugs" remove="true"/><br />        <referenceBlock name="advanced-search-link" remove="true"/><br />        <referenceContainer name="product.info.main"><br />            <referenceBlock name="page.main.title" remove="false" /><br />            <referenceBlock name="product.info.overview" remove="false" /><br />         <referenceBlock name="product.info.description" remove="false" /><br />         <block class="Magento\Catalog\Block\Product\View" name="custom.layout.details" template="Magento_Theme::product/addtocartview.phtml" before="-" /><br />         <referenceBlock name="custom.product.view" remove="true" /><br />        </referenceContainer><br />        <!-- Remove product detail page extra block --><br />        <referenceBlock name="product.info.review" remove="false"/><br />        <referenceBlock name="product.price.final" remove="false"/>       <br />        <referenceBlock name="product.info.stock.sku" remove="false"/><br />        <referenceBlock name="product.info.addtocart" remove="false" /><br />        <referenceBlock name="product.info.mailto" remove="false" />        <br />        <referenceBlock name="catalog.compare.sidebar" remove="true"/><br />        <referenceBlock name="view.addto.compare" remove="true" /><br />        <referenceBlock name="view.addto.wishlist" remove="true" /><br />        <referenceBlock name="reviews.tab" remove="false" /><br />        <referenceBlock name="product.price.tier" remove="false" />         <br />        <referenceBlock name="catalog.product.related" remove="false" /><br />        <referenceBlock name="product.info.upsell" remove="false" /><br />        <referenceBlock name="product.info.additional" remove="false" /><br />        <referenceBlock name="product.info.form.options" remove="false" /><br />        <referenceBlock name="product.info" remove="false" /><br />        <referenceBlock name="product.info.addto" remove="false" /><br /><br />        <!-- Header Div Structure --><br />        <referenceContainer name="header-wrapper"><br />            <container name="section.logo" htmlTag="div" htmlClass="logo-menu" before="-" /><br />            <container name="section.search" htmlTag="div" htmlClass="searchbox" after="section.logo" /><br />            <container name="section.order.link" htmlTag="div" htmlClass="order-link" after="section.search"><br />                <block class="Magento\Cms\Block\Block" name="order.link"><br />                    <arguments><br />                        <argument name="block_id" xsi:type="string">order_link</argument><br />                    </arguments><br />                </block><br />            </container><br />        </referenceContainer><br />        <move element="logo" destination="section.logo" before="-" /><br />        <move element="top.search" destination="section.search" before="-" /><br />        <move element="test.header" destination="section.search" after="top.search" /><br />        <move element="page.main.title" destination="category.view.container" after="category.image" /><br />        <move element="breadcrumbs" destination="columns.top" after="-" /><br /><br />        <referenceContainer name="sidebar.additional"><br />            <referenceBlock name="catalog.compare.sidebar" remove="true"/><br />            <referenceBlock name="wishlist_sidebar" remove="true"/><br />        </referenceContainer><br />        <move element="menu.category" destination="page.top" before="navigation.sections" />        <br />  <br />    </body><br /></page>

五 module 模块更新

app\code\CP\Lessohomeppw\etc\module.xml

<?xml version="1.0"?><br /><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><br />    <module name="CP_Lessohomeppw" setup_version="1.0.1"><br />    </module><br /></config>

Done

后台显示

此图像的alt属性为空;文件名为image-1024x243.png

前台两种界面

展示界面

此图像的alt属性为空;文件名为image-1-1024x475.png

销售界面

此图像的alt属性为空;文件名为image-2-1024x503.png

1