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
Post a Comment