Can’t Disable Configurable Product Variations in Magento 2

configurable-productmagento2product

When I edit a configurable product in the Configuration section, it shows the list of all the configurations.

I want to disable one of the products listed there so when I choose Disable Product the status does change to "Disabled".

But after saving the product, status changes back to Enabled.

I have the error logs enabled but no error message appears there even when I have cleared the cache and reindexed.

enter image description here

Best Answer

Problem solved. After I've tried to change simple products statuses, from the configurable product grid, I've encountered the same problem. Seems to be a magento bug.

The story

Before saving the informations about configurable product, there are initialised the informations about simple products belonging to it. This is happening in module-configurable-product/Controller/Adminhtml/Product/Initialization/Helper/Plugin/UpdateConfigurations.php file, following function:

private function getConfigurationsFromProduct(\Magento\Catalog\Model\Product $configurableProduct)
    {
        $result = [];

        $configurableMatrix = $configurableProduct->hasData('configurable-matrix') ?
            $configurableProduct->getData('configurable-matrix') : [];
        foreach ($configurableMatrix as $item) {
            if (empty($item['was_changed'])) {
                continue;
            } else {
                unset($item['was_changed']);
            }

            if (!$item['newProduct']) {
                $result[$item['id']] = $this->mapData($item);

                if (isset($item['qty'])) {
                    $result[$item['id']]['quantity_and_stock_status']['qty'] = $item['qty'];
                }
            }
        }

        return $result;
    }

Here, the simple products are iterated and if a product contains was_changed, in the array informations, it is added in the $result array to be saved in the database.

The problem: Even if I change the status for a simple product, the was_changed attribute is not set for it, in the informations which will be received by the "save" controller. So, when the simple items are iterated in getConfigurationsFromProduct function, the continue statement will be hit every time, $result array being null.

The solution:

I've found a way to set was_changed attribute, in the toggleStatusProduct function from module-configurable-product/view/adminhtml/web/js/components/dynamic-rows-configurable.js, which is dispatched every time the simple product status is changed. The cleanest way is to rewrite that file and replace the function with:

toggleStatusProduct: function (rowIndex) {
            var tmpArray = this.getUnionInsertData(),
                status = parseInt(tmpArray[rowIndex].status, 10);

            if (status === 1) {
                tmpArray[rowIndex].status = 2;
            } else {
                tmpArray[rowIndex].status = 1;
            }
            tmpArray[rowIndex]['was_changed'] = true;
            this.unionInsertData(tmpArray);
        }

Clear the cache, run a deploy and it should work.

Good luck !

Related Topic