Do not send tax in each item

Looks like it is not displayed anyway, and it causes mismatch + cannot send shipping/discount taxes as tax
This commit is contained in:
Alex P 2022-06-15 10:00:03 +03:00
parent 0e82a5f6c9
commit 770f8e558b
3 changed files with 12 additions and 12 deletions

View file

@ -343,8 +343,14 @@ class PurchaseUnit {
} }
} }
$tax_total = $breakdown->tax_total(); $tax_total = $breakdown->tax_total();
if ( $tax_total ) { $items_with_tax = array_filter(
$this->items,
function ( Item $item ): bool {
return null !== $item->tax();
}
);
if ( $tax_total && ! empty( $items_with_tax ) ) {
$remaining_tax_total = array_reduce( $remaining_tax_total = array_reduce(
$items, $items,
function ( float $total, Item $item ): float { function ( float $total, Item $item ): float {

View file

@ -56,14 +56,12 @@ class ItemFactory {
$price = (float) wc_get_price_including_tax( $product ); $price = (float) wc_get_price_including_tax( $product );
$price_without_tax = (float) wc_get_price_excluding_tax( $product ); $price_without_tax = (float) wc_get_price_excluding_tax( $product );
$price_without_tax_rounded = round( $price_without_tax, 2 ); $price_without_tax_rounded = round( $price_without_tax, 2 );
$tax = round( $price - $price_without_tax_rounded, 2 );
$tax = new Money( $tax, $this->currency );
return new Item( return new Item(
mb_substr( $product->get_name(), 0, 127 ), mb_substr( $product->get_name(), 0, 127 ),
new Money( $price_without_tax_rounded, $this->currency ), new Money( $price_without_tax_rounded, $this->currency ),
$quantity, $quantity,
substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '', substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '',
$tax, null,
$product->get_sku(), $product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
); );
@ -81,7 +79,7 @@ class ItemFactory {
new Money( (float) $fee->amount, $this->currency ), new Money( (float) $fee->amount, $this->currency ),
1, 1,
'', '',
new Money( (float) $fee->tax, $this->currency ) null
); );
}, },
$fees_from_session $fees_from_session
@ -130,15 +128,13 @@ class ItemFactory {
$price = (float) $order->get_item_subtotal( $item, true ); $price = (float) $order->get_item_subtotal( $item, true );
$price_without_tax = (float) $order->get_item_subtotal( $item, false ); $price_without_tax = (float) $order->get_item_subtotal( $item, false );
$price_without_tax_rounded = round( $price_without_tax, 2 ); $price_without_tax_rounded = round( $price_without_tax, 2 );
$tax = round( $price - $price_without_tax_rounded, 2 );
$tax = new Money( $tax, $currency );
return new Item( return new Item(
mb_substr( $item->get_name(), 0, 127 ), mb_substr( $item->get_name(), 0, 127 ),
new Money( $price_without_tax_rounded, $currency ), new Money( $price_without_tax_rounded, $currency ),
$quantity, $quantity,
substr( wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ), 0, 127 ) ?: '', substr( wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ), 0, 127 ) ?: '',
$tax, null,
$product instanceof WC_Product ? $product->get_sku() : '', $product instanceof WC_Product ? $product->get_sku() : '',
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS ( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
); );
@ -158,7 +154,7 @@ class ItemFactory {
new Money( (float) $item->get_amount(), $order->get_currency() ), new Money( (float) $item->get_amount(), $order->get_currency() ),
$item->get_quantity(), $item->get_quantity(),
'', '',
new Money( (float) $item->get_total_tax(), $order->get_currency() ) null
); );
} }

View file

@ -72,7 +72,6 @@ class ItemFactoryTest extends TestCase
$this->assertEquals('name', $item->name()); $this->assertEquals('name', $item->name());
$this->assertEquals('sku', $item->sku()); $this->assertEquals('sku', $item->sku());
$this->assertEquals(1, $item->unit_amount()->value()); $this->assertEquals(1, $item->unit_amount()->value());
$this->assertEquals(2, $item->tax()->value());
} }
public function testFromCartDigitalGood() public function testFromCartDigitalGood()
@ -185,7 +184,6 @@ class ItemFactoryTest extends TestCase
$this->assertEquals(1, $item->quantity()); $this->assertEquals(1, $item->quantity());
$this->assertEquals(Item::PHYSICAL_GOODS, $item->category()); $this->assertEquals(Item::PHYSICAL_GOODS, $item->category());
$this->assertEquals(1, $item->unit_amount()->value()); $this->assertEquals(1, $item->unit_amount()->value());
$this->assertEquals(2, $item->tax()->value());
} }
public function testFromWcOrderDigitalGood() public function testFromWcOrderDigitalGood()