How to Get Cross Sell Products Collection using Root Script in Magento 2



<?php

 

use Magento\Framework\AppInterface;

 

try

{

   require '../app/bootstrap.php';

}

catch (\Exception $e)

{

    echo 'Autoload error: ' . $e->getMessage();

    exit(1);

}

try

{

 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

 $objectManager = $bootstrap->getObjectManager();

 $appState = $objectManager->get('Magento\Framework\App\State');

 $appState->setAreaCode('frontend');

        $productId = 3; // Product Id

        $crossSellProduct = [];

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);

        $crossSell = $product->getCrossSellProducts();

        if (count($crossSell))

        {

                foreach ($crossSell as $productItem)

                {

                     $crossSellProduct[] = $productItem->getSku() . '<br />';  

                     print_r($crossSellProduct);

                }

        } 

}

catch(\Exception $e)

{

    print_r($e->getMessage());

}


Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2