0

I'm using Magento-2.2.6

I am showing cross sell product in product_detailed_info with success.

The only problem is that cross sell product are shown only if the "main" product is in cart.

How can I show cross sell product even if the "main" product isn't in cart ? Do I have to modify Crosssell.php or something like that ?

Thanks in advance !!!

2 Answers 2

0

We can get cross sell products of a current product by using the following function, regardless of product added in cart or not

$currentProduct = $block->getCurrentProduct();
$crossSellProducts = $currentProduct->getCrossSellProducts();
foreach ($crossSellProducts as $crossSellProduct) {
}

Hope it helps!

4
  • I'm sorry but it doesn't work
    – ortoDev
    Dec 10, 2018 at 8:44
  • Are you sure that it works in Magento-2.2.6 ?
    – ortoDev
    Dec 10, 2018 at 8:56
  • I implemented this in one my project. For me it is working. What error you are getting? Dec 10, 2018 at 8:59
  • Where I have to add this code ?
    – ortoDev
    Dec 10, 2018 at 9:00
0

Add the following into your template file,

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('VendorName\ModuleName\Block\Product\CrossSellItems');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$currentProduct = $myBlock->getCurrentProduct();

 if ($currentProduct = $myBlock->getCurrentProduct()) {
$crossSellProducts = $currentProduct->getCrossSellProducts();

  if (!empty($crossSellProducts)) {

    ?>

    <div class="category-right product-parts">
        <h2><span id="_CrossSellDisplay_TitleLabel">Cross Sell Products</span></h2>

        <div class="row">
            <?php
            $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
            foreach ($crossSellProducts as $crossSellProduct) {

                $product = $this->getLoadProduct($crossSellProduct->getId());


                $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();

                ?>
                <li class="col-sm-3 col-lg-3">
                    <div class="sub-cat-tile">
                        <a href="<?php echo $product->getProductUrl() ?>">
                            <img src="<?php echo $imageUrl ?>">
                        </a>
                    </div>
                    <h4 class="subcat-title">
                        <a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName() ?></a>
                    </h4>
                    <p>
                        <?php $product->getPrice() ?>
                    </p>

                </li>
            <?php } ?>
        </div>
    </div>

    <?php

} else
{ echo "<div class=\"category-right product-parts\">No Product Found!</div>"; }

}

?>

In your module create Block/Product/CrossSellItems.php class and add the following,

<?php
namespace VendorName\ModuleName\Block\Product;
class CrossSellItems extends \Magento\Framework\View\Element\Template {
protected $_registry; protected $_productloader;
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Model\ProductFactory $_productloader,
    array $data = []
)
{
    $this->_registry = $registry;
    $this->_productloader = $_productloader;
    parent::__construct($context, $data);
}

public function _prepareLayout()
{
    return parent::_prepareLayout();
}

public function getCurrentProduct()
{
    return $this->_registry->registry('current_product');
}

public function getLoadProduct($id)
{
    return $this->_productloader->create()->load($id);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.