-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Magento B2B - Cannot Assign Customer Group ID #38187
Comments
Hi @portchris. Thank you for your report.
Join Magento Community Engineering Slack and ask your questions in #github channel. |
@magento-admin I am working on this. |
Hi @engcom-Bravo. Thank you for working on this issue.
|
Module: Magento_CompanyAs this appears to be specifically related to Magento B2B companies I have created a patch within the Magento_Company module in order not to disturb the Magento_Customer module (used in other flavours of Adobe Commerce). Here is my patch --- a/vendor/magento/module-company/Controller/Adminhtml/Customer/Index/MassAssignGroup.php
+++ b/vendor/magento/module-company/Controller/Adminhtml/Customer/Index/MassAssignGroup.php
@@ -0,0 +1,156 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Magento\Company\Controller\Adminhtml\Customer\Index;
+
+use Magento\Backend\App\Action\Context;
+use Magento\Company\Api\Data\CompanyInterface;
+use Magento\Company\Api\CompanyManagementInterface;
+use Magento\Company\Api\CompanyRepositoryInterface;
+use Magento\Company\Model\SaveHandler\CustomerGroup;
+use Magento\Company\Model\Company;
+use Magento\Customer\Controller\Adminhtml\Index\MassAssignGroup as Original;
+use Magento\Eav\Model\Entity\Collection\AbstractCollection;
+use Magento\Framework\Exception\CouldNotSaveException;
+use Magento\Framework\Exception\NoSuchEntityException;
+use Magento\Framework\Exception\LocalizedException;
+use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
+use Magento\Ui\Component\MassAction\Filter;
+use Magento\Customer\Api\CustomerRepositoryInterface;
+
+class MassAssignGroup extends Original
+{
+ /**
+ * @var Company|null
+ */
+ private ?CompanyInterface $_company;
+
+ /**
+ * @var CustomerGroup
+ */
+ private CustomerGroup $_customerGroup;
+
+ /**
+ * @var CompanyRepositoryInterface
+ */
+ private CompanyRepositoryInterface $_companyRepository;
+
+ /**
+ * @var CompanyManagementInterface
+ */
+ private CompanyManagementInterface $_companyManagement;
+
+ /**
+ * @param Context $context
+ * @param Filter $filter
+ * @param CustomerGroup $customerGroup
+ * @param CollectionFactory $collectionFactory
+ * @param CompanyRepositoryInterface $companyRepository
+ * @param CompanyManagementInterface $companyManagement
+ * @param CustomerRepositoryInterface $customerRepository
+ */
+ public function __construct(
+ Context $context,
+ Filter $filter,
+ CustomerGroup $customerGroup,
+ CollectionFactory $collectionFactory,
+ CompanyRepositoryInterface $companyRepository,
+ CompanyManagementInterface $companyManagement,
+ CustomerRepositoryInterface $customerRepository
+ ) {
+ parent::__construct(
+ $context,
+ $filter,
+ $collectionFactory,
+ $customerRepository
+ );
+
+ $this->_company = null;
+ $this->_customerGroup = $customerGroup;
+ $this->_companyRepository = $companyRepository;
+ $this->_companyManagement = $companyManagement;
+ }
+
+ /**
+ * Add a warning message to the admin attempting to mass assign a customer to a group.
+ *
+ * @param AbstractCollection $collection
+ * @return \Magento\Backend\Model\View\Result\Redirect
+ */
+ protected function massAction(AbstractCollection $collection)
+ {
+ $resultRedirect = parent::massAction($collection);
+ $groupId = $this
+ ->getRequest()
+ ->getParam('group');
+
+ if ($groupId === null) {
+ return $resultRedirect;
+ }
+
+ foreach ($collection->getAllIds() as $customerId) {
+ try {
+ if (!$this->_isCompanyAdmin((int) $customerId)) {
+ $this->messageManager->addWarningMessage(
+ __(
+ "Cannot assign customer %1 to the group %2 because they are not a company admin.",
+ $customerId,
+ $groupId
+ )
+ );
+
+ continue;
+ }
+
+ $company = $companyInitial = $this->_getCompanyByCustomerId((int) $customerId);
+ $company->setCustomerGroupId($groupId);
+ $this->_customerGroup->execute($company, $companyInitial);
+ $this->_companyRepository->save($company);
+ } catch (LocalizedException | NoSuchEntityException | CouldNotSaveException $e) {
+ $this->messageManager->addErrorMessage(
+ __(
+ "Error attempting to assign customer %1 to group %2. %3.",
+ $customerId,
+ $groupId,
+ $e->getMessage()
+ )
+ );
+ }
+ }
+
+ return $resultRedirect;
+ }
+
+ /**
+ * Check if this customer is a company admin, else this operation is not allowed.
+ *
+ * @param int $customerId
+ * @return boolean
+ */
+ private function _isCompanyAdmin(int $customerId): bool
+ {
+ $company = $this->_getCompanyByCustomerId($customerId);
+ if ($company) {
+ $superUserId = (int) $company->getSuperUserId();
+ return $customerId === $superUserId;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return cached company object if already initialised. Otherwise initialise.
+ *
+ * @param int $customerId
+ * @return Company|null
+ */
+ private function _getCompanyByCustomerId(int $customerId): ?CompanyInterface
+ {
+ if ($this->_company === null) {
+ $this->_company = $this->_companyManagement->getByCustomerId($customerId);
+ }
+
+ return $this->_company;
+ }
+}
--- a/vendor/magento/module-company/etc/adminhtml/di.xml
+++ b/vendor/magento/module-company/etc/adminhtml/di.xml
@@ -38,4 +38,8 @@
<type name="Magento\Catalog\Model\Product\ReservedAttributeList">
<plugin name="reservedAttributeListPlugin" type="Magento\Company\Plugin\Catalog\Model\Product\ReservedAttributeListPlugin"/>
</type>
+ <preference
+ for="Magento\Customer\Controller\Adminhtml\Index\MassAssignGroup"
+ type="Magento\Company\Controller\Adminhtml\Customer\Index\MassAssignGroup"
+ />
</config> Web APIAdditionally, I have a patch here that allows the request data not to be reset: --- a/vendor/magento/module-customer/Model/Plugin/UpdateCustomer.php
+++ b/vendor/magento/module-customer/Model/Plugin/UpdateCustomer.php
@@ -59,6 +59,11 @@
if (!isset($bodyParams['customer']['Id']) && $customerId) {
if ($customerId === $customerSessionId || $customerSessionId === 0) {
$customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer);
+
+ /** @var \Magento\Customer\Model\Data\Customer $customer */
+ foreach ($bodyParams['customer'] as $param => $value) {
+ $customer->setData($param, $value);
+ }
}
} |
Hi @portchris, Thank you for reporting and collaboration. Verified the issue on Magento 2.4-develop instance with EE edition and the issue is not reproducible.Kindly refer the screenshots. ![]() Customer Group field is not editable. As per the Magento devdocs only one customer group can be assigned to company at a time we cannot change afterwards.please refer this document https://experienceleague.adobe.com/docs/commerce-admin/b2b/companies/account-company-customer-group.html?lang=en. ![]() Thanks. |
Hi @portchris, Kindly provide latest update on this comment #38187 (comment) still you are working on this issue if you are able to reproduce the issue please elaborate the steps to reproduce.Please let us know still if you are facing any issue. Thanks. |
Hey @engcom-Bravo If that's the case then perhaps the Mass Action here should be better validated: Because at the moment what's happening is a success message but nothing happens. It appears as a bug. My patch (above) By the way accounts for this and throws a warning if the customer is NOT a company admin. |
Hi @portchris, Thanks for your update. Verified the issue on Magento 2.4-develop instance and the issue is not reproducible.Kindly refer the screenshots. Mass action is working fine after success message appears for Assign a Customer Group the selected records has been changed for appropriate Group. Could you please check the issue in Latest Magento 2.4-develop instance and please let us know if we are missing anything. Thanks. |
Don't forget to install B2B module: composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition .
composer require magento/extension-b2b smile/elasticsuite
composer update --lock |
Hi @portchris, Thanks for your update. We have verified the issue Magento 2.4-develop instance with Enterprise edition with b2b modules and the issue is not reproducible,Could you please check the issue in Latest 2.4-develop instance with EE edition. It is recommended to verify the issue in Magento 2.4-develop instance and let us know if we are missing anything. Thanks |
Hi @portchris, We have noticed that this issue has not been updated since long time. Hence we assume that this issue is fixed now, so we are closing it. Please feel to raise a fresh ticket or reopen this ticket if you need more assistance on this. Thanks |
Preconditions and environment
Steps to reproduce
Install fresh version of Magento:
composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition . composer require magento/extension-b2b smile/elasticsuite composer update --lock
Create DB:
Install and deploy
Login to Admin with newly created user https://magento2-demo.localhost/admin/ navigate to:
Create a new company + admin. Assign default customer group
Then, attempt to change the customer group via:
Expected result
The customer group is updated.
Actual result
The customer group is NOT updated despite a success message displaying.
Additional information
Is this a feature not a bug?
After looking into the code I've found the following:
vendor/magento/module-company/etc/di.xml
Magento\Company\Model\SaveHandler\CustomerGroup::execute
So this appears to be purposely negated in favour of the companies customer group. Despite this however, there is still a misleading message to the end user and this feature should either work or not be there at all.
Since this action is still available as a feature, this should still work to some capacity. That being:
If the customer is a company admin, this should be sufficient enough privileges to change the customer's group + company. Else throw a warning message.
Magento WebAPI
Additionally, I have observed that it is no longer possible to update a customer's group ID via Web API. This appears to be a result of the request parameter's being reverted/reset in:
Magento\Customer\Model\Plugin\UpdateCustomer::beforeSave
Release note
No response
Triage and priority
The text was updated successfully, but these errors were encountered: