0

How to display cross sell products on the products details page for the out of stock products only?

  • you want to show at product details page? – Amit Bera Sep 29 '17 at 10:26
  • balu,please put your question otherwise question is going to close – Amit Bera Sep 29 '17 at 10:46
  • yes @AmitBera , i want to show in pdp page – balu Sep 29 '17 at 11:09
0

Suppose, you want to add Crosssellsection at product detail page and want to show current product's Crosssellproducts then you need to create an extension.

On this extension, you will need to create a block which give you current product Crosssell products.

app/code/{VendorName}/{ModuleName}/Block/Crosssell.php

<?php
namespace {VendorName}\{ModuleName}\Block;

class Crosssell extends \Magento\Catalog\Block\Product\AbstractProduct
{
    /**
     * Crosssell item collection
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
     */
    protected $_itemCollection;

    /**
     * Prepare crosssell items data
     *
     * @return \Magento\Catalog\Block\Product\ProductList\Crosssell
     */
    protected function _prepareData()
    {
        $product = $this->_coreRegistry->registry('product');
        /* @var $product \Magento\Catalog\Model\Product */

        $this->_itemCollection = $product->getCrossSellProductCollection()->addAttributeToSelect(
            $this->_catalogConfig->getProductAttributes()
        )->setPositionOrder()->addStoreFilter();

        $this->_itemCollection->load();

        foreach ($this->_itemCollection as $product) {
            $product->setDoNotUseCategoryId(true);
        }

        return $this;
    }

    /**
     * Before rendering html process
     * Prepare items collection
     *
     * @return \Magento\Catalog\Block\Product\ProductList\Crosssell
     */
    protected function _beforeToHtml()
    {
        $this->_prepareData();
        return parent::_beforeToHtml();
    }

    /**
     * Retrieve crosssell items collection
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
     */
    public function getItems()
    {
        return $this->_itemCollection;
    }

    protected function _toHtml()
    {
            /* if product is out stock then render html  */
            if (!$this->_coreRegistry->registry('product')->isSaleable()) {
                return '';
            }
            return parent::_toHtml();
    }


}

Now, you need create layout file catalog_product_view.xml at

app/design/frontend/{Vendor}/{mytheme}/{VendorName}/{ModuleName}/layout

Now add below code at this layout files:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content.aside">
    <block class="{VendorName}\{ModuleName}\Block\Product\ProductList\Crosssell" name="product.info.upsell" template="Magento_Catalog::product/list/items.phtml">
        <arguments>
            <argument name="type" xsi:type="string">upsell</argument>
        </arguments>
        <block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="crosssell.product.addto" as="addto">
            <block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare"
                   name="upsell.product.addto.compare" as="compare"
                   template="Magento_Catalog::product/list/addto/compare.phtml"/>
        </block>
    </block>
 </referenceContainer>    
 </page>       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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