2

In product detail page the following code is written to display the cross-sell product which are set from the admin side..
<?php echo $this->getChildHtml('crosssell'); ?>
But it is not showing anything although there are cross sell products present in the admin side and the cross-sell products are instock.

I looked into its respective block i.e.

app/core/code/Mage/Catalog/Block/Product/List/Crosssell.php

and the function where the product collection is formed is as follows..

protected function _prepareData()
{
    $product = Mage::registry('product');
    /* @var $product Mage_Catalog_Model_Product */

    $this->_itemCollection = $product->getCrossSellProductCollection()
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->setPositionOrder()
        ->addStoreFilter();


    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

    $this->_itemCollection->load();

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

    return $this;
}  

I got nothing related to cross sell product while doing var_dump($product).

This is product detail layout file i.e. catalog.xml

<catalog_product_view translate="label">
<label>Catalog Product View (Any)</label>
<!-- Mage_Catalog -->
<reference name="root">
    <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="head">
    <action method="addJs"><script>varien/product.js</script></action>
    <action method="addJs"><script>varien/configurable.js</script></action>
    <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/></action>
    <action method="addItem"><type>js</type><name>calendar/calendar.js</name></action>
    <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name></action>
    <action method="addCss"><stylesheet>css/basket.css</stylesheet></action>
</reference>
<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
        <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>
        <block type="page/html_pager" name="product_review_list.toolbar" />
        <block type="core/template" name="product_review_list.count" template="review/product/view/count.phtml" />
        <block type="review/product_view_list" name="product.info.product_additional_data" as="product_reviews" template="review/product/view/list.phtml" />
        <block type="catalog/product_view" name="review_button" as="review_button" template="review/product/view/button.phtml"/>

        <block type="catalog/product_view" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>

        <block type="core/text_list" name="alert.urls" as="alert_urls" translate="label">
            <label>Alert Urls</label>
        </block>

        <action method="setTierPriceTemplate"><template>catalog/product/view/tierprices.phtml</template></action>

        <block type="catalog/product_list_upsell" name="product.info.upsell" as="upsell_products" template="catalog/product/list/upsell.phtml">
            <action method="setColumnCount"><columns>4</columns></action>
            <action method="setItemLimit"><type>upsell</type><limit>6</limit></action>
        </block>

        <block type="catalog/product_list_crosssell" name="product.info.crosssell" as="crosssell" template="catalog/product/list/crosssell.phtml"/>


        <block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/>

        <block type="catalog/product_view_additional" name="product.info.additional" as="product_additional_data" />
        <block type="catalog/product_view_description" name="product.description" as="description" template="catalog/product/view/description.phtml">
            <action method="addToParentGroup"><group>detailed_info</group></action>
        </block>
        <block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
            <action method="addToParentGroup"><group>detailed_info</group></action>
        </block>
        <block type="catalog/product_view" name="product.info.addto" as="addto" template="catalog/product/view/addto.phtml"/>
        <block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>

        <block type="core/text_list" name="product.info.extrahint" as="extrahint" translate="label">
            <label>Product View Extra Hint</label>
        </block>

        <block type="catalog/product_view" name="product.info.options.wrapper" as="product_options_wrapper" template="catalog/product/view/options/wrapper.phtml" translate="label">
            <label>Info Column Options Wrapper</label>
            <block type="core/template" name="options_js" template="catalog/product/view/options/js.phtml"/>
            <block type="catalog/product_view_options" name="product.info.options" as="product_options" template="catalog/product/view/options.phtml">
                <action method="addOptionRenderer"><type>text</type><block>catalog/product_view_options_type_text</block><template>catalog/product/view/options/type/text.phtml</template></action>
                <action method="addOptionRenderer"><type>file</type><block>catalog/product_view_options_type_file</block><template>catalog/product/view/options/type/file.phtml</template></action>
                <action method="addOptionRenderer"><type>select</type><block>catalog/product_view_options_type_select</block><template>catalog/product/view/options/type/select.phtml</template></action>
                <action method="addOptionRenderer"><type>date</type><block>catalog/product_view_options_type_date</block><template>catalog/product/view/options/type/date.phtml</template></action>
            </block>
                <block type="core/html_calendar" name="html_calendar" as="html_calendar" template="page/js/calendar.phtml"/>
        </block>
        <block type="catalog/product_view" name="product.info.options.wrapper.bottom" as="product_options_wrapper_bottom" template="catalog/product/view/options/wrapper/bottom.phtml" translate="label">
            <label>Bottom Block Options Wrapper</label>
            <action method="insert"><block>product.tierprices</block></action>
            <block type="catalog/product_view" name="product.clone_prices" as="prices" template="catalog/product/view/price_clone.phtml"/>
            <action method="append"><block>product.info.addtocart</block></action>
            <action method="append"><block>product.info.addto</block></action>
        </block>

        <block type="core/template_facade" name="product.info.container1" as="container1">
            <action method="setDataByKey"><key>alias_in_layout</key><value>container1</value></action>
            <action method="setDataByKeyFromRegistry"><key>options_container</key><key_in_registry>product</key_in_registry></action>
            <action method="append"><block>product.info.options.wrapper</block></action>
            <action method="append"><block>product.info.options.wrapper.bottom</block></action>
        </block>
        <block type="core/template_facade" name="product.info.container2" as="container2">
            <action method="setDataByKey"><key>alias_in_layout</key><value>container2</value></action>
            <action method="setDataByKeyFromRegistry"><key>options_container</key><key_in_registry>product</key_in_registry></action>
            <action method="append"><block>product.info.options.wrapper</block></action>
            <action method="append"><block>product.info.options.wrapper.bottom</block></action>
        </block>
        <action method="unsetCallChild"><child>container1</child><call>ifEquals</call><if>0</if><key>alias_in_layout</key><key>options_container</key></action>
        <action method="unsetCallChild"><child>container2</child><call>ifEquals</call><if>0</if><key>alias_in_layout</key><key>options_container</key></action>
    </block>
</reference>

So how to check if there is something missing by which the crossell products are not coming ?
Is there any other method of getting crossell products ?

  • As per as ,magento crossell product only show when your cart have items. – Amit Bera Jan 5 '16 at 8:31
  • @AmitBera can't it be shown in the product detail page ? – Kishore Patra Jan 5 '16 at 10:31
  • plz share layout code? – Amit Bera Jan 5 '16 at 10:33
  • @AmitBera I have added layout code for product detail page. Plz have a look. – Kishore Patra Jan 5 '16 at 10:45
1

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') ?> 

clear your cache and re-run product page.

Note: please check you have specify products cross sell for appropriate product.

I hope this will help you.

1

Have you set the cross sell products from admin? If not then in admin panel Catalog > Manage Products > edit one product and under "Product Information > Cross-sells" section asign products as cross-sell. After that do re-indexing and check.

  • yes it is set in the admin panel and the re-indexing is also done.. – Kishore Patra Jul 27 '16 at 7:48
0

if you want directly call cross-sell product then just paste below code in your view.phtml file and let me know its working or not

<?php echo $this->getLayout()->createBlock('catalog/product_list_crosssell')->setTemplate('catalog/product/list/crosssell.phtml')->toHtml() ?>

Just paste above code what ever place you want to display in view.phtml file

  • Does not work, I tried it and nothing renders even though cross sell products are set. – Black Feb 15 at 17:45
  • your magento version ?? – Murtuza Zabuawala Feb 18 at 3:21
  • Magento 1.9.3.0 – Black Feb 18 at 9:07
  • 1
    @Black try this code <?php echo $this->getLayout()->createBlock('catalog/product_list_crosssell')->setProduct($someProductObject)->setTemplate('catalog/product/list/crosssell.phtml')->toHtml() ?> – Murtuza Zabuawala Feb 18 at 9:46
  • Your code cant work, I just downloaded a fresh Magento 1.9.4 and there is no template catalog/product/list/crosssell.phtml – Black Feb 18 at 10:52
0

You have to add this block to your layout.xml:

<catalog_product_view translate="label">
    <reference name="content">
        <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
            <block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
     ...

Then you can render it in your template with this line:

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

There is also a faster way which works like this:

<?php 
    $_product = $this->getProduct();

    echo $this->getLayout()
    ->createBlock('checkout/cart_crosssell')
    ->setProduct($_product)
    ->setTemplate('checkout/cart/crosssell.phtml')
    ->toHtml();
?>

But this is dirty and fast.

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.