0

I am using the latest magento version which is 1.9.0.1.

I have followed the following instructions to increase the limit of cross-sell products but something seems to be wrong here. Any help would be highly appreciated. You can find the original post over here as well How to Increase the Number of Crosssell Items in Cart?

But my code is written below here:

Crosssell.php in the directory app/code/local/TestingMod/Checkout/Block/Cart:

class TestingMod_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/TestingMod/Checkout/etc in config.xml:

<?xml version="1.0"?>
 <config>
<modules>
    <TestingMod_Checkout>
        <version>1.0</version>
    </TestingMod_Checkout>
</modules>
<global>
    <blocks>
        <checkout>
                <rewrite>
                    <cart_crosssell>
                        TestingMod_Checkout_Block_Cart_Crosssell
                    </cart_crosssell>
                </rewrite>
        </checkout>
        <testingmod_checkout>
            <class>TestingMod_Checkout_Block</class>
        </testingmod_checkout>
    </blocks>
</global>

app/etc/modules in TestingMod_Checkout.xml:

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

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

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

Number of cross-sell products is capped to 4 and since core team did not provide us with method to change the $_maxItemCount we will implement our own :)

class Mage_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
    /**
     * Items quantity will be capped to this value
     *
     * @var int
     */
    protected $_maxItemCount = 4;

So we create a Crosssell.php block and extend from Mage_Checkout_Block_Cart_Crosssell. Our block will be created in app/code/local/TestingMod/Checkout/Block/Cart/. You could, of course, implement another way to fetch the limit $value (Mage::getStoreConfig() for example) but I wanted to keep it simple.

class TestingMod_Checkout_Block_Cart_Crosssell extends Mage_Checkout_Block_Cart_Crosssell
{
    public function setMaxItems($value = 4)
    {
        $this->_maxItemCount = $value;
    }
}

We can now replace the default cross-sell block with our own and call setMaxItems() method via our layout update file. So create app/design/frontend/base/default/layout/testingmod/checkout.xml

NOTE: You will probably want to create a layout update file in your_package/your_theme instead of base/default

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_cart_index>
        <reference name="content">
            <reference name="checkout.cart">
                <block type="testingmod_checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml">
                    <action method="setMaxItems"><value>10</value></action>
                </block>
            </reference>
        </reference>
    </checkout_cart_index>
</layout>

Our module configuration file app\code\local\TestingMod\Checkout\etc\config.xml should look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <TestingMod_Checkout>
            <version>1.0.0.0</version>
        </TestingMod_Checkout>
    </modules>

    <global>
        <blocks>
            <testingmod_checkout>
                <class>TestingMod_Checkout_Block</class>
            </testingmod_checkout>
        </blocks>
    </global>

    <frontend>
        <layout>
            <updates>
                <testingmod_checkout>
                    <file>testingmod/checkout.xml</file>
                </testingmod_checkout>
            </updates>
        </layout>
    </frontend>
</config>

And finally we register our module app/etc/modules/TestingMod_Checkout.xml

<?xml version="1.0"?>
<config>
    <modules>
        <TestingMod_Checkout>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout/>
            </depends>
        </TestingMod_Checkout>
    </modules>
</config>

And that's it! Nice and simple, also no class rewrites. Hope it helps.

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.