Good morning, I am implementing a controller that pulls out a list of products and adds them to the shopping cart, but when I give a personalized price to the product, it does not respect me and puts the original.
Any ideas on how to solve this, thanks in advance.
This is my code:
if(count($items)>0)
{
$this->cart->truncate();
foreach ($items as $key => $item) {
$price = $item->getItemPrice();
$price = $priceHelper->currency($price, false, false);
$params = array();
$params['qty'] = $item->getItemQuantity();//product quantity
$params['price'] = $item->getItemPrice();//product price|
$request = new \Magento\Framework\DataObject();
$request->setData($params);
$_product = $this->_productRepository->getById($item->getItemId(),true,null,true);
$_product->setIsSuperMode(true);
$_product->setCustomPrice(20);
$_product->setOriginalCustomPrice(20);
$_product->setSpecialPrice(120);
$_product->setPrice(40);
$_product->save();
$this->cart->addProduct($_product, $request);
}
try
{
$this->cart->save();
$this->messageManager->addSuccessMessage('Cotizacion agregada a carrito correctamente');
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
catch(Exception $e)
{
$this->messageManager->addErrorMessage('Ocurrio un error agregando la cotizacion al carrito');
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}