当前位置: 技术文章>> 如何在Magento 2中以编程方式应用自定义产品属性验证

文章标题:如何在Magento 2中以编程方式应用自定义产品属性验证
  • 文章分类: Magento
  • 17901 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中以编程方式应用自定义产品属性验证的步骤:

第 1 步:InstallData.php在给定的以下路径

app\code\Vendor\Extension\Setup\InstallData.php

添加以下代码

<?php
namespace Vendor\Extension\Setup;
 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
 
class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
 
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
 
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            Product::ENTITY,
            'attribute_code',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'group' => 'General',
                'label' => 'Attribute Label',
                'input' => 'text',
                'frontend_class' => 'validate-greater-than-zero validate-digits',
                'class' => '',
                'source' => '',
                'sort_order' => 100,
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'adminhtml_only' => true,
                'filterable' => true,
                'comparable' => true,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

第 2 步: 在以下路径创建 di.xml

app\code\Vendor\Extension\etc\di.xml

现在,添加以下代码

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules">
        <plugin name="vendor_custom_validation_for_extension_product_attribute" type="Vendor\Extension\Plugin\Product\Validationrules"/>
    </type>
</config>

步骤 3:创建验证规则.php路径如下

app\code\Vendor\Extension\Plugin\Product\Validationrules.php

并且,按如下方式添加代码

<?php
namespace Vendor\Extension\Plugin\Product;
 
use Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules;
use Closure;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
 
class Validationrules
{
    public function aroundBuild(
        CatalogEavValidationRules $rulesObject,
        Closure $proceed,
        ProductAttributeInterface $attribute,
        array $data
    ){
        $rules = $proceed($attribute,$data);
        if($attribute->getAttributeCode() == 'attribute_code'){
            //custom filter
            $validationClasses = explode(' ', $attribute->getFrontendClass());
            foreach ($validationClasses as $class) {
                $rules[$class] = true;
            }
        }
        return $rules;
    }
}

结论:

因此,通过这种方式,您可以在Magento 2中以编程方式成功应用自定义产品属性验证。


推荐文章