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

I am working on the display of stock availability on a separate product page of my Magento theme, and there is something I don’t quite understand about it.

I see two methods used in templates to check if a product is available for sale:

Mage_Catalog_Model_Product::isAvailable() Mage_Catalog_Model_Product::isSaleable() 

My own conclusions:
I see that isSalable() (which in turn isSaleable() ) is called) calls isAvailable() , but also dispatches two events ( catalog_product_is_salable_before and catalog_product_is_salable_after ).

At the front, I noticed that in the Magento database template, isAvailable() used to decide whether to show the product as “out of stock” or “out of stock”; isSaleable() used to decide something like whether to show the "Add to Cart" button.

On the backend, I noticed that when the amount of stock becomes zero, and backups are not allowed, the availability of the stock of the product goes “in stock”. When the quantity of stocks becomes equal to zero, and allowable stocks are allowed, the availability of stock and product remains unchanged.

Question:
The "stock availability" and "stock quantity" properties are obviously related to each other and the PHP methods mentioned. I'd like to know:

  • what is the semantic difference between the PHP methods isAvailable() and isSaleable() is there and why I will use one over the other,

  • What seems to me, I still do not know about their connection with these properties and the behavior of Magento.

Thank.

EDIT:
I tried every appropriate combination of stock quantity (-1,0,1), stock availability (on / off) and backups (on / off) for the product, and this is the result:

 St.Qu BckOrd St.Av isSalable () isSaleable () isAvailable ()
    -1 0 0 0 0 0
    -1 0 1 N / AN / AN / A
    -1 1 0 0 0 0
    -1 1 1 1 1 1
     0 0 0 0 0 0
     0 0 1 N / AN / AN / A
     0 1 0 0 0 0
     0 1 1 1 1 1
     1 0 0 0 0 0
     1 0 1 1 1 1
     1 1 0 0 0 0
     1 1 1 1 1 1

For completeness only:

 St.Av 0 = out of stock
 St.Av 1 = in stock
 BckOrd 0 = no backorders allowed
 BckOrd 1 = backorders are allowed

This is the Magento stock availability switch that controls the return value of all PHP methods, but when the backups are disabled and the number of stocks falls below 1, the stock availability will automatically reset to “out of stock” (hence the N/A ).

+47
product magento
Jan 27 '12 at 19:33
source share
4 answers

isSaleable () When working with Magento templates, you definitely came across the isSalable () method applied to the product object. This method physically exists, but it checks to see if the product has allowed status, and a sale 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 loading the product, it is already installed on the model, but it is not an attribute and is not a column in the product’s flat table.

As usual, all bizarre things in Magento are done by observers. Mage_Cataloginventory observes the 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 seen that the decision, if the product is sold or not, is made during reindexing. And ignore share_id, which is some kind of incomplete functionality that will also come out later.

So, we find ourselves in a place where no sane Magento developer would willingly go ... a pointer. Catalog inventory indexer in our case. After a quick passage through the labyrinth Mage_CatalogInventory_Model_Indexer_Stock :: _ processEvent, Mage_Index_Model_Indexer_Abstract :: reindexAll and Mage_CatalogInventory_Model_Resource_Indexer_Stock :: reindex we have our own index / model, we have our own index / Model, reindex, which we find Indexer / Photo.

Each type has a _getStockStatusSelect method, where the SQL query finally decides whether the product is sold or not. Although the query may seem massive, the logic is not complicated.

Most of the code here is again rudimentary material. It seems that the main developers made an excellent attempt to allow different levels of inventory for different sites, but for some reason this functionality never ended.

So, for example, checking the availability of simple products is available only to verify that the product is included, and the quantity is positively flavored with inventory control flags. Requests for custom and grouped products are slightly different due to the nature of the product type.

+2
Nov 15 '16 at 14:01
source share

I see those who have semantic differences. An item that is not in stock can still be sold if the item is configured to allow backups.

As far as I can tell, it seems that isAvailable checking the instance of the product type to see if the product type can be sold if it is really available.

So, take a chance to assume when you can choose one by one:

If you are testing a single product to make sure that the product is indeed ready for sale, you should use isSalable() , as it calls isAvailable() .

To check if a product (whose type you do not know) can be sold, I suggested skipping the step of checking the type of product, you could call isAvailable() on the product.

isAvailable() checks if a product type is available.

isSalable() checks if the item is available.

isSaleable() is an alias of isSalable() .

-one
Jan 28 2018-12-12T00:
source share

As far as I understand, isSaleable() means that you are checking the topmost product that is ready for sale. So far, isAvailable() means that you are checking the product from the available lists.

-3
Sep 19 '16 at 12:15
source share

isAvailable () is used to determine whether the product should be displayed both in stock and in stock, and isSaleable () is used to decide whether to add Add to the cart button or not.

-four
May 08 '15 at 10:45
source share



All Articles