@@ -41,9 +41,7 @@ public function createOrderLines( $order, $products = array(), $wcRefund = false
4141
4242 // generate order lines product by product
4343 foreach ( $ cartItems as $ key => $ item ) {
44- $ total = $ item ->get_total ();
45- $ subtotal = $ item ->get_subtotal ();
46- $ discount = 0 ;
44+ $ total = $ item ->get_total ();
4745
4846 if ( $ total == 0 && $ isSubscription ) {
4947 $ total = (float ) $ item ->get_product ()->get_regular_price ();
@@ -53,22 +51,14 @@ public function createOrderLines( $order, $products = array(), $wcRefund = false
5351 continue ;
5452 }
5553
56- $ discountPercentage = 0 ;
57- if ( $ subtotal > 0 ) {
58- $ discount = $ subtotal - $ total ;
59- $ discountPercentage = ( $ subtotal - $ total ) / $ subtotal * 100 ;
60- }
61- // get product details for each order line
62- $ productDetails = $ this ->getProductDetails ( $ item , $ discount , $ discountPercentage , ( ( $ i == 0 ) ? $ isSubscription : 0 ) );
63-
6454 if ( $ wcRefund ) {
6555 $ orderLines [ $ key ] = array (
6656 'qty ' => $ products ['itemQty ' ][ $ key ],
6757 'refund_total ' => ( $ item ['line_total ' ] / $ item ->get_quantity () ) * $ products ['itemQty ' ][ $ key ],
6858 'refund_tax ' => ( $ item ->get_total_tax () / $ item ->get_quantity () ) * $ products ['itemQty ' ][ $ key ],
6959 );
7060 } else {
71- $ orderLines [] = $ productDetails [ ' product ' ] ;
61+ $ orderLines [] = $ this -> getOrderLine ( $ item , ( ( $ i == 0 ) ? $ isSubscription : 0 ) ) ;
7262 }
7363
7464 $ i ++;
@@ -85,6 +75,20 @@ public function createOrderLines( $order, $products = array(), $wcRefund = false
8575 $ orderLines [] = $ shippingDetails ;
8676 }
8777
78+ if ( $ order ->get_total_discount () ) {
79+ $ orderLine = new OrderLine (
80+ 'Discount ' ,
81+ 'discount ' ,
82+ 1 ,
83+ -abs ( $ order ->get_total_discount () )
84+ );
85+ $ orderLine ->taxAmount = 0 ;
86+ $ orderLine ->unitCode = 'unit ' ;
87+ $ orderLine ->setGoodsType ( 'handling ' );
88+
89+ $ orderLines [] = $ orderLine ;
90+ }
91+
8892 if ( ! $ wcRefund ) {
8993 // Calculate compensation amount
9094 $ totalCompensationAmount = $ this ->totalCompensationAmount ( $ orderLines , $ order ->get_total () );
@@ -98,76 +102,36 @@ public function createOrderLines( $order, $products = array(), $wcRefund = false
98102 }
99103
100104 /**
101- * Returns product Details based on product type and tax configuration settings
105+ * Returns order line for the order item
102106 *
103107 * @param object $item
104- * @param float $discount
105- * @param float $discountPercentage
108+ * @param bool $isSubscription
106109 *
107- * @return array
110+ * @return OrderLine
108111 */
109- private function getProductDetails ( $ item , $ discount , $ discountPercentage , $ isSubscription = false ) {
110- $ product = $ item ->get_product ();
111- $ quantity = $ item ->get_quantity ();
112- $ regularPrice = (float ) $ product ->get_regular_price ();
113- $ salePrice = (float ) $ product ->get_sale_price ();
114- $ subtotal = $ item ->get_subtotal ();
115- $ taxRate = $ subtotal > 0 ? $ item ->get_subtotal_tax () / $ subtotal : 0 ;
116- $ productId = $ product ->get_sku () ? $ item ->get_id () . '- ' . $ product ->get_sku () : $ item ->get_id ();
117- $ productData = array ();
118- $ productDiscount = 0 ;
119-
120- if ( $ product ->get_type () === 'variable ' || $ product ->get_type () === 'variable-subscription ' ) {
121- $ variationId = (int ) $ item ->get_variation_id ();
122- $ product_data = wc_get_product ( $ variationId );
123- $ regularPrice = (float ) $ product_data ->get_regular_price ();
124- $ salePrice = (float ) $ product_data ->get_sale_price () ? $ product_data ->get_sale_price () : 0 ;
125- }
126-
127- // calculate discount if catalogue rule is applied on order line i.e. product sale price is set
128- if ( $ product ->is_on_sale () ) {
129- $ productDiscount = $ regularPrice - $ salePrice ;
130- // convert discount amount into percentage
131- $ discountPercentage += round ( ( $ productDiscount / $ regularPrice ) * 100 , 2 );
132- }
133-
134- if ( wc_prices_include_tax () ) {
135- $ taxRate = 1 + $ taxRate ;
136- $ productPrice = round ( $ regularPrice / $ taxRate , 2 );
137- $ taxAmount = $ regularPrice - $ productPrice ;
138- } else {
139- $ productPrice = $ regularPrice ;
140- $ taxAmount = $ productPrice * $ taxRate ;
141- }
142-
143- $ productPrice = $ isSubscription ? ( $ productPrice + ( $ taxAmount * $ quantity ) - ( $ discount + $ productDiscount ) ) : round ( $ productPrice , 2 );
144- $ taxPercent = $ productPrice > 0 ? round ( ( $ taxAmount / $ productPrice ) * 100 , 2 ) : 0 ;
145- $ taxAmount = round ( $ taxAmount * $ quantity , 2 );
146- $ discount = round ( $ discountPercentage , 2 );
112+ private function getOrderLine ( $ item , $ isSubscription = false ) {
113+ $ product = $ item ->get_product ();
114+ $ quantity = $ item ->get_quantity ();
147115
148116 // generate line date with all the calculated parameters
149117 $ orderLine = new OrderLine (
150118 $ item ->get_name (),
151- $ productId ,
119+ $ item -> get_id () ,
152120 $ quantity ,
153- round ( $ productPrice , 2 )
121+ round ( $ item -> get_subtotal () / $ quantity , 2 )
154122 );
155123 $ orderLine ->productUrl = $ product ->get_permalink ();
156124 $ orderLine ->imageUrl = wp_get_attachment_url ( $ product ->get_image_id () );
157125 $ orderLine ->unitCode = $ quantity > 1 ? 'units ' : 'unit ' ;
158126
159127 if ( ! $ isSubscription ) {
160- $ orderLine ->discount = $ discount ;
161- $ orderLine ->taxAmount = $ taxAmount ;
162- $ orderLine ->taxPercent = $ taxPercent ;
128+ $ orderLine ->taxAmount = round ( $ item ->get_total_tax (), 2 );
163129 }
164130
165131 $ goodsType = ( $ isSubscription ) ? 'subscription_model ' : 'item ' ;
166132 $ orderLine ->setGoodsType ( $ goodsType );
167133
168- $ productData ['product ' ] = $ orderLine ;
169-
170- return $ productData ;
134+ return $ orderLine ;
171135 }
172136
173137 /**
@@ -180,16 +144,14 @@ private function getProductDetails( $item, $discount, $discountPercentage, $isSu
180144 */
181145 public function compensationAmountOrderline ( $ productId , $ compensationAmount ) {
182146 // Generate compensation amount orderline for payment, capture and refund requests
183- $ orderLine = new OrderLine (
147+ $ orderLine = new OrderLine (
184148 'Compensation ' ,
185149 'comp- ' . $ productId ,
186150 1 ,
187151 $ compensationAmount
188152 );
189- $ orderLine ->taxAmount = 0.00 ;
190- $ orderLine ->taxPercent = 0.00 ;
191- $ orderLine ->unitCode = 'unit ' ;
192- $ orderLine ->discount = 0.00 ;
153+ $ orderLine ->taxAmount = 0.00 ;
154+ $ orderLine ->unitCode = 'unit ' ;
193155 $ orderLine ->setGoodsType ( 'handling ' );
194156
195157 return $ orderLine ;
@@ -233,16 +195,14 @@ private function getShippingDetails( $order, $products, $wcRefund ) {
233195 'refund_tax ' => wc_format_decimal ( $ totalShippingTax ),
234196 );
235197 } else {
236- $ orderLine = new OrderLine (
198+ $ orderLine = new OrderLine (
237199 $ order ->get_shipping_method (),
238200 $ shippingID ,
239201 1 ,
240202 round ( $ totalShipping , 2 )
241203 );
242- $ orderLine ->taxAmount = round ( $ totalShippingTax , 2 );
243- $ orderLine ->taxPercent = round ( ( $ totalShippingTax / $ totalShipping ) * 100 , 2 );
244- $ goodsType = 'shipment ' ;
245- $ orderLine ->setGoodsType ( $ goodsType );
204+ $ orderLine ->taxAmount = round ( $ totalShippingTax , 2 );
205+ $ orderLine ->setGoodsType ( 'shipment ' );
246206 $ shippingDetails [] = $ orderLine ;
247207 }
248208 }
0 commit comments