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

9
  • 1
    you can add ? this one $productItem->save(); Commented Mar 2, 2019 at 8:22
  • also check this link magento.stackexchange.com/a/248218/68695 Commented Mar 2, 2019 at 8:44
  • @RakeshDonga, Yes tried save() method, that did not work for me Commented Mar 2, 2019 at 9:56
  • you can add last of execute function $productItem->save(); $this->cart->save(); Commented Mar 2, 2019 at 9:57
  • @RakeshDonga, can you please update the answer, I am not cleared Commented Mar 2, 2019 at 9:59

1 Answer 1

4

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

<?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();
    $cartItem = $quote->getItemByProduct($product);
    return $cartItem;
}}

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

11
  • we can not get the $ quoteId until we save the item. I think the above code is not proper Commented Mar 2, 2019 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 Commented Mar 2, 2019 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 Commented Mar 11, 2019 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 Commented Mar 12, 2019 at 4:53
  • Is it possible to update that code pls? @Prathap Commented Mar 12, 2019 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.