0

I want to change dynamically the qty of simple product according to custom management so how to proceed using plugin or a certain event and is their a specific function to mention like afterGetName when we want to change the display of name. any help please

  • You mean once you create a product from the admin. At a time qty should update as per your requirement. Like, if you added qty 10 at the creating time and once you save the product your qty should update as per you want. – Ravi Soni Jul 2 at 12:50
  • yes , exactly that's what i want – Developper Magento Jul 2 at 12:54
  • So here one question. Why you did not add that qty whatever you want at the time of creation? – Ravi Soni Jul 2 at 12:55
  • because i created another custom entity that have the relation many to one with a simple product so one my product is created i will assign as many of this entity to a simple product and the qty of my product will chaneg automatically to the total of quantity of all associated custom entities – Developper Magento Jul 2 at 12:56
  • i should create the product then assign some custom entity and finally i will know the real qty of my product so how to poceed please – Developper Magento Jul 2 at 12:58
1

You can achieve this via observer

/app/code/Custom/Module/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="Custom\Module\Observer\Productsaveafter" />
    </event>
</config>

Your observer file is

/app/code/Custom/Module/Observer/Productsaveafter.php

<?php

namespace Commercepundit\Simpleshipping\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId(); 
        $stockItem = $product->getExtensionAttributes()->getStockItem();
        $stockItem->setQty(45); //Set New Qty to Main Qty
        $stockItem->save();
    }   
}

You should set your qty whatever you want.

Thanks

  • thanks for your replay i will try this immediatly – Developper Magento Jul 2 at 13:20
  • I tried this code but the problem is that i have to always return to the product and resave it but i want this that the quantity change automatically – Developper Magento Jul 3 at 6:42
  • i need to change the qtity with a plugin but what will be the type of this plugin do you have any idea please – Developper Magento Jul 3 at 6:47
  • You want same thing. But via plugins. Right ? – Ravi Soni Jul 3 at 6:49
  • yes but with event i sould resave the aproduct but with a plugin it wil change without returning to product – Developper Magento Jul 3 at 8:57

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.