0

I want to set Discount on my product after Save product so I'm using catalog_product_save_after then I am creating an attribute 'discount' and set the calculated value, but when i save the collection it not working properly ('stuck & looping admin panel loader'). How can i solve this issue ?

namespace Wac\SortDiscount\Observer\Catalog;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;


class ProductSaveAfter implements ObserverInterface
{

    public function execute(Observer $observer)
    {
        $product = $observer->getProduct();
        $orgprice = $product->getPrice();
        $specialprice = $product->getSpecialPrice();
        $product->setDiscount($orgprice - $specialprice);
        // die($specialprice);
        $product->save();
    }
}

Thanks

1 Answer 1

4

I've saved the custom attribute value to the product with the below code.

class FilterProduct implements ObserverInterface {

    /**
     * @var \Magento\ConfigurableProduct\Model\Product\Type\ConfigurableFactory
     */
    protected $configurableProductProductTypeConfigurableFactory;

    public function __construct(
    \Magento\ConfigurableProduct\Model\Product\Type\ConfigurableFactory $configurableProductProductTypeConfigurableFactory
    ) {
        $this->configurableProductProductTypeConfigurableFactory = $configurableProductProductTypeConfigurableFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $product = $observer->getProduct();
        $productTypeId = $product->getTypeId();
        if (in_array($productTypeId, array("configurable", "grouped", "bundle"))) {
            if ($productTypeId == "configurable") {
                $skus = array();
                $childProducts = $this->configurableProductProductTypeConfigurableFactory->create()->getUsedProducts($product);
                foreach ($childProducts as $child) {
                    $skus[] = $child->getSku();
                }
                $skus = implode(', ', $skus);
                $product->setChildSku($skus);
                $product->getResource()->saveAttribute($product, 'child_sku');
            }
        }
    }

}

You need to change your code from

public function execute(Observer $observer)
    {
        $product = $observer->getProduct();
        $orgprice = $product->getPrice();
        $specialprice = $product->getSpecialPrice();
        $product->setDiscount($orgprice - $specialprice);
        $product->save();
    }

To this

public function execute(Observer $observer)
    {
        $product = $observer->getProduct();
        $orgprice = $product->getPrice();
        $specialprice = $product->getSpecialPrice();
        $product->setDiscount($orgprice - $specialprice);
        $product->getResource()->saveAttribute($product, 'discount');
    }

Hope this will help!!!

5
  • same issue...! stuck the loader Commented Aug 22, 2019 at 4:49
  • I've added the code which I've used to update the custom attribute value for your reference. Hope this will help you to find the solution.
    – Sumit
    Commented Aug 22, 2019 at 5:13
  • its working great .....! thanks dude ; ) Commented Aug 22, 2019 at 5:41
  • You're welcome :)
    – Sumit
    Commented Aug 22, 2019 at 5:42
  • @Sumith please answer this one magento.stackexchange.com/q/289872/80389 Commented Sep 17, 2019 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.