Magento Cart – How to Increase Number of Crosssell Items

cartcross-sells

I want to increase the number of cross sell items appearing in cart. The max item count is 4 in defined at app/code/core/Mage/Checkout/Block/Cart/Crossell.php

class Mage_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
    protected $_maxItemCount = 4;

How can I increase this limit from admin end or set the limit in checkout.xml file.

Please Guide.

Thanks.

Best Answer

You're going to need to implement your own module to do so

app/code/local/MyCompany/Checkout/Block/Cart in Crosssell.php

 class MyCompany_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{

//     /**
//      * Items quantity will be capped to this value
//      *
//      * @var int
//      */
//     protected $_maxItemCount = 100;

//     /**
//      * Get crosssell items
//      *
//      * @return array
//      */

public function getItemCount()
{
         return count($this->getItems());
}

public function getItems()
{
    $items = $this->getData(\'items\');
    if (is_null($items)) {
        $items = array();
        $ninProductIds = $this->_getCartProductIds();
        if ($ninProductIds) {
            $lastAdded = (int) $this->_getLastAddedProductId();
            if ($lastAdded) {
                $collection = $this->_getCollection()
                    ->addProductFilter($lastAdded);
                if (!empty($ninProductIds)) {
                    $collection->addExcludeProductFilter($ninProductIds);
                }
                $collection->setPositionOrder()->load();

                foreach ($collection as $item) {
                    $ninProductIds[] = $item->getId();
                    $items[] = $item;
                }
            }

            if (count($items) < 100) {
                $filterProductIds = array_merge($this->_getCartProductIds(), $this->_getCartProductIdsRel());
                $collection = $this->_getCollection()
                    ->addProductFilter($filterProductIds)
                    ->addExcludeProductFilter($ninProductIds)
                    ->setPageSize(100-count($items))
                    ->setGroupBy()
                    ->setPositionOrder()
                    ->load();
                foreach ($collection as $item) {
                    $items[] = $item;
                }
            }

        }

        $this->setData(\'items\', $items);
    }
    return $items;
}
}

app/code/local/MyCompany/Checkout/etc in config.xml:

 <?xml version="1.0"?>
 <config>
<modules>
    <MyCompany_Checkout>
        <version>1.0</version>
    </MyCompany_Checkout>
</modules>
<global>
    <blocks>
        <checkout>
                <rewrite>
                    <cart_crosssell>
                        MyCompany_Checkout_Block_Cart_Crosssell
                    </cart_crosssell>
                </rewrite>
        </checkout>
        <mycompany_checkout>
            <class>MyCompany_Checkout_Block</class>
        </mycompany_checkout>
    </blocks>
</global>

app/etc/modules in MyCompany_Checkout.xml:

<?xml version="1.0"?>
<config>
<modules>
    <MyCompany_Checkout>
        <active>true</active>
        <codePool>local</codePool>
    </MyCompany_Checkout>
</modules>

app/design/frontend/default/mytheme/layout/ in checkout.xml:

 <?xml version="1.0"?>
<reference name="content">
    <block type="mycompany_checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
</reference> 

EDIT : To make the value selectable in the backend, you'll want to create:

app/code/local/MyCompany/Checkout/etc in system.xml:

<config>
<tabs>
    <mycompany_tab translate="label" module="mycompany">
        <label>MyCompany X-Sell Settings</label>
        <sort_order>100</sort_order>
    </mycompany_tab>
</tabs>
<sections>
    <mycompany_section translate="label" module="mycompany">
        <label>Max QTY For Upsell Block</label>
        <tab>mycompany_tab</tab>
        <frontend_type>How many</frontend_type>
        <sort_order>0</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>      
        <groups>
            <mycompany_group translate="label">
                <label>Settings</label>
                <frontend_type>text</frontend_type>
                <sort_order>1</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>
                <fields>
                    <mycompany_field>
                        <label>Maximum QTY To Display</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>This is the maximum number of upsell products to display</comment>                    
                    </mycompany_field>
                </fields>                      
            </mycompany_group>
        </groups>              
    </mycompany_section>
</sections>    

Add this to your existing app/code/local/MyCompany/Checkout/etc/ in config.xml:

....
<adminhtml>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <mycompany_section>
                                        <title>MyCompany - All</title>
                                    </mycompany_section>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</adminhtml>

And finally, you'd want to replace app/code/local/MyCompany/Checkout/Block/Cart in Crossell.php:

protected $_maxItemCount = 100;

With

protected $_maxItemCount = Mage::getStoreConfig('mycompany_section/mycompany_group/mycompany_field');

If you'd like to know why the config section works like it works, the best article for reference in my opinion is:

Alan Storm | Custom Magento System Configuration

Good luck!

Related Topic