5
votes

I am going to get product's enabled/disabled status programmatically. Now, we got product instance by this code.

 <?php $_product = $_item->getProduct(); ?>

Also, using following code part, we can get product stock information.

 <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
      $product_is_stock = $StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId()); ?>

But, is there any solution to get status of product from $_product on Magento 2.1?

2

2 Answers

10
votes

After debugging on few hours, I got a good solution about this issue.

<?php $_product = $_item->getProduct(); 

 $product_status = $_product->getStatus();

In my example, I got deactivated product's status as value using $_product->getStatus().

<?php if (($product_is_stock == 0) || ($_product->getStatus() == 2)): ?>

To confirm this operation, I got deactivated products using these sql commands.

SELECT entity_id FROM `catalog_product_entity_int`
WHERE attribute_id = (
    SELECT attribute_id FROM `eav_attribute`
    WHERE `attribute_code` LIKE 'status'
) AND `catalog_product_entity_int`.value = 2

Finally, I confirmed deactivated products' status was just 2, not 0.

Enabled products' status was just 1.

Hope my example will help many developers.

1
votes

Looking in magento core, we can find that enabled = 1 and disabled = 2.

In Magento\Catalog\Model\Product\Attribute\Source\Status.php

 /**
 * Product Status values
 */
const STATUS_ENABLED = 1;

const STATUS_DISABLED = 2;

This is from the core code of magento 2.3.0

Use the core Luke!