5

I have the sku in the page and I want to see if product is in stock or out of stock and to do that I have done this:

  $firstProductId = Mage::getSingleton('catalog/product')->getIdBySku('mysku');
                        $product = Mage::getModel('catalog/product');
                        $product->setId($firstProductId);
                        $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock();

I want to know if I can achieve this without loading the whole product any other way which is more efficient or I am on the right path?

6

You can use this code which give stock in faster way using product id

$model=Mage::getModel('cataloginventory/stock_item');
$model->getResource()->loadByProductId($model, $productId);
$data=$model->setOrigData();
echo "<pre>";
print_r($data->getData());

Out is an array

An exmaple:

Array
(
    [item_id] => 1868
    [product_id] => 889
    [stock_id] => 1
    [qty] => 5000.0000
    [min_qty] => 0.0000
    [use_config_min_qty] => 1
    [is_qty_decimal] => 0
    [backorders] => 0
    [use_config_backorders] => 1
    [min_sale_qty] => 1.0000
    [use_config_min_sale_qty] => 1
    [max_sale_qty] => 0.0000
    [use_config_max_sale_qty] => 1
    [is_in_stock] => 1
    [low_stock_date] => 
    [notify_stock_qty] => 
    [use_config_notify_stock_qty] => 1
    [manage_stock] => 0
    [use_config_manage_stock] => 1
    [stock_status_changed_auto] => 0
    [use_config_qty_increments] => 1
    [qty_increments] => 0.0000
    [use_config_enable_qty_inc] => 1
    [enable_qty_increments] => 0
    [is_decimal_divided] => 0
    [type_id] => simple
    [stock_status_changed_automatically] => 0
    [use_config_enable_qty_increments] => 1
)
3

Try this

$firstProductId = Mage::getSingleton('catalog/product')->getIdBySku('mysku');
$inStock = Mage::getModel('cataloginventory/stock_item')->getCollection()->addFieldToFilter('product_id', $firstProductId)->getFirstItem()

This should do the trick.

Although, not sure how efficient it would be but can confirm this is magento way.

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.