2

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.

7

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.

  • nice post dude...!!! i have tested and also give this suggetion to other...!!! its work for me. thanks...!!! – denish vachhani Feb 17 '16 at 6:28
  • @denishvachhani Happy to hear it. :) – Rajeev K Tomy Feb 17 '16 at 6:51
  • @RajeevKTomy Great descriptive answer Rajeev. Its feels really good to see such helping members here. Good luck :) – Naeem Ul Wahhab Nov 16 '17 at 18:04
  • Anyone reading this should look at the catalog/product_list_crosssell block that already exists in Magento 1. There is no need to re-invent this. – BlueC Jun 17 at 14:44
4

Try this.

You do not want to call cross sell block in layout file. Instead put the below code in view.phtml file.

<?php if($_crossSellProducts = $_product->getCrossSellProducts()): ?>

            <div class="topproducts_containter">
            <div class="topsellerTitle">TOP <?php echo strtoupper(Mage::getModel('catalog/layer')->getCurrentCategory()->getName()); ?> PRODUCTS</div>

            <?php foreach ($_crossSellProducts as $_item): ?>
                <?php $_item = Mage::getModel('catalog/product')->load($_item->getId()); ?>

                <div class="col-sx-12 col-sm-2 items">
                    <div class="img">
                        <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>">
                            <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(100, 150); ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
                        </a>
                    </div>
                    <div class="name">
                        <a href="<?php echo $_item->getProductUrl() ?>">
                                <?php echo $this->htmlEscape($_item->getName()) ?>
                        </a>
                    </div>
                    <div class="price">
                               <?php echo $this->getPriceHtml($_item, true) ?>
                    </div>
               </div>
            <?php endforeach; ?>
            <div class="clearer"></div>
            </div>
        <?php endif; ?>

Reference : http://www.magentoworks.net/magento-display-cross-selling-products-on-product-detail-page/

  • Where i can add this code part in view.html, – Gem Aug 12 '16 at 12:17
  • This is a bad idea, performance wise, to do a load() of the product model within a loop. – BlueC Jun 17 at 14:42

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.