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());
                }
            }
        }
}
?>
Kevin S
  • 169
  • 2
  • 7
  • 25

1 Answers1

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>
Moose
  • 7,495
  • 7
  • 48
  • 91
  • do you mean I need to change if (!$item->getIsSalable()) { to if (!$item->getIsSaleable()) { ? – Kevin S Mar 07 '14 at 06:59
  • I believe it would need to be if(!$item->isSaleable()), you can try both methods though. – Moose Mar 07 '14 at 07: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 07 '14 at 07: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 07 '14 at 07: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 07 '14 at 07:08
  • I've edited my answer to show where you can set the column count. Magento will always show the maximum amount there, unless a product does not have this amount, in which case it will show that amount of upsells. – Moose Mar 07 '14 at 07:14
  • I have already placed that code but it still shows random number of products that us below 4. – Kevin S Mar 07 '14 at 07:19
  • Are you using a custom theme? – Moose Mar 07 '14 at 07:24
  • If you've got that code there it should show all four unless your theme controller is setting a random number. Are you sure the products you're seeing with less than 4 only have fewer than 4? Maybe that's why it's displaying that number. – Moose Mar 07 '14 at 07:27
  • In first refresh it showed 4 products and again after refreshing it showed 3 products. Its showing random. I have pastes catalog.xml at link and upsell.phtml at link – Kevin S Mar 07 '14 at 07:31
  • I'm not quite certain what the problem is in this instance, the code that you posted should work correctly unless the collection itself is being assembled incorrectly. – Moose Mar 07 '14 at 11:12
  • worked perfectly. Though I had to make few changes to the code. – Kevin S Mar 30 '14 at 11:28