0

This seems like it should be straightforward but I cannot find a clean solution:

Given an order item, what's the best way to figure out if the item has its own stock?

First I load the stock item:

$stockItem = Mage::getModel('cataloginventory/stock_item')
    ->loadByProduct($orderItem->getProductId());

Unfortunately, I get results for all items, and those from configurable products seem undistinguishable from simple products.

Then I thought of:

  • checking the product type. This is my least favorite solution because we work a lot with custom product types. I would have to define every single case and change it when new types are added
  • checking if item is a parent item. Possible but there is only getParentItem() so I'd have to go through all items, collect the parent item ids and remove those items.

For a generic solution it would be great to cover virtual products as well but for my use case it is not relevant. Am I missing something obvious?

1

If nobody comes up with a better solution, this is what works for me:

$hasStock = ! $orderItem->getIsVirtual() && count($orderItem->getChildrenItems()) === 0

And to get all real stock items for an order $order:

$stockItems = [];
foreach ($order->getAllVisibleItems() as $item) {
    /** @var Mage_Sales_Model_Order_Item $item */
    if ($item->getIsVirtual()) {
        continue;
    }
    if ($children = $item->getChildrenItems()) {
        foreach ($children as $childItem) {
            /** @var Mage_Sales_Model_Order_Item $childItem */
            if ($childItem->getIsVirtual()) {
                continue;
            }
            $stockItems[] = Mage::getModel('cataloginventory/stock_item')
                                ->loadByProduct($childItem->getProductId());
        }
    } else {
        $stockItems[] = Mage::getModel('cataloginventory/stock_item')
                            ->loadByProduct($item->getProductId());
    }
}

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.