#StackBounty: #magento2 #price #addtocart #tax How to set custom tax price during add to cart in Magento 2

Bounty: 50

I am adding product to cart using ajax call.

Here is my controller code.

use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
class AddItems extends MagentoFrameworkAppActionAction
{

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

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCheckoutModelSession $checkoutSession,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoQuoteApiCartRepositoryInterface $quoteRepository,
  array $data = []
) {
   $this->formKey = $formKey;
   $this->cart = $cart;
   $this->product = $product;     
   $this->_resource = $resource;
   $this->checkoutSession = $checkoutSession;  
   $this->_taxCalculationService = $taxCalculationService; 
   $this->quoteRepository = $quoteRepository;
    parent::__construct($context);
 }

 public function execute()
{ 

 try{
   $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 );
    if($item){          
        $quote = $this->cart->getQuote();                   
        $quote->updateItem($item->getId(), array( 'qty' => 1));
        $quote->save();
    }else{                  
        $this->cart->addProduct($_product , $params);
    }
  $customPrice = 1000;
  $grossAmt = 550;

  $productTaxClassId = $child_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 );                
  $productItem->setCustomPrice($customPrice);
  $productItem->setOriginalCustomPrice($customPrice);
  $productItem->getProduct()->setIsSuperMode(true);   
  $this->cart->save(); 
   $productItem->setTaxAmount($taxAmount)->setBaseTaxAmount($taxAmount)->save(); 
    $quote = $this->checkoutSession->getQuote();
    $quote->collectTotals();
    //$quote->save(); 
    $this->quoteRepository->save($quote);
   }
 }

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

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.

$productItem->setTaxAmount($taxAmount)->setBaseTaxAmount($taxAmount)->save(); 
             $quote = $this->checkoutSession->getQuote();
             $quote->collectTotals();
             $this->quoteRepository->save($quote);
            // $quote->save();

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


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.