【问题标题】:Magento 2 filter with custom customer attribute in create order select customer gridMagento 2过滤器在创建订单选择客户网格中具有自定义客户属性
【发布时间】:2020-06-17 11:34:01
【问题描述】:

我正在开发 Magento 2。在管理 -> 销售 -> 订单中创建订单,它显示所有客户列表。我想使用自定义客户属性过滤该列表。我已经创建了属性。

示例:

客户属性:允许创建订单 -> 是/否

在创建订单客户列表中,只有那些客户应该显示哪个属性值为“是”。

【问题讨论】:

    标签: magento2


    【解决方案1】:

    我花了一段时间,但我明白了,希望你也可以让它工作:

    使用registration.php、etc/module.xml 和常用的composer.json 制作一个模块。可以使用扩展生成器或其他在线工具..

    然后在你的模块中创建一个 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">
        <preference for="Magento\Sales\Model\ResourceModel\Order\Customer\Collection" type="Eighties\CustAttr2\Model\ResourceModel\Order\Customer\Collection" />     
    </config>
    

    然后在你的模块中创建 Setup/InstallData.php:

    <?php
    
    namespace Eighties\CustAttr2\Setup;
    
    use Magento\Eav\Setup\EavSetup;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Eav\Model\Config;
    use Magento\Customer\Model\Customer;
    
    class InstallData implements InstallDataInterface
    {
        private $eavSetupFactory;
    
        public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
        {
            $this->eavSetupFactory = $eavSetupFactory;
            $this->eavConfig       = $eavConfig;
        }
    
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->addAttribute(
                Customer::ENTITY,
                'allow_customer_order_create',
                [
    
                    'label'                 => 'Allow customer creating orders',
                    'input'                 => 'boolean',
                    'required'              => false,
                    'sort_order'            => 900,
                    'visible'               => true,
                    'system'                => false,
                    'is_used_in_grid'       => false,
                    'is_visible_in_grid'    => false,
                    'is_filterable_in_grid' => false,
                    'is_searchable_in_grid' => false,
                    'default'               => 0
                ]
            );
            $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'allow_customer_order_create');
    
            // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $sampleAttribute->setData(
                'used_in_forms',
                ['adminhtml_customer']
    
            );
            $sampleAttribute->save();
        }
    }
    

    然后在模块中制作Model/ResourceModel/Order/Customer/Collection.php

    <?php
    /**
     * Customer Grid Collection
     *
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Eighties\CustAttr2\Model\ResourceModel\Order\Customer;
    
    class Collection extends \Magento\Customer\Model\ResourceModel\Customer\Collection
    {
        /**
         * @return $this
         */
        protected function _initSelect()
        {
            parent::_initSelect();
            $this->addNameToSelect()->addAttributeToSelect(
                'email'
            )->addAttributeToSelect(
                'created_at'
            )->addAttributeToSelect(
                'allow_create_order'
            )->joinAttribute(
                'billing_postcode',
                'customer_address/postcode',
                'default_billing',
                null,
                'left'
            )->joinAttribute(
                'billing_city',
                'customer_address/city',
                'default_billing',
                null,
                'left'
            )->joinAttribute(
                'billing_telephone',
                'customer_address/telephone',
                'default_billing',
                null,
                'left'
            )->joinAttribute(
                'billing_regione',
                'customer_address/region',
                'default_billing',
                null,
                'left'
            )->joinAttribute(
                'billing_country_id',
                'customer_address/country_id',
                'default_billing',
                null,
                'left'
            )->joinField(
                'store_name',
                'store',
                'name',
                'store_id=store_id',
                null,
                'left'
            )->joinField(
                'website_name',
                'store_website',
                'name',
                'website_id=website_id',
                null,
                'left'
            );
            return $this->addAttributeToFilter('allow_customer_order_create', array('eq' => 1));
        }
    }
    

    'allow_customer_order_create' 是属性名称。

    然后运行 ​​setup:upgrade 和所有命令来备份网站(静态内容等),并清除缓存。

    n.b.如果您的属性第一次生成并且不再生成...从数据库中的“setup_module”表中删除模块条目。

    如果您不再需要创建属性,只需将 InstallData.php 留在外面。并将属性名称更改为您的属性名称。

    希望你能成功!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多