I am adding product to cart using ajax call.

Here is my controller code.

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class AddItems extends \Magento\Framework\App\Action\Action

 protected $formKey;   
 protected $cart;
 protected $product;
 protected $_resource;
 protected $checkoutSession;
 protected $_taxCalculationService;
 protected $quoteRepository;

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\Framework\App\ResourceConnection $resource,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Tax\Api\TaxCalculationInterface $taxCalculationService,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  array $data = []
) {
   $this->formKey = $formKey;
   $this->cart = $cart;
   $this->product = $product;     
   $this->_resource = $resource;
   $this->checkoutSession = $checkoutSession;  
   $this->_taxCalculationService = $taxCalculationService; 
   $this->quoteRepository = $quoteRepository;

 public function execute()

   $productId = 10;
    $params = array(
        'form_key' => $this->formKey->getFormKey(),
        'product_id' => $productId, //product Id
        'qty'   =>1 //quantity of product                
    $_product = $this->product->create()->load($productId);       
     $item = $this->cart->getQuote()->getItemByProduct($_product );
        $quote = $this->cart->getQuote();                   
        $quote->updateItem($item->getId(), array( 'qty' => 1));
        $this->cart->addProduct($_product , $params);
  $customPrice = 1000;
  $grossAmt = 550;

  $productTaxClassId = $_product->getTaxClassId();
  $customerId = $this->getCustomerId();
  $storeId = $this->_storeManager->getStore()->getId();
  $productRates = $this->_taxCalculationService->getCalculatedRate($productTaxClassId, $customerId, $storeId);

  $taxAmount = ((1 * $grossAmt * $productRates) / 100);

  $quote = $this->cart->getQuote();
  $quoteId = $quote->getId();   
  $productItem = $this->getProductQuote($_product );                

    $quote = $this->checkoutSession->getQuote();

public function getProductQuote($product) {
    $quote = $this->checkoutSession->getQuote();        
    $cartItems = $quote->getItemByProduct($product);        
    return $cartItems;
 public function getCustomerId(){
    $customerId =  $this->customerSession->getCustomer()->getId();
    if ($this->customerSession->isLoggedIn()) {
        return $this->customerSession->getCustomer()->getId();
    return 0;

Using above code, I am able to add product to cart with custom price.

I need to add custom tax price also upon adding the product to cart. Right now tax is not getting applied.

How can I set custom tax price for the product added to cart.

Can anyone help me please, custom tax price I have got in variable, $taxAmount

This code save the data to "tax_amount" and "base_tax_amount" fields in quote_item table but when navigated to cart page, tax is showing as 0 and in table values are restored to 0.

             $quote = $this->checkoutSession->getQuote();
            // $quote->save();

Please share the ideas where I am doing wrong here. Thanks in advance

  • Do you want to add fixed amout or % amount of product? – Saphal Jha May 14 '19 at 11:09
  • @SaphalJha, i need to add the Fixed amount, that value already i am getting in taxAmount variable. – jafar pinjar May 14 '19 at 11:10
  • @SaphalJha, I am looking for code how to set custom tax price when product added to cart using its id or sku – jafar pinjar May 14 '19 at 11:23
  • ok, I'll look for solution and update you – Saphal Jha May 14 '19 at 11:32
  • 1
    @magefms, right now once i used above code, in quote_item table, tax_amount value updating but if come to cart page, its getting refreshed to 0 again – jafar pinjar May 15 '19 at 7:45

capture event: sales_quote_add_item

You can set tax price for item on this event.

  • @light, can u update me answer pls, is there any way to set from controller? – jafar pinjar May 15 '19 at 4:40
  • if you would like to add from controller, I think you need to set quote items: $taxAmount = 100; $cartItem = $quote->getItemByProduct($product); $cartItem->setTaxAmount($taxAmount)->setBaseTaxAmount($taxAmount)->save(); $quote->collectTotals(); $quote->save(); – the light May 15 '19 at 4:44
  • This code I tried, but that didn't work for me, but setting custom price s working, tax amount not getting saved – jafar pinjar May 15 '19 at 5:30
  • hello @light, can you help in updating the observer code either, i trigger the mentioned event but looking for code in observer file. – jafar pinjar May 15 '19 at 6:16
  • @jafarpinjar got any solution ? – Manish Maheshwari Aug 27 '19 at 6:03

You can observe to the event sales_quote_address_collect_totals_after and achieve it.

File : app\code\Vendor\Module\etc\events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_address_collect_totals_after">
        <observer name="changeTax" instance="Vendor\Module\Observer\ChangeTax"/>

File : app\code\Vendor\Module\Observer\ChangeTax.php

namespace Vendor\Module\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;

class ChangeTax implements ObserverInterface

    public function execute(Observer $observer)
        /** @var Magento\Quote\Model\Quote\Address\Total */
        $total = $observer->getData('your_data');

  • hi @Rathod, this will add the tax for total, not for the specific quote item is it? how can we apply fixed tax amount for specific quote item, like i set the custom price for quote item only – jafar pinjar May 16 '19 at 6:34
  • here you can received all the data of quote and you can change as per your requirment – Rk Rathod May 16 '19 at 6:35
  • if you don't mind, can you please update code for updating tax amount for specific quote item in the observer file? @Rathod, what need to be replaced in your_data? – jafar pinjar May 16 '19 at 6:36
  • in your_data you can get data which you want – Rk Rathod May 16 '19 at 6:46
  • 1
    This is probably the best answer so far but I think you can actually just add a totals collector. – Shawn Abramson May 21 '19 at 1:27

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.