Here is what I have so far, to get IN STOCK items:

$productCollection = Mage::getModel('cataloginventory/stock_item')
                     ->getCollection()
                     ->addQtyFilter('>', 0);

echo $productCollection->getSelect();

foreach ( $productCollection as $currentProduct ) {

    $categoryIds = $currentProduct->getCategoryIds();

    print_r( $categoryIds );

}

It gets the products just fine, however I need to get each item's current category ID's, so in the above code $currentProduct->getCategoryIds() isn't working.

Does anyone has an idea how can I retrieve IN STOCK items along with it's category ID's?

Thank you.

有帮助吗?

解决方案

You can try this sample of code:

...
foreach ( $productCollection as $currentProduct ) {
    $categoryIds = Mage::getModel('catalog/product')->load($currentProduct->getId())
        ->getCategoryIds();

    print_r( $categoryIds );
}

其他提示

This appears to have already been answered here.

Please see the following urls on how to retrieve the products category ID:

Magento category ID from product ID

Get product's category ids and names

You can check if it is in stock with the following if statement:

<?php   
$__manStock = $_product->getStockItem()->getManageStock();
$__invAmt = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
    if ($__invAmt >= 1)
        {
    //Do Something here
    }
?>

And even:

<?php if ($_product->isAvailable()): ?>
Do Something here
<?php endif; ?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top