How to use insertOnDuplicate query in Magento 2.

Use of insertOnDuplicate query using Magento 2,  You need to create a Connection object first to run the query using ResourceConnection class.

When you insert any new row into a table if the row causes a duplicate in the Primary key or UNIQUE index, throw an error in MySQL.

Base Definition of function:

/**
 * Inserts a table row with specified data.
 *
 * @param mixed $table The table to insert data into.
 * @param array $data Column-value pairs or array of column-value pairs.
 * @param array $fields update fields pairs or values
 * @return int The number of affected rows.
 */
public function insertOnDuplicate($table, array $data, array $fields = []);

Example,
Continue reading “How to use insertOnDuplicate query in Magento 2.”

Get Company id by company name programmatically in B2B Magento 2.

You can get company id by company name using B2B Magento 2.

You required Company name to fetch company id programmatically using Magento\Company\Api\CompanyRepositoryInterface interface. Continue reading “Get Company id by company name programmatically in B2B Magento 2.”

Delete Company by company id programmatically B2B Magento 2.

You can delete company data by company id in B2B version of Magento Commerce or Magento Cloud Edition.

For Delete, Company you required company id to delete the company. use Magento\Company\Api\CompanyRepositoryInterface interface and use deleteById() method from the interface. Continue reading “Delete Company by company id programmatically B2B Magento 2.”