0

How can I make up sell to show only saleable products? if ($_product->isSaleable()) doesn't work in this case. Thank you

1

When you load Upsell Collection it doesnt give you complete product data. So you must load product Mage::getModel('catalog/product')->load(ID) and then check IsSaleable()

<?php 
     $object = Mage::getModel('catalog/product');
     foreach($upsell_product as $_upsell):

        //get detail of single upsell product using upsell product id
        $upsp = $object->load($_upsell->getId());

        if($upsp->isSaleable()){
             echo "Product is Saleable: ". $upsp->getName();
        }


     endforeach;
 ?>

is this enough for you?? or you need more details??

| improve this answer | |
  • Thank you. I believe it would be helpful if I didn't find extension iWeb Auto Related Products which has this function in it. – sidlo Feb 26 '15 at 1:03
0

Magento is filter product collection by salable using below code:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_productCollection);

if you get product collection by $product->getUpSellProductCollection().

Then you can filter collection by Salable by below code:

    Mage::getSingleton('cataloginventory/stock')
->addInStockFilterToCollection($product->getUpSellProductCollection())

If you default magento upsell.phtml then you can filter then collection using event /observer because of on class Mage_Catalog_Block_Product_List_Upsell define an event

and where you Updating collection with desired items

   Mage::dispatchEvent('catalog_product_upsell', array(
        'product'       => $product,
        'collection'    => $this->_itemCollection,
        'limit'         => $this->getItemLimit()
    ));

config.xml code:

<frontend>
    <events>
    <catalog_product_upsellf> <!-- identifier of the event we want to catch -->
        <observers>
          <catalog_product_upsellf_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento58700/observer</class> <!-- observers class alias -->
            <method>filtersaleable</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </catalog_product_upsellf_handler>
        </observers>
      </catalog_product_upsellf>
    </events>
</frontend>

Observer code is

<?php
class Stackexchange_Magento58700_Model_Observer
{
    public function filtersaleable(Varien_Event_Observer $observer)
    {
        $collection = $observer->getEvent()->getCollection();
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
    }
}
| improve this answer | |
  • Thank you. I believe it would be helpful if I didn't find extension iWeb Auto Related Products which has this function in it. – sidlo Feb 26 '15 at 1:04

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.