Last active
August 28, 2019 10:49
-
-
Save 0-Sony/d9d2ea792b5533bfeaff28827e6f207a to your computer and use it in GitHub Desktop.
Magento 2 : Set a custom Price after adding product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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