2

I have created a event observer module that will trigger and remove any product that is out of stock from Upsell, but for some reason it is removing the products that are in stock. I have included the module code below and let me know if I am missing anything.

File name: kevz_upsellobserver.xml

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

File name: config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <kevz_upsellobserver>
            <version>0.0.1</version>
        </kevz_upsellobserver>
    </modules>
    <global>
        <models>
            <kevzupsellobserver>
                <class>upsellobserver_Model</class>
            </kevzupsellobserver>
        </models>
        <events>
            <catalog_product_upsell>
                <observers>
                    <kevzupsellobserver>
                        <type>singleton</type>
                        <class>Kevz_Upsellobserver_Model_Observer</class>
                        <method>filterUpsells</method>
                    </kevzupsellobserver>
                </observers>
            </catalog_product_upsell>
        </events>
    </global>
</config>

File name: Observer.php

<?php

class Kevz_Upsellobserver_Model_Observer {
        public function filterUpsells($observer) {
            $collection = $observer->getEvent()->getCollection();
            foreach ($collection as $item) {
                if (!$item->getIsSalable()) {
                    $collection->removeItemByKey($item->getId());
                }
            }
        }
}
?>
1

In Oberserver.php:

Change:

if (!$item->getIsSalable()) {

To

if (!$item->isSaleable()) {

This should correct your issue.

P.S. the reason that in stock items are currently being removed is due to the fact that !$item->getIsSalable() will return true for every product because that function is not defined anywhere within Magento.

P.S.S. If anything, the method could be getIsSaleable()

Edit: as per comment, to set the column count for product upsell:

In your theme's layout (xml) file app/design/frontend/yourtheme/default/layout/yourtheme.xml file:

<reference name="product.info.upsell">
        <action method="setColumnCount"><columns>4</columns></action>
        <action method="setItemLimit"><type>upsell</type><limit>4</limit></action>
</reference>
  • do you mean I need to change if (!$item->getIsSalable()) { to if (!$item->getIsSaleable()) { ? – Kevin S Mar 7 '14 at 6:59
  • I believe it would need to be if(!$item->isSaleable()), you can try both methods though. – Moose Mar 7 '14 at 7:00
  • it looks like if (!$item->getIsSalable()) { is working fine. P.S. Is there any way to pull static number of products in upsell unless there are less then 4 products assigned to a product as upsell? – Kevin S Mar 7 '14 at 7:03
  • Do you mean if(!$item->getIsSaleable()), you keep dropping the 'e'. I don't quite understand what you mean by pulling a static number of products in upsell, can you clarify? – Moose Mar 7 '14 at 7:04
  • right now in upsell section (upsell.phtml) random number of products are being shown. Sometimes 1, 2 and 4 on every page refresh, but I would like to display 4 products and show less then 4 products only if less then 4 products are assigned as upsell to a product – Kevin S Mar 7 '14 at 7:08

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.