What is the difference between isSaleable() and isAvailable()?

怎甘沉沦 提交于 2019-12-02 16:07:02
krishna

isSaleable() While working with Magento templates you definitely stumbled upon isSalable() method applied to product object. The method physically exists but it only checks if product has enabled status and salable check should not be skipped. Then the is_salable property of the product object is returned.

The obvious question is when this property is set. After product is loaded it is already set on model but it is not an attribute and is not a column in product flat table.

As usual, all the bizarre stuff in Magento is done by observers. Mage_Cataloginventory is observing catalog_product_load_after event and there it comes down to Mage_CatalogInventory_Model_Resource_Stock_Status::getProductStatus and the following query:

SELECT `cataloginventory_stock_status`.`product_id`, 
    `cataloginventory_stock_status`.`stock_status` 
FROM `cataloginventory_stock_status` 
WHERE product_id IN('241319') 
    AND stock_id=1 
    AND website_id=3;

It is clearly visible that the decision if product is salable or not is made during reindexing. And disregard stock_id which is sort of unfinished functionality which will also pop out later.

So we are ending up in a place no sane Magento developer will willingly go .. the indexer. Catalog inventory indexer in our case. After quick travel through the maze of Mage_CatalogInventory_Model_Indexer_Stock::_processEvent, Mage_Index_Model_Indexer_Abstract::reindexAll and Mage_CatalogInventory_Model_Resource_Indexer_Stock::reindexAll we discover that each product type has it’s own stock indexer which resides in app/code/core/Mage/CatalogInventory/Model/Resource/Indexer/Stock.

Each type has a _getStockStatusSelect method where an SQL query finally decides if the product is in salable or not. Even though the query may seem massive the logic behind is not complicated.

The big part of code here is again this rudimentary stuff. Seems like core developers made a fine attempt to allow having different stock levels for different websites but for some reason this functionality was never finished.

So for instance the check of simple products stock availability only contains of verifying that product is enabled and quantity is positive spiced with a stock management flags. Queries for configurable and grouped products varies a bit due to product type specifics.

elcash

I see those having semantic differences. An item that is not in stock can still be saleable if said item is set to allow backorders.

As far as I can tell, it looks like isAvailable checks a product type instance to see if the product type could be for sale if it is indeed available.

So, to venture a guess at when you might choose one over the other:

If you are checking an individual product to see if said product is actually ready for sale, you should use isSalable(), as it will call isAvailable().

To check if a product (whose type you don't know off hand) could be sold, and I suppose skipping the step of checking the product's type, you could call isAvailable() on the product.

isAvailable() checks if a product's type is salable.

isSalable() checks if a product is salable.

isSaleable() is an alias of isSalable().

Bhavna Malhi

As far as my concern, isSaleable() means you are checking the top most product that is ready for sale. While, isAvailable() means you are checking the product from the lists that is available.

isAvailable() is used to decide whether to display the product as in stock or out of stock, while isSaleable() is used to decide whether to show an Add to cart button or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!