Magento2 – Add Cross-Sell Block on Product Page

cross-sellsproduct-pageproduct-view

I'm trying to get cross-sell product on view.phtml. It is possible?

I added

<?php echo $this->getChildHtml('crosssell') ?> 

into view.phtml and

<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>

into catalog.xml
but I don't see any result in product page.

Cross sell products are correctly shown only in cart page.

EDIT: If I add a product with cross sell product to che cart, I see them on ALL product pages.

Best Answer

Try this module:

File : app\etc\modules\Rkt_CrossSell.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Rkt_CrossSell>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Rkt_CrossSell>
    </modules>
</config>

File : app\code\community\Rkt\CrossSell\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Rkt_CrossSell>
          <version>1.0.0</version>
        </Rkt_CrossSell>
    </modules>
    <global>
        <helpers>
            <rkt_crossell>
                <class>Rkt_CrossSell_Helper</class>
            </rkt_crossell>
        </helpers>
        <blocks>
            <rkt_crossell>
                <class>Rkt_CrossSell_Block</class>
            </rkt_crossell>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <rkt_crossell>
                    <file>rkt_crossell.xml</file>
                </rkt_crossell>
            </updates>
        </layout>
    </frontend>
</config>

File : app\code\community\Rkt\CrossSell\Block\Catalog\Product\View\Crosssell.php

<?php
class Rkt_CrossSell_Block_Catalog_Product_View_Crosssell extends Mage_Checkout_Block_Cart_Crosssell
{

    /**
     * Get crosssell items
     *
     * @return array
     */
    public function getItems()
    {
        $items = $this->getData('items');
        if (is_null($items)) {
            $items = $this->getProduct()->getCrossSellProducts();
            $this->setData('items', $items);
        }
        return $items;
    }
}

File : app\code\community\Rkt\CrossSell\Helper\Data.php

<?php
class Rkt_CrossSell_Helper_Data extends Mage_Core_Helper_Abstract
{

}

File : app\design\frontend\base\default\layout\rkt_crossell.xml

<?xml version="1.0"?>
<layout>
    <catalog_product_view>
        <reference name="product.info">
            <block type="rkt_crossell/catalog_product_view_crosssell" name="product.view.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml" />
        </reference>
    </catalog_product_view>
</layout>

Now in your view.phtml add this code in the appropriate position

<?php echo $this->getChildHtml('crosssell') ?> 

Now clear your cache. Double check evey file name and file paths are correct. Then you are good to go.

Related Topic