1

When an admin is about to modify the price and save the modification, he should get warning message which he should set a price over $100 and it must not be saved what if he set the price below $100.

Is there any basic function on Magento? Or i can use Observer in admin page.

Please give me an idea for this.

Thank you

2

I am assuming you know how to create basic module in magento2, here I am giving you an idea.

You need to create event.xml file in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?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="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

Now create your observer file Productsaveafter.php at below path -

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $price = $_product->getPrice(); // will get price
        if($price < 100)
        {
           // put your logic here
        }
    }   
}
  • Thank a lot! I created tiny and cute observer referring to your reply! – 3CE Oct 17 '18 at 18:25
  • Sounds cute @3CE, Happy Coding :) – Shashank Kumrawat Oct 17 '18 at 18:27
1

Thanks to @Shashank Kumrawat.

This is my code

app\code\YOUR_NAMESPACE\YOUR_MODULE\etc\adminhtml\event.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="productsaveafter" instance="YOUR_NAMESPACE\YOUR_MODULE\Observer\Productsaveafter" />
   </event>


</config>

app\code\YOUR_NAMESPACE\YOUR_MODULE\Observer\Productsaveafter.php

namespace YOUR_NAMESPACE\YOUR_MODULE\Observer;

class Productsaveafter implements \Magento\Framework\Event\ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    $_product = $observer->getProduct();  // you will get product object
    $producType = $_product->getTypeId();
    $price = $_product->getPrice(); // will get price

    if($_producType=="simple" && $price < 100)
        {

            throw new \Magento\Framework\Exception\CouldNotDeleteException(__("Prices have been changed below 100!"));
        }

     return $this;
  }
}
  • ok so getting any issue ? – Shashank Kumrawat Oct 17 '18 at 18:30
  • Nothing! Perfect working! – 3CE Oct 17 '18 at 18:31
  • @ShashankKumrawat Btw after inserting this code, the loading time of product grid is extremely slow. Do you know what happened to it? – 3CE Oct 17 '18 at 19:31
  • where are you applying this code? – Shashank Kumrawat Oct 17 '18 at 19:45
  • are you sure product grid at admin is slow due to above code ? because above code run only on product_save observer then it redirects to grid page with displaying your error msg. thus grid page should not get affected with this. – Shashank Kumrawat Oct 17 '18 at 20:01

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.