CrossSell Products display on the Cart page of the store on the eCommerce Website.
Cross-sell Products in Magento 2 are displayed on the cart page with labels like, “More Choices“ and display a list of available products by matching rules on the cart page in Magento Commerce.
Native Magento displays 9 Cross-sell items on the cart page and if you want to display more than the default limit you can override the PHP file with How to change CrossSell product limit in Magento 2?
You can Get the cross-sell items by Product id or SKU via fetch the Product repository collection in Magento 2.
<?php
namespace Jesadiya\Crosssell\Model;
use Exception;
use Psr\Log\LoggerInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
class CrosssellList
{
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
LoggerInterface $logger,
ProductRepositoryInterface $productRepository
) {
$this->logger = $logger;
$this->productRepository = $productRepository;
}
/**
* get cross sell products
*
* @return array
*/
public function getCrosssellProductsList()
{
$productId = 5;
$crossSellProduct = [];
try {
$product = $this->productRepositoryInterface->getById($productId);
$crossSell = $product->getCrossSellProducts();
if (count($crossSell)) {
foreach ($crossSell as $productItem) {
$crossSellProduct[] = $productItem->getId();
}
}
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
}
return $crossSellProduct;
}
}
You can load the Cross-sell items by calling getCrossSellProducts() on product objects.
Get Related Products Collection in Magento 2
Get Upsell Products collection in Magento 2
Output:
Cross-sell products array of ids.
If the product is not assigned, the Result will be an empty array.
Retrieve Cross-sell Items is the easy way in Magento 2 by Programmatic Way.