Assign customer to company Magento 2 B2B.

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;
    }
}