magento2: Can not change product status which product created with productRepository

Preconditions

  • Any supported Magento version

Steps to reproduce

$product = $objectManager->create(Product::class); $product->setTypeId(Type::TYPE_SIMPLE) ->setWebsiteIds([1]) ->setName(NAME) ->setSku(SKU) ->setPrice(10) ->setVisibility(Visibility::VISIBILITY_BOTH) ->setStatus(Status::STATUS_DISABLED) ->setCategoryIds(array(1,5,7)); try { $product = $productRepository->save($product); } catch (\Exception $e) { echo $e->getMessage(); } Product created successfully. In admin set ‘Enable Product’ true. But at product grid in admin, status seems ‘Disabled’.

Expected result

Product have to be online in website

Actual result

‘We can’t find products matching the selection.’ And product status seen Disabled in admin grid.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Why is it still not fixed. It was reported on 16 July 2016. It is really serious issue @magento-engcom-team

@denisrpriebe There is still no solution, so I did the following work-around. It is not pretty but it works.

public function __construct(
\Magento\Framework\App\ResourceConnection $resourceConnection
){
    $this->resourceConnection = $resourceConnection;
}
    /**
     * Changes product status
     *
     * This function changes the status of given product.
     *
     * @param \Magento\Catalog\Api\Data\ProductInterface $product
     * @param int $status
     */
    protected function changeStatus(\Magento\Catalog\Api\Data\ProductInterface $product, $status)
    {
        $resourceConnection = $this->resourceConnection;
        $connection = $resourceConnection->getConnection();

        $tableEavAttribute = $resourceConnection->getTableName('eav_attribute');
        $tableProductEntityInt = $resourceConnection->getTableName('catalog_product_entity_int');
        $tableEavEntityType = $resourceConnection->getTableName('eav_entity_type');

        $entityTypeIdProduct = $connection->fetchOne("SELECT `entity_type_id` FROM `{$tableEavEntityType}` WHERE `entity_type_code` = 'catalog_product' LIMIT 0,1;");
        $attributeIdStatus = $connection->fetchOne("SELECT `attribute_id` FROM `{$tableEavAttribute}` WHERE `attribute_code` = 'status' AND `entity_type_id` = {$entityTypeIdProduct}");
        $connection->update($tableProductEntityInt, ['value' => $status], "`attribute_id` = {$attributeIdStatus} AND `row_id` = {$product->getRowId()}");
    }

The same issue occurs when you insert a product with API.

I am having the same problem on 2.1.3

Easier solution would be to login the user… then you can use the the normal way to change status

    /**
     * Dirty Fix to login customer due to Magento 2 Bug
     * https://github.com/magento/magento2/issues/5664
     *
     * @return $this
     */
    public function loginAdminUser()
    {
        $model = $this->userFactory->create()->loadByUsername('USERNAME');

        $this->state->emulateAreaCode('adminhtml', function ($model) {
            $session = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Backend\Model\Auth\Session');
            $session->start();
            $session->setUser($model);
            $session->processLogin();
        }, array($model));

        return $this;
    }

An addition; If I change status via products grid (check checkbox of item, select ‘Change Status’->‘Enabled’ action) it is working fine