0

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):?>
2
  • Can you please describe into detail, what you are looking for? Nov 18, 2019 at 13:49
  • @DhirenVasoya I updated the current code. I want to check if at least one of the $_item got a stock value above 0. And if so then display a line of text.
    – JGeer
    Nov 18, 2019 at 14:09

1 Answer 1

1

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;
1
  • Thanks a lot, works perfect!
    – JGeer
    Nov 23, 2019 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.