TransWikia.com

Magento2 Assign company name to customer while placing an order

Magento Asked on December 7, 2021

I would like to assign a company name to the customer when place an order. Here I have tried using the below logic and able to assign it to the customer. But I am unable to view that particular orders in the customer dashboard and it’s showing company/accessdenied in the URL.

If I comment on this code that respected order I am able to view.

Note:

  1. This logic in the helper file and I am calling this method from another file bypassing the customer id as parameter. Can anyone face this kind of issue? Please advise.
  2. I have logged into the company profile dashboard and provided roles and permissions but no luck.

Magento verision is : 2.3 (B2B)

<?php 
namespace DemoTestHelper;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;
use PsrLogLoggerInterface; 
use MagentoFrameworkAppResourceConnection;
use MagentoCompanyModelResourceModelCustomer;
use MagentoCompanyApiDataCompanyCustomerInterface;

class Data extends AbstractHelper
{
/**
 * @var CompanyManagementInterface
 */
private $companyRepository;

 /**
 * @var Context
 */
public $context;

/**
 * @var ResourceConnection
 */
protected $resource;

/**
 * @var CompanyCustomerInterface
 */
protected $companyCustomerInterface;

/**
 * @var Customer
 */
protected $customer;


public function __construct(
    Context $context,
    LoggerInterface $logger,
    ResourceConnection $resource,
    CompanyCustomerInterface $companyCustomerInterface,
    Customer $companyCustomerResourceModel
) {
    $this->logger = $logger;       
    $this->resource = $resource;
    $this->companyCustomerInterface = $companyCustomerInterface;
    $this->companyCustomerResourceModel = $companyCustomerResourceModel;
}

public function assignCompany($customerId) {

try {
        $companyId = 2;
        if ($companyId && $customerId) {
            $connection = $this->resource->getConnection ();
            $select = $connection->select ()->from (['co' => 'company_advanced_customer_entity'])
                ->where ('co.customer_id = "' . $customerId . '" AND co.company_id = ' . $companyId . '');
            $checkMapping = $connection->fetchRow ($select);
            if ($checkMapping) {
                return true;
            } else {
                $companyMapping = $this->companyCustomerInterface;
                $companyMapping->setCustomerId ($customerId);
                $companyMapping->setCompanyId ($companyId);
                $companyMapping->setStatus (1);
                $companyMappingObject = $this->companyCustomerResourceModel->saveAdvancedCustomAttributes ($companyMapping);
                if ($companyMappingObject):
                    return true;
                endif;
            }
        }
        return false;
     } catch (Exception $e) {
        $this->logger->warning('Something went wrong when assign customer to company' . $e->getMessage());
    }
}

One Answer

Check Company User Roles and Permissions

Answered by Shivanand Chary on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP