Skip to content

Commit e5085f9

Browse files
committed
don't create WC_Order_Item_Tax for order without any taxes
1 parent e4e8ba3 commit e5085f9

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

src/Orders/Order.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ public function add() {
154154
$item = new \WC_Order_Item_Shipping();
155155
$item->set_method_title( $this->shipping->get_method() );
156156
$item->set_total( $this->shipping->get_total() );
157-
if ( $this->include_vat ) {
157+
if ( $this->include_vat && $this->shipping->get_total_tax() > 0 ) {
158158
$item->set_taxes(
159159
[
160160
'total' => [
161-
self::RATE_ID => (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'],
161+
self::RATE_ID => $this->shipping->get_total_tax(),
162162
],
163163
]
164164
);
@@ -233,18 +233,20 @@ public function add() {
233233
$total_shipping_tax = (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'];
234234
}
235235

236-
$tax = new \WC_Order_Item_Tax();
237-
$tax->set_props(
238-
[
239-
'rate_code' => 'SF-VAT',
240-
'rate_id' => self::RATE_ID,
241-
'label' => __( 'VAT', 'shopping-feed' ),
242-
'tax_total' => $total_product_tax,
243-
'shipping_tax_total' => $total_shipping_tax,
244-
]
245-
);
246-
$tax->save();
247-
$wc_order->add_item( $tax );
236+
if ( $total_product_tax > 0 || $total_shipping_tax > 0 ) {
237+
$tax = new \WC_Order_Item_Tax();
238+
$tax->set_props(
239+
[
240+
'rate_code' => 'SF-VAT',
241+
'rate_id' => self::RATE_ID,
242+
'label' => __( 'VAT', 'shopping-feed' ),
243+
'tax_total' => $total_product_tax,
244+
'shipping_tax_total' => $total_shipping_tax,
245+
]
246+
);
247+
$tax->save();
248+
$wc_order->add_item( $tax );
249+
}
248250
}
249251

250252
$wc_order->set_status( $this->status->get_name(), $this->status->get_note() );

src/Orders/Order/Products.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function mapping_product( $sf_product ) {
101101
'quantity' => $sf_product_quantity,
102102
);
103103

104-
if ( $this->include_vat ) {
104+
if ( $this->include_vat && $sf_product->getTaxAmount() > 0 ) {
105105
$args['taxes'] = [
106106
'subtotal' => [
107107
Order::RATE_ID => $sf_product->getTaxAmount(),

src/Orders/Order/Shipping.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ class Shipping {
3434
*/
3535
private $total;
3636

37-
/**
38-
* @var bool $include_vat
39-
*/
37+
/** @var float $total_tax */
38+
private $total_tax;
39+
40+
/** @var bool $include_vat */
4041
private $include_vat;
4142

4243
/** @var array|\WC_Shipping_Rate $shipping_rate */
@@ -52,11 +53,12 @@ class Shipping {
5253
* @param bool $include_vat
5354
*/
5455
public function __construct( $sf_order, $include_vat = false ) {
55-
$this->sf_order = $sf_order;
56+
$this->sf_order = $sf_order;
5657
$this->include_vat = $include_vat;
5758

5859
$this->set_shipping_method_and_colis_number();
5960
$this->set_total();
61+
$this->set_total_tax();
6062
}
6163

6264
/**
@@ -101,11 +103,11 @@ private function set_shipping_rate() {
101103
$shipping_rate = $default_shipping_method;
102104
}
103105

104-
$vat = [];
105-
if ( $this->include_vat && isset( $this->sf_order->toArray()['additionalFields']['shipping_tax'] ) ) {
106-
$vat = [
106+
$taxes = [];
107+
if ( $this->include_vat && $this->get_total_tax() > 0 ) {
108+
$taxes = [
107109
'total' => [
108-
Order::RATE_ID => (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'],
110+
Order::RATE_ID => $this->get_total_tax(),
109111
],
110112
];
111113
}
@@ -114,7 +116,7 @@ private function set_shipping_rate() {
114116
$shipping_rate['method_rate_id'],
115117
$shipping_rate['method_title'],
116118
$this->get_total_shipping() ? $this->get_total_shipping() : ShoppingFeedHelper::get_sf_default_shipping_fees(),
117-
$vat,
119+
$taxes,
118120
$shipping_rate['method_rate_id'],
119121
$shipping_rate['method_id']
120122
);
@@ -123,19 +125,37 @@ private function set_shipping_rate() {
123125
}
124126

125127
/**
128+
* Get total shipping amount.
129+
*
126130
* @return float
127131
*/
128132
public function get_total() {
129133
return $this->total;
130134
}
131135

132136
/**
133-
* Set total
137+
* Set total shipping amount.
134138
*/
135139
public function set_total() {
136140
$this->total = $this->get_total_shipping();
137141
}
138142

143+
/**
144+
* Get total shipping tax amount.
145+
*
146+
* @return float
147+
*/
148+
public function get_total_tax() {
149+
return $this->total_tax;
150+
}
151+
152+
/**
153+
* Set total shipping tax amount.
154+
*/
155+
public function set_total_tax() {
156+
$this->total_tax = isset( $this->sf_order->toArray()['additionalFields']['shipping_tax'] ) ? (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'] : 0;
157+
}
158+
139159
/**
140160
* @return array|\WC_Shipping_Rate $shipping_rate
141161
*/

0 commit comments

Comments
 (0)