在下拉菜单中编辑显示的用户组
Edit shown user groups in dropdown menu
我需要让我的客户能够选择他们的用户组。我使用了在 Whosebug 中找到的脚本(如果有人需要 link,这里是 https://magento.stackexchange.com/questions/239067/magento-2-customer-registration-with-customer-group/239071#239071):
我使用的代码是:
<?php
$blockObj= $block->getLayout()->createBlock('Nano\CommissionAgents\Block\CustomerGroups');
$groups = $blockObj->getCustomerGroup();
?>
<div class="field group_id required">
<label for="group_id" class="label"><span><?php /* @escapeNotVerified */ echo __('I buy as:') ?></span></label>
<div class="control">
<select name="group_id">
<?php foreach ($groups as $key => $data) { ?>
<option value="<?php echo $data['value'] ?>"><?php echo $data['label'] ?></option>
<?php } ?>
</select>
</div>
</div>
CustomerGroup.php
<?php
namespace Nano\CommissionAgents\Block;
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;
Class CustomerGroups extends Template {
public $_customerGroup;
public function __construct(
CustomerGroup $customerGroup
) {
$this->_customerGroup = $customerGroup;
}
public function getCustomerGroup() {
$groups = $this->_customerGroup->toOptionArray();
return $groups;
}
}
SaveCustomerGroupId.php
<?php
namespace Nano\CommissionAgents\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Message\ManagerInterface;
Class SaveCustomerGroupId implements ObserverInterface {
public $_customerRepositoryInterface;
public $_messageManager;
public function __construct(
CustomerRepositoryInterface $customerRepositoryInterface,
ManagerInterface $messageManager
) {
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_messageManager = $messageManager;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$accountController = $observer->getAccountController();
$request = $accountController->getRequest();
$group_id = $request->getParam('group_id');
try {
$customerId = $observer->getCustomer()->getId();
$customer = $this->_customerRepositoryInterface->getById($customerId);
$customer->setGroupId($group_id);
$this->_customerRepositoryInterface->save($customer);
} catch (Exception $e){
$this->_messageManager->addErrorMessage(__('Something went wrong! Please try again.'));
}
}
}
这很好用,除了它显然还显示了 "NOT LOGGED IN" 用户组。我需要它只显示 ID 为 1(客户)和 6(公司)的用户组,而不是 ID 为 0(未登录)的用户组。
您可以更新 CustomerGroup.php 并执行以下操作:
- 创建要删除的 ID 数组
- 遍历组
- 对被排除的群体进行array_search
- 删除(使用 unset())任何匹配的 ID
namespace Nano\CommissionAgents\Block;
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;
Class CustomerGroups extends Template {
public $_customerGroup;
public function __construct(
CustomerGroup $customerGroup
) {
$this->_customerGroup = $customerGroup;
}
public function getCustomerGroup() {
$groups = $this->_customerGroup->toOptionArray();
// Add group id's to this array you don't want to display in dropdown
$excludeGroups = [0];
foreach ($groups as $group) {
// Check if the group is equal to any groups you want to exclude
$groupKeyToRemove = array_search($group['value'], $excludeGroups);
// remove the group from the array
unset($groups[$groupKeyToRemove]);
}
// return the groups with only the values you want
return $groups;
}
}
我需要让我的客户能够选择他们的用户组。我使用了在 Whosebug 中找到的脚本(如果有人需要 link,这里是 https://magento.stackexchange.com/questions/239067/magento-2-customer-registration-with-customer-group/239071#239071): 我使用的代码是:
<?php
$blockObj= $block->getLayout()->createBlock('Nano\CommissionAgents\Block\CustomerGroups');
$groups = $blockObj->getCustomerGroup();
?>
<div class="field group_id required">
<label for="group_id" class="label"><span><?php /* @escapeNotVerified */ echo __('I buy as:') ?></span></label>
<div class="control">
<select name="group_id">
<?php foreach ($groups as $key => $data) { ?>
<option value="<?php echo $data['value'] ?>"><?php echo $data['label'] ?></option>
<?php } ?>
</select>
</div>
</div>
CustomerGroup.php
<?php
namespace Nano\CommissionAgents\Block;
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;
Class CustomerGroups extends Template {
public $_customerGroup;
public function __construct(
CustomerGroup $customerGroup
) {
$this->_customerGroup = $customerGroup;
}
public function getCustomerGroup() {
$groups = $this->_customerGroup->toOptionArray();
return $groups;
}
}
SaveCustomerGroupId.php
<?php
namespace Nano\CommissionAgents\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Message\ManagerInterface;
Class SaveCustomerGroupId implements ObserverInterface {
public $_customerRepositoryInterface;
public $_messageManager;
public function __construct(
CustomerRepositoryInterface $customerRepositoryInterface,
ManagerInterface $messageManager
) {
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_messageManager = $messageManager;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$accountController = $observer->getAccountController();
$request = $accountController->getRequest();
$group_id = $request->getParam('group_id');
try {
$customerId = $observer->getCustomer()->getId();
$customer = $this->_customerRepositoryInterface->getById($customerId);
$customer->setGroupId($group_id);
$this->_customerRepositoryInterface->save($customer);
} catch (Exception $e){
$this->_messageManager->addErrorMessage(__('Something went wrong! Please try again.'));
}
}
}
这很好用,除了它显然还显示了 "NOT LOGGED IN" 用户组。我需要它只显示 ID 为 1(客户)和 6(公司)的用户组,而不是 ID 为 0(未登录)的用户组。
您可以更新 CustomerGroup.php 并执行以下操作:
- 创建要删除的 ID 数组
- 遍历组
- 对被排除的群体进行array_search
- 删除(使用 unset())任何匹配的 ID
namespace Nano\CommissionAgents\Block;
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;
Class CustomerGroups extends Template {
public $_customerGroup;
public function __construct(
CustomerGroup $customerGroup
) {
$this->_customerGroup = $customerGroup;
}
public function getCustomerGroup() {
$groups = $this->_customerGroup->toOptionArray();
// Add group id's to this array you don't want to display in dropdown
$excludeGroups = [0];
foreach ($groups as $group) {
// Check if the group is equal to any groups you want to exclude
$groupKeyToRemove = array_search($group['value'], $excludeGroups);
// remove the group from the array
unset($groups[$groupKeyToRemove]);
}
// return the groups with only the values you want
return $groups;
}
}