Magento2 在创建订单选择客户网格中使用自定义客户属性的Magento 2过滤器

Magento2 在创建订单选择客户网格中使用自定义客户属性的Magento 2过滤器,magento2,Magento2,我正在做Magento 2。在管理->销售->订单创建订单中,显示所有客户列表。我想用自定义客户属性筛选该列表。我已经创建了属性 例如: 客户属性:允许创建订单->是/否 在“创建订单客户列表”中,只有那些客户应该显示哪个属性值为“是”。我花了一些时间,但我得到了它,希望您也能让它工作: 使用registration.php、etc/module.xml和常用的stuff composer.json创建一个模块。使用一个扩展生成器或者在线的东西 然后在模块中创建一个etc/di.xml文件: &

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

例如:

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


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

我花了一些时间,但我得到了它,希望您也能让它工作:

使用registration.php、etc/module.xml和常用的stuff 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>
<?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();
    }
}
然后在模块中设置Setup/InstallData.php:

<?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>
<?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));
    }
}
“允许客户订单创建”是属性名称

然后运行setup:upgrade和所有命令以使站点备份静态内容等,并清除缓存

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

如果您不再需要创建该属性,只需将InstallData.php去掉即可。。并将属性名称更改为属性名称

希望你能成功