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

2.95K 浏览开发笔记问题

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

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

一 通过setup新建产品属性

app\code\CP\Lessohomeppw\Setup\UpgradeData.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

/**
* 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文件 这里就是对应开启购买显示的xml配置,因为默认配置文件是不予显示购买,所以这里配置有购买功能的xml

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







images/logo.svg













































order_link

















五 module 模块更新

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





Done

后台显示

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

前台两种界面

展示界面

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

销售界面

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

1