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


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


Hello magento friends todays in this blog post im share with you to How to Get Cross Sell Products Collection using Root Script in Magento 2.To get the cross-sell products collection using a root script in Magento 2, you can follow these steps:


➤ Create a Root Script File:

Create a PHP file in your Magento 2 root directory (where your index.php file is located). You can name it something like cross_sell_collection.php.


➤ Initialize Magento Environment:

In your root script file, include the following code at the beginning to initialize the Magento environment:


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


➤ Load Product and Retrieve Cross-Sell Collection:

After initializing the Magento environment, you can load a product and retrieve its cross-sell products collection using the following code:


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

➤ Run the Root Script:

Now, you can save the root script file and run it using the command line from your Magento 2 root directory:

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

Make sure to replace PRODUCT_ID with the actual ID of the product for which you want to retrieve cross-sell products.

Please note that directly using the Object Manager ($objectManager) is not the recommended way of working with Magento 2. It's better to use dependency injection and follow best practices for Magento 2 development. However, for a quick script like this, using the Object Manager might be acceptable.


Also, keep in mind that this script assumes you have basic knowledge of Magento 2 structure and development practices. Always test scripts in a development environment before using them in a production environment.



Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post