You can add Customer to Company Programmatically by assignCustomer( ) function from CompanyManagementInterface.
Magento\Company\Api\CompanyManagementInterface Interface will be used for assign customer to Company in Magento 2.
You need Company id and Customer id to achieve this.
You can get assigned a company to customer programmatically by below way,
<?php class YourClass { public function __construct( \Magento\Company\Api\CompanyManagementInterface $companyRepository ) { $this->companyRepository = $companyRepository; } public function assignedCompany() { $companyId = 10; $customerId = 1; $company = null; if($companyId && $customerId) { $company = $this->companyRepository->assignCustomer($companyId,$customerId); } return $company; } }
Hello, unfortunatelly, the assignCustomer method hasn’t worked for me. Could it be because I created the customer through the customer factory and then used its Id to assign it to an existing company? In my case, the customer gets created as an individual user but doesn’t get assigned to the company I specified through the assignCustomer method. Do you have any suggestions how to undertake this problem?