Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active August 28, 2019 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0-Sony/d9d2ea792b5533bfeaff28827e6f207a to your computer and use it in GitHub Desktop.
Save 0-Sony/d9d2ea792b5533bfeaff28827e6f207a to your computer and use it in GitHub Desktop.
Magento 2 : Set a custom Price after adding product
<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="set_custom_price" instance="MyNamespace\MyModule\Observer\SetCustomPriceAfterAddProduct" />
</event>
</config>
<?php
/**
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Phuong LE <sony@menincode.com> <@>
* @copyright Copyright (c) 2019 Menincode (http://www.menincode.com)
*/
namespace MyNamespace\MyModule\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class SetCustomPriceAfterAddProduct implements ObserverInterface
{
/**
* @event checkout_cart_product_add_after
* @param Observer $observer
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(Observer $observer)
{
/** @var \Magento\Quote\Model\Quote\Item $item */
$item = $observer->getEvent()->getData('quote_item');
$item = ($item->getParentItem() ? $item->getParentItem() : $item);
$price = 100;
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment