I am adding the product to the cart with below code.
class AddItems extends \Magento\Framework\App\Action\Action
{
protected $formKey;
protected $cart;
protected $product;
protected $checkoutSession;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\ProductFactory $product,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->checkoutSession = $checkoutSession;
parent::__construct($context);
}
public function execute()
{
$productId = 1;
$customPrice = 150;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $ProductId, //product Id
'qty' => 1 //quantity of product
);
$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);
$this->cart->save();
$quote = $this->cart->getQuote();
$quoteId = $quote->getId();
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
// Enable super mode on the product.
$productItem->getProduct()->setIsSuperMode(true);
}
public function getProductQuote($product){
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;
}
}
I am adding custom price to the quote item with above code. Its not updating the custom price.
Can anyone look into this and update me please. Thanks
$productItem->save();
$productItem->save();
$this->cart->save();