1

I want to set custom quote price programmatically. Without the observer method.

The observer method is working fine but it is worked on add to cart button.

Please check my observer file below

<?php
namespace Codism\Csr\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomPrice implements ObserverInterface
{
    protected $_request;

    public function __construct(\Magento\Framework\App\RequestInterface $request)
    {
        $this->_request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $item = $observer->getEvent()->getData('quote_item');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $price = 100; //set your price here
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
    }

}

I want to set custom quote price using the controller. Please tell me how is it possible.

enter image description here

1

Update

For your requirement,You have to send cart item id and price to your custom controller on submit click.Best idea to create <form></form> on each item and pass quote item id and custom price as input field

And controller you have to get fetch that quote/cart item and custom price and update price.

<?php
namespace {VendorName}\{MOdulename}\Controller\Index;
use Magento\Checkout\Model\Cart as CustomerCart;

class CartItemUpdate  extends \Magento\Framework\App\Action\Action {
    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Checkout\Model\Session $checkoutSession,
            CustomerCart $cart
    ) {
        parent::__construct($context);
        $this->checkoutSession = $checkoutSession;
        $this->cart = $cart;
    }

    public function execute() {

        $id = (int)$this->getRequest()->getParam('quote_item_id');
        $customPrice = (int)$this->getRequest()->getParam('custom_price');
        $item = $this->cart->getQuote()->getItemById($id);
         if (!$item) {
                throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t find the quote item.'));
         }
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
        $item->save();
        $this->cart->save();
    }

}

OLD

I am not sure about what controller you have talking about.

If you want to update the price of quote item then first you have to fetch your quote and then get the item from your quote after you can update the price.

I have create a Action class: {VendorName}\{MOdulename}\Controller\Index\CartItemUpdate and here I have updated all cart item price to 10;

<?php
namespace {VendorName}\{MOdulename}\Controller\Index;


class CartItemUpdate  extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Checkout\Model\Session $checkoutSession
    ) {
        parent::__construct($context);
        $this->checkoutSession = $checkoutSession;
    }

    public function execute() {

        $quote = $this->checkoutSession->getQuote();
        $quoteItems= $quote->getAllItems();
        foreach ($quoteItems as $item )
        {
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
                $price = 100; //set your price here
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
                $item->getProduct()->setIsSuperMode(true);
                $item->save();
        }
        $quote->save();
    }

}
  • Hi, I have to Fetch all data from the checkout session as frontend side. and update the custom price using the controller is not working. – Masud Shaikh Oct 29 '18 at 7:56
  • Can i share you code? – Masud Shaikh Oct 29 '18 at 7:56
  • Please check screenshot in my post. Fetch all quote data on my custom quote page and i want to set custom price. – Masud Shaikh Oct 29 '18 at 8:09
  • hi @Amit,how to set custom tax price like custom price? I need to set custom tax price – jafar pinjar May 14 at 10:38

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.