Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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/

Function getCrossSellProducts() return collection with no all product data (eg. with no price and images). Crossell.php in above Rkt module should look eg. like this, then products have all data with prices, names and images:

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)) {
            $crossell_ids= $this->getProduct()->getCrossSellProductIds();
            $crossell_collection = Mage::getModel('catalog/product')
            ->getCollection()  
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->addAttributeToSort('position', 'ASC')
            ->addAttributeToFilter('entity_id', array('in' => $crossell_ids));   


            foreach ($crossell_collection as $item) {
                $items[] = $item;
            }
            $this->setData('items', $items);
        }

        return $items;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top