3

I am having a module that is creating quotes using the Magento quote module.

Now I want to proceed to checkout that should add the quote items to the cart and the checkout page should be displayed to the user with those items that were in the quote.

Here I am creating the quotes as:

$quote = $this->quoteFactory->create()->load($quoteId);

The quotes are creating fine and I am getting the items in the quote as:

$items = $quote->getAllItems();

I am adding the products to cart as below,

$items = $quote->getAllItems();

foreach ($items as $item) {
    $formatedPrice = $item->getPrice();
    $quantity = $item['qty'];
    $productId = $item->getProductId();

    $params = array(
          'form_key' => $this->formKey->getFormKey(),
          'product' => $productId, //product Id
          'qty' => $quantity, //quantity of product
          'price' => $formatedPrice //product price
    );

    $_product = $this->_productRepository->getById($productId);

    if ($_product) {
        $this->cart->addProduct($_product, $params);
    }
}
try {
    $this->cart->save();
    $this->messageManager->addSuccess(__('Added to cart successfully.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
    $this->messageManager->addException($e, __('%1', $e->getMessage()));
}

The issue here is the items are getting added into the cart but in case there are products that have a custom price, I need to add those products to the cart, with a different price to what is configured for the product in the catalog.

That custom price is defined in,

$formatedPrice = $item->getPrice();

Also, I am getting an issue where whenever I am creating a new quote and adding a previous quote to cart it is displaying the items for the latest quote that was created. How can this happen when the quote id is correct here.

I actually want to do something like this in Magento 2: Programmatically add product to cart with price change

Please, can anyone help in figure this out?

Cœur
  • 37,241
  • 25
  • 195
  • 267
manini
  • 358
  • 1
  • 4
  • 14

2 Answers2

2

This worked for me in Magento 2.2.8:

In a controller:

        $price = rand(0,1000);

        $this->product->setData('custom_overwrite_price', $price);

        $params = [
            'form_key' => $this->formKey->getFormKey(),
            'qty' => 1,
            'options' => ...
        ];

        $this->cart->addProduct($this->product, $params);
        $this->cart->save();

In checkout_cart_product_add_after

public function execute(\Magento\Framework\Event\Observer $observer) {
    $item = $observer->getEvent()->getData('quote_item');
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

    $price = $item->getProduct()->getData('custom_overwrite_price');

    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);
    $item->getProduct()->setIsSuperMode(true);
}
Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
Alex
  • 32,506
  • 16
  • 106
  • 171
  • 1
    This solution works fine for me in 2.3.1 just for extra explanation what @Alex it is doing is to save in the controller the price and use it in the observer so you will need to add your observer file to etc/events.xml and create the file in Observers: https://magento.stackexchange.com/questions/179633/magento2-how-create-an-observer – open-ecommerce.org Nov 19 '19 at 13:52
-1

Working solution and really easy if you think about it:

$params = array(
  'form_key' => $this->_formKey->getFormKey(),
  'product' => $productId,
  'qty'   => $qty
);

$product = $this->_product->load($productId); 
$product->setPrice($customPrice); // without save this does the trick
$this->cart->addProduct($product, $params);
$this->cart->save();

The missing pieces feel free to fill them in.

  • 1
    Did not work for me - the subtotal was not properly updated (at least in a solution based on this) – Alex Apr 12 '19 at 11:08