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 ?
Hello Magento Friends, In this blog, we will learn How to Add a Custom Field…
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…