Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

Get Company using Customer ID programmatically Magento 2 B2B

You can get Company Programmatically by getByCustomerId( ) function from CompanyManagementInterface. Magento\Company\Api\CompanyManagementInterface Interface will be used to get Company data using the customer id in Magento 2.

Get Company using Customer ID programmatically Magento 2 B2B
Written by
JKJigar Karangiya
Published
Reading time
1 min read

Jigar Karangiya · · 1 min read

You can get Company Programmatically by getByCustomerId( ) function from CompanyManagementInterface.

Magento\Company\Api\CompanyManagementInterface Interface will be used to get Company data using the customer id in Magento 2.

You need a Customer ID to achieve this.

You can get company details programmatically by the below way.

php
<?php
class YourClass {
    public function __construct(
        \Magento\Company\Api\CompanyManagementInterface $companyRepository
    ) {
        $this->companyRepository = $companyRepository;
    }
 
    public function getByCustomerId()
    {
        $customerId = 232;
        $company = $this->companyRepository->getByCustomerId($customerId);
        //echo $company->getCompanyName();
        return $company;
    }
}

Using this way you can get the company object of the company associated with a particular customer id.

I hope this tutorial is helpful. Happy Coding !!

Keep reading
All posts

Newsletter

New Magento tutorials, straight to your inbox

Code examples, performance tips, and fixes for common Magento issues. No spam.

Subscribe