Jump to content

Get cart tax totals


alexm
 Share

Recommended Posts

Happy Friday @kongondo I see that getCartTaxTotals() is no longer in use therefor $padloper->cart->getCartTaxTotals() is redundant. There is reference to a utility for this but I can't see it in utilities, only one for orders. Am I missing something or is this yet to be implemented for PadloperCart?

Thanks in advance ?

Link to comment
Share on other sites

Or perhaps 

$padloper->padloperUtilities->calculateTaxOnAmount($padloper->getOrderTotalAmount())

is what I'm after? I'm just getting 0 on cart edit though even if I apply a base 20% tax to all items.

===== 

Yeah, nah, that won't work cus that's for orders not cart items. Hmmm.

Link to comment
Share on other sites

Hi @alexm,

Yes, the method moved. It is in the docs here. However, currently something has messed up the JavaScript in the docs so the content is not loading. Here's the code from that example:

 

<?php

namespace ProcessWire;
/** @var WireArray $orderLineItems */
$orderLineItems = $padloper->getOrderLineItems();
/** @var array $orderTaxTotals */
$orderTaxTotals = $padloper->getOrderTaxTotals($orderLineItems);
foreach ($orderTaxTotals as $taxShortName => $taxValue) {
    echo "<span>{$taxShortName}: </span><span>" . $padloper->renderCartPriceAndCurrency($taxValue) . "</span><br>";
}

 

26 minutes ago, alexm said:

I'm assuming this is due to the necessity to know the customer's country first obviously.

Exactly.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Somehow related: 

$padloper->getOrderTotalAmount() returns 0 once the order is confirmed.

I am trying to display the order net amount (Excluding taxes and shipping) in the order-products-table.php template. In /checkout-custom/confirmation/ it works, but in /checkout-custom/success/ it returns 0.

Link to comment
Share on other sites

Hi @Jan Fromm,

5 hours ago, Jan Fromm said:

but in /checkout-custom/success/ it returns 0.

That's the expected behaviour. This is because for a successful order checkout, when /checkout-custom/success/ is called, the order has been finalised hence Padloper clears the sessions related to the successful order. Hence, $padloper->getOrderTotalAmount() finds nothing. In fact, if you reload the page at this point, you will be redirected to the shop's homepage as their is no current 'cart'.

5 hours ago, Jan Fromm said:

I am trying to display the order net amount (Excluding taxes and shipping) in the order-products-table.php template.

There's at least two ways to do this. order-products-table.php template receives a number of template variables:

<?php

namespace ProcessWire;

/** @var WireData $order */
/** @var WireArray $orderLineItems */
/** @var float $orderSubtotal */
/** @var bool $isOrderGrandTotalComplete */
/** @var bool $isOrderConfirmed */

We can use those to calculate the order amount minus taxes and shipping. 

Method #1: ADD TOTAL PRICE OF EACH LINE ITEM

<?php

namespace ProcessWire;

// init empty total price
$totalPriceMinusTaxesAndShipping = 0;
foreach ($orderLineItems as $orderLineItem) {
  // @note: there are also aliases to line item 'totalPrice'
  // if you prefer, i.e. 'lineItemTotalPrice' and 'orderItemTotalPrice'
  // -------
  // add price of each line item
  /** @var WireData $orderLineItem */
	$totalPriceMinusTaxesAndShipping += (float) $orderLineItem->totalPrice;
}
// @debug: here is your total price without taxes and shipping
// @note: if you applied discounts, those will be reflected in this value
bd($totalPriceMinusTaxesAndShipping, __METHOD__ . ': $totalPriceMinusTaxesAndShipping at line #' . __LINE__);

 

Method #2: GET ORDER SUBTOTAL AND MINUS ORDER TAX TOTALS

<?php

namespace ProcessWire;

/** @var array $orderTaxTotalsArray */
// get all the taxes applied to order
// this returns 'tax_name' => 'total_tax_applied' values
$orderTaxTotalsArray = $padloper->getOrderTaxTotals($orderLineItems);
// sum the tax totals
$orderTaxTotals = array_sum($orderTaxTotalsArray);

// $orderSubtotal  = order_total + tax_total (it doesn't including shipping)
// subtract taxes from order subtotal
$totalPriceMinusTaxesAndShipping = (float) $orderSubtotal - $orderTaxTotals;
// @debug: here is your total taxes
bd($orderTaxTotals, __METHOD__ . ': $orderTaxTotals at line #' . __LINE__);
// @debug: here is your total price without taxes and shipping
bd($totalPriceMinusTaxesAndShipping, __METHOD__ . ': $totalPriceMinusTaxesAndShipping at line #' . __LINE__);

 

Hope this helps.

Edited by kongondo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...