0

I am getting that tax using shipping address if state is Washington than this tax will be applied postcode wise.

I override Magento\Tax\Model\Sales\Total\Quote\Tax.php

code

protected function processProductItems(
    ShippingAssignmentInterface $shippingAssignment,
    array $itemTaxDetails,
    QuoteAddress\Total $total
) {
     $store = $shippingAssignment->getShipping()->getAddress()->getQuote()->getStore();
    $ship = $shippingAssignment->getShipping()->getAddress()->getShippingMethod();
    $shippingAddress = $shippingAssignment->getShipping()->getAddress();
    $regionId = '';
    $postCode = ''; 
    $waaddress ='';     
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $quote = $objectManager->create('Magento\Checkout\Model\Cart')->getQuote()->getShippingAddress();
    if(!empty($quote)){
        $regionId = $quote->getRegionId();
        $postCode = $quote->getPostCode();
    }else{
        $regionId = $shippingAddress->getRegionId();
        $postCode = $shippingAddress->getPostCode();
    }



    /** @var AbstractItem[] $keyedAddressItems */
    $keyedAddressItems = [];
    foreach ($shippingAssignment->getItems() as $addressItem) {
        $keyedAddressItems[$addressItem->getTaxCalculationItemId()] = $addressItem;
    }

    $subtotal = $baseSubtotal = 0;
    $discountTaxCompensation = $baseDiscountTaxCompensation = 0;
    $tax = $baseTax = 0;
    $subtotalInclTax = $baseSubtotalInclTax = 0;

    foreach ($itemTaxDetails as $code => $itemTaxDetail) {
        /** @var TaxDetailsItemInterface $taxDetail */
        $taxDetail = $itemTaxDetail[self::KEY_ITEM];
        /** @var TaxDetailsItemInterface $baseTaxDetail */
        $baseTaxDetail = $itemTaxDetail[self::KEY_BASE_ITEM];
        $quoteItem = $keyedAddressItems[$code];
        $this->updateItemTaxInfo($quoteItem, $taxDetail, $baseTaxDetail, $store);

        //Update aggregated values
        if ($quoteItem->getHasChildren() && $quoteItem->isChildrenCalculated()) {
            //avoid double counting
            continue;
        }
        $subtotal += $taxDetail->getRowTotal();
        $baseSubtotal += $baseTaxDetail->getRowTotal();
        $discountTaxCompensation += $taxDetail->getDiscountTaxCompensationAmount();
        $baseDiscountTaxCompensation += $baseTaxDetail->getDiscountTaxCompensationAmount();
        $tax += $taxDetail->getRowTax();
        $baseTax += $baseTaxDetail->getRowTax();
        $subtotalInclTax += $taxDetail->getRowTotalInclTax();
        $baseSubtotalInclTax += $baseTaxDetail->getRowTotalInclTax();
    }

    //Set aggregated values
    $total->setTotalAmount('subtotal', $subtotal);
    $total->setBaseTotalAmount('subtotal', $baseSubtotal);

    if($regionId == 62){
        $wacity ='';

        $wazipcode = $postCode;
        $URL = "http://dor.wa.gov/AddressRates.aspx?output=xml&";
        $URL .= "addr=" .$waaddress. "&city=" . $wacity . "&zip=" . $wazipcode;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        $parser = xml_parser_create();
        xml_parse_into_struct($parser, $response, $values);
        //result code
        if(isset($values[0])){
        $resultCode = $values[0]["attributes"]["CODE"];
        //tax rate
        $rate = $values[0]["attributes"]["RATE"]*100;
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($rate);
        $logger->info($URL);
        if($rate>0)
        {
            $rate = ($subtotal*$rate)/100;
        }
        $total->setTotalAmount('tax', $rate);
        $total->setBaseTotalAmount('tax', $rate);
        }else{
            $total->setTotalAmount('tax', $tax);
        $total->setBaseTotalAmount('tax', $baseTax);
        }
    }else{
        $total->setTotalAmount('tax', $tax);
        $total->setBaseTotalAmount('tax', $baseTax);
    }

    $total->setTotalAmount('discount_tax_compensation', $discountTaxCompensation);
    $total->setBaseTotalAmount('discount_tax_compensation', $baseDiscountTaxCompensation);

    $total->setSubtotalInclTax($subtotalInclTax);
    $total->setBaseSubtotalTotalInclTax($baseSubtotalInclTax);
    $total->setBaseSubtotalInclTax($baseSubtotalInclTax);
    //return parent::processProductItems($shippingAssignment,$itemTaxDetails,$total);
    return $this;
}

Using this i am adding tax in total but i want to add tax product item wise

How can i add tax in product item programmatically shipping address wise?

  • Have you tried looking into events? – Julien Lachal Sep 8 '16 at 8:52
  • No i am tried looking into Magento Tax Module but i can't find for it. – Newbie Sep 8 '16 at 8:54

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.