@@ -155,29 +155,32 @@ public static function addProducts()
155155 */
156156 public static function getProducts ($ shippingMethodArr )
157157 {
158- $ tax = new WC_Tax ();
159158 $ products = [];
160159 $ orderDiscount = 0 ;
161160
162- foreach (WC ()->cart ->get_cart () as $ cart_item ) {
161+ foreach (WC ()->cart ->get_cart () as $ itemNumber => $ cart_item ) {
163162 $ product = $ cart_item ['data ' ];
164163 $ product_id = $ cart_item ['product_id ' ];
165164 $ variation_id = $ cart_item ['variation_id ' ];
166- $ name = $ product ->get_title ();
167- $ quantity = $ cart_item ['quantity ' ];
168- $ price = $ product ->get_price ();
169- $ tax_percentage = 0 ;
170- $ taxes = $ tax ->get_rates ($ product ->get_tax_class ());
171- if (!empty ($ taxes )) {
172- $ rates = array_shift ($ taxes );
173- $ tax_percentage = round (array_shift ($ rates ));
174- }
165+
166+ $ line_total = $ cart_item ['line_total ' ]; // excl tax
167+ $ line_tax = $ cart_item ['line_tax ' ]; // tax amount
168+ $ qty = $ cart_item ['quantity ' ];
169+
170+ $ price_ex_tax = $ line_total / $ qty ;
171+ $ tax_per_unit = $ line_tax / $ qty ;
172+ $ price_inc_tax = $ price_ex_tax + $ tax_per_unit ;
173+
174+ $ tax_percentage = $ price_ex_tax > 0
175+ ? round (($ tax_per_unit / $ price_ex_tax ) * 100 , 2 )
176+ : 0 ;
177+
175178 $ products [] = [
176- 'id ' => $ product_id ,
179+ 'id ' => empty ( $ product_id ) ? ' p ' . $ itemNumber : $ product_id ,
177180 'variation_id ' => $ variation_id ,
178- 'name ' => $ name ,
179- 'qty ' => $ quantity ,
180- 'amount ' => $ price ,
181+ 'name ' => $ product -> get_title () ,
182+ 'qty ' => $ cart_item [ ' quantity ' ] ,
183+ 'amount ' => round ( $ price_inc_tax , 2 ) ,
181184 'taxPercentage ' => $ tax_percentage ,
182185 'type ' => 'ARTICLE ' ,
183186 'currency ' => get_woocommerce_currency ()
@@ -215,15 +218,20 @@ public static function getProducts($shippingMethodArr)
215218
216219 if (!empty ($ shippingMethodArr ['shippingMethod ' ])) {
217220 $ shippingMethod = $ shippingMethodArr ['shippingMethod ' ];
218- $ shippingAmount = WC ()->cart ->get_shipping_total () + (float ) array_shift ($ shippingMethod ->get_taxes ());
221+ $ shippingAmount = WC ()->cart ->get_shipping_total () + (float )array_shift ($ shippingMethod ->get_taxes ());
222+
219223 if ($ shippingAmount > 0 ) {
224+ $ shipTotal = WC ()->cart ->get_shipping_total ();
225+ $ shipTax = WC ()->cart ->get_shipping_tax ();
226+ $ vatPerc = round ((100 * $ shipTax ) / $ shipTotal );
227+
220228 $ products [] = [
221- 'id ' => $ shippingMethodArr [ ' shippingMethodId ' ] ?? 0 ,
229+ 'id ' => ' shipping ' ,
222230 'name ' => $ shippingMethod ->label ,
223231 'qty ' => 1 ,
224232 'amount ' => $ shippingAmount ,
225233 'amountExclTax ' => WC ()->cart ->get_shipping_total (),
226- 'taxPercentage ' => round (\ Paynl \Helper:: calculateTaxPercentage ( $ shippingAmount , array_shift ( $ shippingMethod -> get_taxes ()))), // phpcs:ignore
234+ 'taxPercentage ' => $ vatPerc ,
227235 'type ' => 'SHIPPING ' ,
228236 'currency ' => get_woocommerce_currency ()
229237 ];
0 commit comments