初学者

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

  • 1

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

一 通过setup新建产品属性

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

<?php

namespace CP\Lessohomeppw\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

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

二 通过observer控制显示xml

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

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace CP\Lessohomeppw\Observer;
use Magento\Framework\Event\ObserverInterface;


class ProductViewLayout implements ObserverInterface
{
protected $_resultPageFactory;

public function __construct(\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
$this->_resultPageFactory = $resultPageFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($observer->getProduct()->getId());
$is_add_to_cart = $_product->getData('pdp_add_cart');
if(isset($is_add_to_cart)&&$is_add_to_cart==1){
$page = $this->_resultPageFactory->create();
$page->addHandle('catalog_product_custom_layout');
}
return $this;
}
}

三 添加event.xml

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

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

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

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

<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 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>
<referenceBlock name="logo">
<arguments>
<argument name="logo_file" xsi:type="string">images/logo.svg</argument>
</arguments>
</referenceBlock>
<!-- <referenceBlock name="catalog.topnav" remove="true" /> -->
<referenceBlock name="top.links" remove="true" />
<referenceBlock name="footer_links" remove="true" />
<referenceBlock name="form.subscribe" remove="true" />
<referenceBlock name="header.panel.wrapper" remove="true"/>
<referenceContainer name="header.container">
<referenceBlock name="minicart" remove="true"/>
<block class="CP\Category\Block\Categories" name="menu.category" template="CP_Category::categories.phtml" after="logo" />
</referenceContainer>
<referenceBlock name="report.bugs" remove="true"/>
<referenceBlock name="advanced-search-link" remove="true"/>
<referenceContainer name="product.info.main">
<referenceBlock name="page.main.title" remove="false" />
<referenceBlock name="product.info.overview" remove="false" />
<referenceBlock name="product.info.description" remove="false" />
<block class="Magento\Catalog\Block\Product\View" name="custom.layout.details" template="Magento_Theme::product/addtocartview.phtml" before="-" />
<referenceBlock name="custom.product.view" remove="true" />
</referenceContainer>
<!-- Remove product detail page extra block -->
<referenceBlock name="product.info.review" remove="false"/>
<referenceBlock name="product.price.final" remove="false"/>
<referenceBlock name="product.info.stock.sku" remove="false"/>
<referenceBlock name="product.info.addtocart" remove="false" />
<referenceBlock name="product.info.mailto" remove="false" />
<referenceBlock name="catalog.compare.sidebar" remove="true"/>
<referenceBlock name="view.addto.compare" remove="true" />
<referenceBlock name="view.addto.wishlist" remove="true" />
<referenceBlock name="reviews.tab" remove="false" />
<referenceBlock name="product.price.tier" remove="false" />
<referenceBlock name="catalog.product.related" remove="false" />
<referenceBlock name="product.info.upsell" remove="false" />
<referenceBlock name="product.info.additional" remove="false" />
<referenceBlock name="product.info.form.options" remove="false" />
<referenceBlock name="product.info" remove="false" />
<referenceBlock name="product.info.addto" remove="false" />

<!-- Header Div Structure -->
<referenceContainer name="header-wrapper">
<container name="section.logo" htmlTag="div" htmlClass="logo-menu" before="-" />
<container name="section.search" htmlTag="div" htmlClass="searchbox" after="section.logo" />
<container name="section.order.link" htmlTag="div" htmlClass="order-link" after="section.search">
<block class="Magento\Cms\Block\Block" name="order.link">
<arguments>
<argument name="block_id" xsi:type="string">order_link</argument>
</arguments>
</block>
</container>
</referenceContainer>
<move element="logo" destination="section.logo" before="-" />
<move element="top.search" destination="section.search" before="-" />
<move element="test.header" destination="section.search" after="top.search" />
<move element="page.main.title" destination="category.view.container" after="category.image" />
<move element="breadcrumbs" destination="columns.top" after="-" />

<referenceContainer name="sidebar.additional">
<referenceBlock name="catalog.compare.sidebar" remove="true"/>
<referenceBlock name="wishlist_sidebar" remove="true"/>
</referenceContainer>
<move element="menu.category" destination="page.top" before="navigation.sections" />

</body>
</page>

五 module 模块更新

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

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

Done

后台显示

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

前台两种界面

展示界面

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

销售界面

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

分享到

您必须登录才能添加回答。