How can I filter a product's availability using isSaleable attribute?

How can I filter a product's availability using isSaleable attribute?

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I filter a product's availability using isSaleable attribute?

Filtering Product Availability Using the isSaleable Attribute

In Magento, you can filter product availability using the isSaleable() function along with custom attributes like getProductAvailableDate(). This combination allows for dynamic content about product stock status and estimated availability.

Your Current Approach

You’ve attempted to use a ternary operator within a PHP block in your template file. The logic seems correct - if the product is saleable and the available date is empty ($empty == $_product->getProductAvailableDate()), it’s considered “in stock”. Otherwise, the product is temporarily out of stock.

However, there are a few issues with this approach:

  1. Security: Mixing PHP logic directly in your template can pose security risks if not handled correctly.
  2. Performance: Executing PHP code on every page load for each product might impact site performance.
  3. Readability: Ternary operators can make the code harder to read and maintain.

An Improved Strategy

Instead of handling this logic in the template, I recommend using Magento’s observer pattern and moving your availability check into a controller or model. This way, you keep your templates clean, improve security, and enhance performance.

Here’s an outline of how you can achieve this:

  1. Create a custom module: First, create a new Magento 2 module following the standard structure (e.g., app/code/Vendor/Module).

  2. Add an observer: In the module’s etc/ directory, add an event.xml file to observe the catalog_product_get_final_price event.

<event name="catalog_product_get_final_price">
   <observer name="check_product_availability" instance="Vendor\Module\Model\AvailabilityChecker" />
</event>
  1. Create the AvailabilityChecker model: In your module’s Model/ directory, create a new file called AvailabilityChecker.php. This model will handle the availability check logic.
class AvailabilityChecker implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();
        if ($_product->isSaleable()) {
            $availableDate = $_product->getData('your_date_attribute');
            if (empty($availableDate)) {
                // Product is in stock
                $_product->setHasOptions(true);  // Set the product as having options to trigger availability block
            } else {
                // Product is temporarily out of stock
                $_product->setIsSalable(false);
            }
        }

        return $this;
    }
}
  1. Update your template: In your template file, simply check for $_product->hasOptions() to display the “in stock” message.
<?php if ($_product->hasOptions()): ?>
   <p><?php echo $this->__('Availability: In stock.'); ?></p>
<?php else: ?>
   <p><?php echo $this->__('Availability: Temporarily Out of stock. Due to high demand for this product, it is not available until %1.', $_product->getProductAvailableDate()); ?></p>
<?php endif; ?>

Conclusion

By moving the availability check logic into a module and observer pattern, you achieve better security, performance, and maintainability.

Remember, Magento 2 follows the principle of keeping templates clean and doing heavy processing in models or controllers. This approach allows you to keep your codebase organized and easy to manage.

If you need more assistance with custom Magento development, feel free to explore our Magento Development Services at Laravel Company. We specialize in creating high-performing, secure, and scalable e-commerce solutions tailored to your business needs.

Magento 2 Catalog Performance Security

Our network