3

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

3

Try this, In your controller add the below code to make it working

<?php                                                                 
namespace {Vendor}\{Module}\Controller\Product;                           
class AddProduct 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);
    //$quote = $this->cart->getQuote();
    //$quoteId = $quote->getId();
    $productItem = $this->getProductQuote($_product);
    //$productItem->setPrice($customPrice);
    //$productItem->setBasePrice($customPrice);
    $productItem->setCustomPrice($customPrice);
    $productItem->setOriginalCustomPrice($customPrice);
    //Enable super mode on the product.
    $productItem->getProduct()->setIsSuperMode(true);
    $this->cart->save();
    echo "success";
}

public function getProductQuote($product)
{
    $quote = $this->checkoutSession->getQuote();
    $cartItems = $quote->getItemByProduct($product);
    return $cartItems;
}}

There the problem was you saved the cart before setting custom price. Hope this helps :)

  • we can not get the $ quoteId until we save the item. I think the above code is not proper – jafar pinjar Mar 2 at 9:57
  • You doesn't need quoteId and moreover $quoteId is not been used there it seems, I've updated it and try this code and it does working fine. Thanks – Prathap Gunasekaran Mar 2 at 10:13
  • 1
    hi @Prathap, this code adds 1 qty each time it is called, can we set only one qty in cart by removing previously added item – jafar pinjar Mar 11 at 14:03
  • Yes we can do that get quote by product id and if exist remove the item from quote and let your run after that @jafarpinjar – Prathap Gunasekaran Mar 12 at 4:53
  • Is it possible to update that code pls? @Prathap – jafar pinjar Mar 12 at 5:49

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.