How to Get Product Status in Magento 2
Magento 2 storefront displays products that are enabled from the backend to the store visitors.
Apart from whether to display the product to visitors or not, there are other tasks where you need product status.
Before doing any product-related task, we have to check whether the product is enabled or not in the frontend. To do so, we need to get product status in Magento 2.
For instance, while sending data to a third-party API, it requires sending only enabled products’ data and exclude the disabled products.
Or, while generating different types of reports in the Magento 2 store, one may only want to get the data of the enabled products. To implement such a solution, one may need to get product status in Magento 2.
The product status is the current status of a particular product.
Magento 2 product status has two different states:
- Enabled: The product is visible or enabled in the frontend.
- Disabled: The product is not visible or disabled in the frontend.
In order to get product status programmatically in Magento 2, use the below method:
Method to Get Product Status in Magento 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Magento\Catalog\Model\Product; protected $product; public function __construct(Product $product) { $this->product = $product; } public function getIsProductEnable($productId) { $product = $this->product->load($productId); return $product->getStatus(); // 2 means Disabled , 1 means Enabled } |
That’s all!
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Unhide All Product Images on Product Page in Magento 2
How to Make a Partial Refund in Magento 2
Next