Hello Magento Friends,
Today I am here with another solution, How to Get Cross Sell Products Collection using Root Script in Magento 2?
To boost sales, website owners can display Cross-sell, Up-sell, and related products to customers. Learn How to Setup Cross-Sell, Up-Sell, and Related products in Magento 2.
Cross-sell products are displayed with tags like “more choices”, “people also buy”, etc. Cross-sell products are displayed in the shopping cart before checkout. If you want to get the collection of cross-selling products in Magento 2, you can get it using the root script.
Let’s take a closer look at How to Get Cross Sell Products Collection using Root Script in Magento 2?
Step 1: Create a root script for cross-sell product collection as follows.
<?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()); }
Therefore, the above method can effortlessly help you get a collection of cross-selling products in Magento 2. If you cannot get the desired results, let me know through the comment section. Share the tutorial with your friends, and stay in touch with us for more Magento 2 solutions.
Happy Coding!
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…
Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…
Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…
View Comments
Great article on how to get cross-sell products collection using root script in Magento 2! Thank you for sharing your knowledge and experience with us.