Categories: How ToMagento 2

How to Set Custom Default qty When Creating New Product in Magento 2

Hello, MageComp Folks,

Today we are going to learn how to set custom default qty. when creating a new product in Magento 2.3. It is a commonly known issue in default Magento.

When you create a new product in default Magento, the qty. of the product is set to 0 which is not good at all because when consumers visit the product page, they think that the product qty is 0 and leave the page without ordering. But the good news is that you can set the custom qty of any number you like when creating a new product in Magento itself with little bit of coding knowledge.

Follow the steps described below to set the custom default qty when creating a new product in Magento2.3.

First thing you want to do is to create a file app\code\Vendor\Extension\etc\adminhtml\di.xml

Once the file is created then use the below code to modify default qty

<?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="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="defaultQtyModifier" xsi:type="array">
                    <item name="class" xsi:type="string">VENDOR\EXTENSION\Ui\DataProvider\Product\Form\Modifier\DefaultQtyModifier</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

If your store has multiple vendors then follow the above step for every vendor.

Now when the above step is complete then create a file, app\code\Vendor\Extension\Ui\DataProvider\Product\Form\Modifier\DefaultQtyModifier.php

Now when the file is created then use the below code to set the custom default qty for new product as any number you like

<?php

namespace Vendor\Extension\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Model\Locator\LocatorInterface;

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'] = 25;
        }

        return $data;
    }

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

Here, instead of 25, you can define your custom qty number and you can set different custom qty for different user groups. For wholesale users, the qty can be set as 50 or 100.

So, in this article, you have learned how to set the custom default qty in when creating a new product in default Magento 2.3. Using the codes described you are able to set the qty as any number you like.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

Thanks and Happy Coding ?

 

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

21 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago