我需要让我的客户能够选择他们的用户组。我使用了在StackOverFlow中找到的脚本(如果有人需要链接,这里是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.'));
}
}
}
这很有效,除了它显然还显示“ NO LOGGED IN”用户组。我需要它只显示ID为1(客户)和6(公司)的用户组,而不显示ID为0(未登录)的用户组。
答案 0 :(得分:0)
您可以更新CustomerGroup.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();
// 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;
}
}