4

Modifying product page I have encountered some methods touching stock and product availibility:

$_product->isSaleable());
$_product->isSalable());
$_product->isAvailable());
$_product->isInStock());

Can someone compare their functionality and add some hints when any of them should be used? If there are any method more which can be misunderstood like them, please add them in comments.

5

isSaleable() is just an alias of isSalable() :

/**
 * Alias for isSalable()
 *
 * @return bool
 */
public function isSaleable()
{
    return $this->isSalable();
}

The isSalable() method looks complex but it simply checks for the availability of the product via the isAvailable() method:

/**
 * Check is product available for sale
 *
 * @return bool
 */
public function isSalable()
{
    if ($this->hasData('salable') && !$this->_catalogProduct->getSkipSaleableCheck()) {
        return $this->getData('salable');
    }
    $this->_eventManager->dispatch('catalog_product_is_salable_before', ['product' => $this]);

    $salable = $this->isAvailable();

    $object = new \Magento\Framework\DataObject(['product' => $this, 'is_salable' => $salable]);
    $this->_eventManager->dispatch(
        'catalog_product_is_salable_after',
        ['product' => $this, 'salable' => $object]
    );
    $this->setData('salable', $object->getIsSalable());
    return $object->getIsSalable();
}

The isAvailable() method checks whether the product type or stock allows to purchase the product:

public function isAvailable()
{
    return $this->getTypeInstance()->isSalable($this) || $this->_catalogProduct->getSkipSaleableCheck();
}

If you check isSalable method from the type class:

public function isSalable($product)
{
    $salable = $product->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
    if ($salable && $product->hasData('is_salable')) {
        $salable = $product->getData('is_salable');
    }

    return (bool)(int)$salable;
}

Finally the isInStock() method only checks is the status is enabled (which is weird right ?) :

public function isInStock()
{
    return $this->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
}
  • Ho wcan I check isIn stock() for upsell products – Jaisa Feb 16 '18 at 12:58
3

These all are methods which checked the product availability with different parameters .

  1. isInStock() - Its just check the product is in stock or not. And for this its check the product attribute value of Stock Status, if it is set as In Stock then it will return true.

  2. isAvailable() - Its check the product availability according to product status, stock status, visibility and website.

  3. isSaleable() or isSalable() - It is similar as isAvailable, but it has dispatch event "catalog_product_is_salable_after" before returning the value. And in this event developer can write their own logic to set the availability of product. And on the basis of that, product will be available on website.

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.