2

I know I asked the question once before, but i got no answer, so I try it again and hope for an answer this time. I also saw some other guys that tried to get an answer for this question, but none of them got one, so i need your help.

I would like to set a custom discount price to an existing cart/quote in my controller.

To give you an example, if the customer clicks a button in the cart he will get a random discount price. But now how can I achieve to add this specific disount price to the cart and quote in the controller?

I would appreciate if you could help me with this question.

0

This can be achieved by using event. use event checkout_cart_product_add_after. Let have an Observer with name AddToCart.php. We would be needing events.xml ofcourse for letting magento2 know.

let makes a file etc/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="checkout_cart_product_add_after">
        <observer name="vendor_catalogevent" instance="Vendor\Module\Observer\Product\CustomOption\PriceRule\Addtocart" />
    </event>
</config>

Defining Observer , make a file in Observer/AddToCart.php

namespace Vendor\Module\Observer\Product\CustomOption\PriceRule;


use Magento\Catalog\Model\ProductFactory;

/**
 * Class Addtocart
 * @package Vendor\Module\Observer\Product\CustomOption\PriceRule
 */
class Addtocart implements \Magento\Framework\Event\ObserverInterface {

  /**
   * @var \Magento\Framework\App\RequestInterface
   */
  protected $_request;

  /**
   * @var \Magento\Store\Model\StoreManagerInterface
   */
  protected $_storeManager;

  /**
   * @var \Magento\Catalog\Model\Product
   */
  protected $_product;

  public function __construct(
        \Magento\Catalog\Model\Product $product,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\RequestInterface $request
    )
    {
      $this->_product                 = $product;
      $this->_storeManager            = $storeManager;
      $this->_request                 = $request;
    }

  /**
   * @param \Magento\Framework\Event\Observer $observer
   * @throws \Magento\Framework\Exception\NoSuchEntityException
   */
  public function execute(\Magento\Framework\Event\Observer $observer)
    {
      $item = $observer->getEvent()->getData('quote_item');
      $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
      $percentFactor = 0.2 //giving 20% discount
      $sku = $item->getSku();
      $productCollection = $this->_product->loadByAttribute('sku', $sku);
      $productPriceBySku = $productCollection->getPrice();
      $customPrice = $productPriceBySku * $percentFactor; // custom price
      $item->setCustomPrice($customPrice);
      $item->setOriginalCustomPrice($customPrice);
      $item->getProduct()->setIsSuperMode(true);
    }
}

Let me know if this solves your problem.

Thank you.

| improve this answer | |
  • As I said I want to achieve this in a controller and not in a observer, i found a lot of solutions using an observer, but I just can't use an observer. – Felix Schönherr Oct 28 '19 at 14:40
  • Then you should override AddProduct function which is present in Magento\Checkout\Model\Cart and add your logic right before checkout_cart_product_add_after event dispatch. – Ali Hussain Oct 28 '19 at 15:13
  • Sorry, but this will also not help – Felix Schönherr Oct 29 '19 at 8:03

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.