0

While creating new product in admin of magento 2. Product status is enable.

How can i change that status to disable as by default at a time of create new product?

enter image description here

0

After some hours of research, I found solution:

Create а plugin for the class that is responsible for the status switch output on the product page.

Module_Root/etc/adminhtml/di.xml

<type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General">
    <plugin name="setStatusForNewProducts"
            type="Your_Vendor\Your_Module\Plugin\Ui\DataProvider\Product\Form\Modifier\General"
    />
</type>

Module_Root/Plugin/Ui/DataProvider/Product/Form/Modifier/General.php

<?php

namespace Your_Vendor\Your_Module\Plugin\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Api\Data\ProductAttributeInterface;

class General
{
    /** @var \Magento\Catalog\Model\Locator\LocatorInterface */
    protected $locator;

    /**
     * @param \Magento\Catalog\Model\Locator\LocatorInterface $locator
     */
    public function __construct(
        \Magento\Catalog\Model\Locator\LocatorInterface $locator
    ) {
        $this->locator = $locator;
    }

    /**
     * @param \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General $subject
     * @param array $data
     * @return array
     */
    public function afterModifyData(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General $subject,
        array $data
    ) {
        $modelId = $this->locator->getProduct()->getId();
        if(!$modelId) {
            $data[$modelId][$subject::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS] = '0';
        }

        return $data;
    }
}

I hope it'll help. Write me if you have any issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.