在创建订单 select 客户网格中具有自定义客户属性的 Magento 2 过滤器
Magento 2 filter with custom customer attribute in create order select customer grid
我正在开发 Magento 2。在管理 -> 销售 -> 订单创建订单中,它显示了所有客户列表。我想用自定义客户属性过滤该列表。我已经创建了属性。
示例:
客户属性:允许创建订单 -> 是/否
在创建订单客户列表中,只有那些客户应该显示其属性值为 "Yes"。
我花了一些时间,但我明白了,希望你也能让它发挥作用:
制作一个包含 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' table 中删除模块条目。
如果您不再需要制作属性,只需将 InstallData.php 保留在外。并将属性名称更改为您的属性名称。
希望你能成功!
我正在开发 Magento 2。在管理 -> 销售 -> 订单创建订单中,它显示了所有客户列表。我想用自定义客户属性过滤该列表。我已经创建了属性。
示例:
客户属性:允许创建订单 -> 是/否
在创建订单客户列表中,只有那些客户应该显示其属性值为 "Yes"。
我花了一些时间,但我明白了,希望你也能让它发挥作用:
制作一个包含 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' table 中删除模块条目。
如果您不再需要制作属性,只需将 InstallData.php 保留在外。并将属性名称更改为您的属性名称。
希望你能成功!