How To

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

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?

Steps 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());
}

Conclusion:

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!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

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.

Recent Posts

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

9 hours ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 day ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago