turtlechief
turtlechief

Reputation: 100

Trying to calculate sales tax on an individual item in Magento without displaying prices including tax

Magento CE ver. 1.7.0.2

I'm trying to grab some order data from our Magento store to integrate into our other business software. In my case I need to compute the price plus tax on individual items. The below code only works if Display Product Prices In Catalog is set to Include or Both (in System > Configuration > Sales > Tax). How can I calculate the tax on an item while still having the website display prices excluding tax?

$customer_tax_class = Mage::getModel('tax/calculation')->getRateRequest()->getCustomerClassId();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$skunumber);
$my_price = Mage::helper('tax')->getPrice($_product, $_product->getPrice(), true, $shippingAddress, $billingAddress, $customer_tax_class);

I also tried using this instead, but I still get prices without tax (unless I change display settings mentioned above):

$_finalPriceInclTax = Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice(), true, $shippingAddress, $billingAddress, $customer_tax_class); 

I know it must be possible, as Magento figures out the tax when you place the order. Any help would be greatly appreciated.

Upvotes: 1

Views: 3133

Answers (1)

turtlechief
turtlechief

Reputation: 100

It took a while to get all the parameters I needed, as we are using a highly customized checkout, but here's what eventually worked for me

    $my_quote = Mage::getSingleton('checkout/session')->getQuote();             
    $my_customer = Mage::getSingleton('customer/session')->getCustomer();
    $my_items = $quote->getAllItems();
    $taxClassId = $qty = $price = array();
    foreach ($my_items as $key => $my_item) {
      //get the price plus tax for this item
      // get the product tax id for this item first.
      $my_sku = $my_item->getSku();
      $qty[$my_sku] = $my_item->getQty(); 
      $taxClassId[$my_sku] = Mage::getModel('catalog/product')->load(
                  $my_item->getProductID())->getData("tax_class_id");
      $price[$my_sku] = Mage::getModel('catalog/product')->load(
                  $my_item->getProductID())->getData("price");
    }
    $my_store = Mage::app()->getStore($my_quote->getStoreId());
    $ctc = $my_customer->getTaxClassId();
    $tax_calc = Mage::getSingleton('tax/calculation');
    $tax_rate_req = $tax_calc->getRateRequest(
        $shippingAddress,
        $billingAddress,
        $ctc,
        $my_store);
    if(is_Array($taxClassId)){
    foreach($taxClassId as $key => $value){
      $my_rate[$key] = Mage::getSingleton('tax/calculation')->getRate(
                 $tax_rate_req->setProductClassId($value));
    }    
    foreach($my_rate as $key => $value){
      foreach($split_filter as $my_key => $my_value){
         //This is used because we split orders based on their shipping method
        if($my_value == $key){
        // This code might malfunction if tax rate is an integer (i.e. 8%)
        if(is_float($value)){
          $my_price = $price[$key];
          $my_qty = $qty[$key];
          $taxy = Mage::getModel('tax/calculation')->calcTaxAmount(
             $my_price, 
             $value
          );
          $price_withtax = $my_price + $taxy;
          // still need to multiply times qty ordered to get row totals
          $row_total = ($price_withtax * $my_qty);
        } else {// $value is not a float.
          $row_total = ($price[$key] * $qty[$key]);
        }
          // then add to other rows to get subtotal
          $subtotal_with_tax += $row_total;
      }
    }

Upvotes: 2

Related Questions