Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento 2.1.5 how to get product disabled status programmatically

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?

like image 612
Igor Revenko Avatar asked Jun 07 '17 09:06

Igor Revenko


2 Answers

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.

like image 148
Igor Revenko Avatar answered Nov 09 '22 21:11

Igor Revenko


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!

like image 38
Kilise Avatar answered Nov 09 '22 23:11

Kilise