InsideDarkWeb.com

Set custom default qty when creating new product Magento 2.3

I need Quantity field to be filled with custom value when creating new product in admin. I have already tried modificators (haven’t figured out how to set data for new product with modifyData($data) and
this answer seems to be not working anymore.. Is there some way to do so?

Making an observer on event catalog_product_new_action and $observer->getEvent()->getProduct()->setQty(%number%) doesnt work either.

Cheers.

Magento Asked by Vadim Shmyrov on September 9, 2020

1 Answers

One Answer

You can create your own modifier pool in your custom extension to achieve this. Following files would do the work.

etc/adminhtml/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="MagentoCatalogUiDataProviderProductFormModifierPool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="defaultQtyModifier" xsi:type="array">
                    <item name="class" xsi:type="string">VendorModuleUiDataProviderProductFormModifierDefaultQtyModifier</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

Ui/DataProvider/Product/Form/Modifier/DefaultQtyModifier.php

<?php

namespace VendorModuleUiDataProviderProductFormModifier;

use MagentoCatalogUiDataProviderProductFormModifierAbstractModifier;
use MagentoCatalogModelLocatorLocatorInterface;

class DefaultQtyModifier extends AbstractModifier
{
    public function __construct(
        LocatorInterface $locator
    ) {
        $this->locator = $locator;
    }

    public function modifyData(array $data)
    {
        $model = $this->locator->getProduct();
        $modelId = $model->getId();

        if (!isset($data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'])) {
            $data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'] = 12;
        }

        return $data;
    }

    public function modifyMeta(array $meta)
    {
        return $meta;
    }
}

Here, instead of 12, you can defined your custom qty number.

Replace Vendor and Module with your original Vendor and Module name.

Hope this helps.

Correct answer by Yash Shah on September 9, 2020

Add your own answers!

Related Questions

Magento 2: Make sales without stock

1  Asked on August 9, 2020 by hudson-alves-triunfante

     

Magento 2 : print_r or var_dump is not working

4  Asked on July 30, 2020 by zed-blackbeard

   

Create quick order form at product page

0  Asked on July 29, 2020 by faraz-bashir

   

How to Special price based on attribute?

2  Asked on July 26, 2020 by user90560

     

is creating block within template in magento 2 good practice

0  Asked on July 24, 2020 by herve-tribouilloy

       

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.