Question

How can I check if any of the cross sell items are in stock in our Magento 1 store?

I want to load some php if rule, that only display a text line, if any of the cross sell items are in stock.

My current foreach code:

<?php foreach($this->getItems() as $_item): ?>
<?php $productqty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty(); ?>

<?php if ($productqty > 0):?>
Was it helpful?

Solution

You need to do the code like this one :

    $product = Mage::registry('product');

    $productcrosscol = $product->getCrossSellProductCollection()
              ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
                ->setPositionOrder()
                ->addStoreFilter();

    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($productcrosscol);

    $productcrosscol->load();
    $isruleshow = 0;
    foreach ($productcrosscol as $product) {
        $productqty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty(); 
        if ($productqty > 0) {
            $isruleshow = 1;
            break;
        }
    }

    if($isruleshow) :
       //PRINT YOUR RULE HERE
    endif;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top