Programmatically assign related products magento 2

This program explains how to programmatically assign related products magento 2.


assigned-related-products-magento-2

Notes:-

1) The following code adds the data directly to the database. I have assigned the related products as part of the main products import. After the imports, I have noted some of the data (additional images) is missing. Then I wrote this script for direct inserting the data to the database. That resolves the issue.

Please also refer the tutorials that explain how to assign related products in Magento 1

Initialise object manager

use \Magento\Framework\App\Bootstrap;

include('../app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$app_state = $objectManager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');

$url = Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getStoreId();

Assign related products

The following code inserts the related products Id directly to the database. The code reads the data from the CSV. As the first step, it retrieves the new products ids from the old ids.

function relatedProducts($relatedDatas, $objectManager, $storeId, $newProductIds) {

    foreach($relatedDatas as $key => $exportData ) {

        if (isset($newProductIds[$key])) {

            $newProdId = $newProductIds[$key];
            echo 'Product Id Match : ' . $key . ' - ' . $newProdId . PHP_EOL;
            $relatedProductOldIds = explode(':', trim($exportData));

            foreach ($relatedProductOldIds as $relatedProductOldId) {

                if (isset($newProductIds[$relatedProductOldId])) {

                    $newRelatedProdId = $newProductIds[$relatedProductOldId];
                    echo 'Product Id match : ' . $relatedProductOldId . ' : ' . $newRelatedProdId . PHP_EOL;
                    $productRelatedExist = $objectManager->get('Magento\Catalog\Model\Product')->load($newRelatedProdId);

                    if(!is_null($productRelatedExist)) {

                        echo 'Related Products Id :' . $newRelatedProdId . ' Name : ' . $productRelatedExist->getName() . PHP_EOL;

                        try {
                            $q = $objectManager->get('\Magento\Framework\App\ResourceConnection');
                            $q->getConnection('core_write')->query("insert into catalog_product_link (linked_product_id, product_id,link_type_id) values (".$newRelatedProdId.",".$newProdId.",1)");

                        }
                        catch(Exception $e)
                        {
                            echo 'Duplicate Key'.PHP_EOL;
                        }

                    }
                }

                else {
                        echo 'Product Id not existing : ' . $relatedProductOldId . PHP_EOL;
                    }


            }

            echo '-----------------------------------------------------'.PHP_EOL;


        } else {
            echo 'Product Not Match : ' . $key . PHP_EOL;
        }

    }


}

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*