0

I want to apply shipping tax class based on products in cart. How can i change the shipping tax class in magento programmatically. I tried below links but i am getting total tax amount. I need to apply a tax class to shipping amount.

Lets say shipping amount is 499 , then i need to apply 5% shipping tax if all the products are under 5% tax. If products having 12% tax then i need to apply 12% tax to shipping amount.

How to get Tax Rates from Tax Class?

Apply Tax to Shipping Amount in Magento2

<?php
/**
 * Codilar Technologies Pvt. Ltd.
 * @category    Codilar_Gst Extension
 * @package    Codilar\Gst\Controller\Checkout
 * @copyright   Copyright (c) 2017 Codilar. (http://www.codilar.com)
 * @purpose
 * @author       Codilar Team
 **/
namespace Codilar\Gst\Controller\Checkout;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\AddressFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class ShippingAddress extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_logger;
    /**
     * @var AddressFactory
     */
    protected $_addressFactory;
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;
    /**
     * @var Customer
     */
    protected $_customer;

    /**
     * ChangeBillingAddress constructor.
     * @param Context $context
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface,
        AddressFactory $addressFactory,
        Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Customer $customer
    ) {
        parent::__construct($context);
        $this->_logger = $loggerInterface;
        $this->_addressFactory = $addressFactory;
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession;
        $this->_customer = $customer;
    }

    public function execute()
    {
        $gstin = $this->getRequest()->getParam('gstin');


        $items = $this->_checkoutSession->getQuote()->getAllVisibleItems();

       $highest_gst=0;
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();


        foreach($items as $item) {

              $product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
             if ($product->getGstRate() > $highest_gst){
                $highest_gst = $product->getGstRate();
             }    
          }
  //echo ' final highest gst is'.$highest_gst;
        if($this->_customerSession->isLoggedIn()){
            $this->_checkoutSession->setGuestCheckout("0");
            $customerId = $this->_customerSession->getCustomerId();
            $customer = $this->_customer->load($customerId);
            if(!$customer->getAddresses()){
                $this->_checkoutSession->getQuote()->getShippingAddress()->setData('gstin',$gstin)->setAddressType('shipping')->save();
                $this->_checkoutSession->setNewCustomer("1");
                $this->_checkoutSession->setNewCustomerShippingGst($gstin);
                $this->_checkoutSession->setNewCustomerSaveShippingAddress("1");
            }
            else{
                $this->_checkoutSession->setOldCustomer("1");
                if($this->_checkoutSession->getQuote()->getShippingAddress()->getCustomerAddressId()){
                    $shippingAddressId = $this->_checkoutSession->getQuote()->getShippingAddress()->getCustomerAddressId();
                    $addressCollection = $this->_addressFactory->create()->load($shippingAddressId);
                    $shippingGstin = $addressCollection->getGstin();
                    $this->_checkoutSession->setOldShippingGst($shippingGstin);
                    $this->_checkoutSession->getQuote()->getShippingAddress()->setData('gstin',$highest_gst)->setAddressType('shipping')->save();


                }
                else{
                    $shippingGstin = $gstin;
                    $this->_checkoutSession->setOldNewShippingGst($highest_gst);
                }
            }

        }
        else{
            $this->_checkoutSession->getQuote()->getShippingAddress()->setData('gstin',$gstin)->setAddressType('shipping')->save();
            $this->_checkoutSession->setGuestCheckout("1");
            $this->_checkoutSession->setGuestShippingGst($gstin);
        }
        $data['status'] = 1;
        print_r(json_encode($data));
    }
}
  • setData('gstin',$gstin) is not working. Am I correct – MSA May 2 at 8:39
  • yes.. it is taking default magento settings tax classes instead of this. – Dinesh May 2 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.