59

I'm working on the display of the stock availability on the (individual) product page of my Magento theme, and there's something I don't completely understand about this.

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

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

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

On the frontend, I've noticed that in Magento's base template isAvailable() is used to decide whether to display the product as "in stock" or "out of stock"; isSaleable() is used to decide something like whether to show an "Add to Cart" button.

On the backend, I've noticed that when the stock quantity becomes zero and backorders are not allowed, the stock availability of a product goes to "out of stock". When the stock quantity becomes zero and backorders are allowed, the stock availability of the product remains unchanged.

Question:
The properties "stock availability" and "stock quantity" are obviously linked with each other and the mentioned PHP methods. I would like to know:

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

  • what it is I appear not yet to know about their relation with these properties and Magento's behavior.

Thank you.

EDIT:
I've tried every relevant combination of stock quantity (-1,0,1), stock availability (in/out of), and backorders (on/off) for a product and this is the result:

St.Qu  BckOrd  St.Av  isSalable()  isSaleable()  isAvailable()
   -1       0      0            0             0              0
   -1       0      1          N/A           N/A            N/A
   -1       1      0            0             0              0
   -1       1      1            1             1              1
    0       0      0            0             0              0
    0       0      1          N/A           N/A            N/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

Just for the sake of completeness:

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

It is the stock availability switch in Magento that controls the return value of all of the PHP methods, but when backorders are off and stock quantity drops below 1, the stock availability will automatically be reset to 'out of stock' (hence the N/A rows).

Osama Rizwan
  • 601
  • 1
  • 4
  • 18
pancake
  • 1,863
  • 2
  • 20
  • 42

5 Answers5

1

If I'm not mistaken, the difference in these checks has to do with reservations of products for placed orders. When a customer adds products to an order, these products will still be in your stock, so they are still available. Though, they aren't saleable, because they have already been ordered by another customer.

So the semantic difference is:

  • saleable means: in stock and not ordered by another customer yet
  • available means: in stock but ordered by another customer, so available, but not saleable.

You could try to validate this by placing an order for a product. And doing the same check as you already did. This should cause a difference between the amount of available products and the amount of saleable products.

Edit: More info here:

https://blog.magestore.com/magento-multi-source-inventory-msi/#a3

qvotaxon
  • 363
  • 6
  • 14
0

isSeable() looks like it's checking if it reaches 0

isAvailable() looks like it's counting

Ismael Padilla
  • 4,051
  • 2
  • 17
  • 29
-1

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().

Guille Acosta
  • 1,581
  • 16
  • 25
elcash
  • 401
  • 2
  • 10
  • 1
    I understand the semantic difference between having something in stock and being able to sell something, but I still don't know what the semantic difference between isSaleable()/isSalable() and isAvailable(). Why use one over the other, when in every situation that I can think of they do the same thing (see edited question)? I say "that I can think of" because I would like to know if there are situations I haven't thought of that might make a distinction between these methods, because now I have the feeling one of them is useless, which I think is a little quick to judge. – pancake Jan 28 '12 at 11:46
  • In your edited question, you have written when backorders are off and stock quantity drops below 1, the stock availability will automatically be reset to 'out of stock' There is also a 'stock_is_changed_automatically' flag, which I use to show items that are (technically) not inventoried, and are explicitly in-stock, backorders off, and qty = 0 – elcash Jan 28 '12 at 17:26
  • That's useful information, thank you for that. I don't want to seem ungrateful, but I still don't know when to use isSaleable and when to use isAvailable, when they *seem* to do the same thing. I'm currently building a theme, so I'd like to get it right. – pancake Jan 28 '12 at 20:09
  • Right, the comment didn't answer your question but I thought may have been somehow related, and my edit to my answer is sort of incongruent with how you said Mage's base templates are setup. Maybe someone else will chime in. – elcash Jan 28 '12 at 22:08
-4

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 are available.

Sloan Thrasher
  • 4,806
  • 2
  • 19
  • 39
-6

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.

Vinay Sikarwar
  • 411
  • 6
  • 18