@@ -16,6 +16,7 @@ class LineItem extends AbstractService {
1616 private $ products ;
1717 private $ shipping ;
1818 private $ coupon ;
19+ private $ coupon_total ;
1920 private $ voucher ;
2021 private $ total ;
2122 private $ xfeepro ;
@@ -36,6 +37,7 @@ public function getReducedItemsFromOrder(array $order_info, $transaction_id, $sp
3637 $ this ->tax = \WalleeVersionHelper::newTax ($ this ->registry );
3738 $ this ->tax ->setShippingAddress ($ order_info ['shipping_country_id ' ], $ order_info ['shipping_zone_id ' ]);
3839 $ this ->tax ->setPaymentAddress ($ order_info ['payment_country_id ' ], $ order_info ['payment_zone_id ' ]);
40+ $ this ->coupon_total = 0 ;
3941
4042 \WalleeHelper::instance ($ this ->registry )->xfeeproDisableIncVat ();
4143 $ line_items = $ this ->getItemsFromOrder ($ order_info , $ transaction_id , $ space_id );
@@ -88,6 +90,7 @@ public function getItemsFromOrder(array $order_info){
8890 $ order_total = 0 ;
8991 $ items = array ();
9092
93+ $ this ->coupon_total = 0 ;
9194 $ this ->fixed_taxes = array ();
9295 $ this ->products = $ order_model ->getOrderProducts ($ order_info ['order_id ' ]);
9396 $ voucher = $ order_model ->getOrderVouchers ($ order_info ['order_id ' ]);
@@ -190,6 +193,11 @@ private function createLineItems($currency_code){
190193 $ calculated_total += $ item ->getAmountIncludingTax ();
191194 }
192195
196+ if ($ this ->coupon ) {
197+ $ items [] = $ item = $ this ->createLineItemFromCoupon ();
198+ $ calculated_total += $ item ->getAmountIncludingTax ();
199+ }
200+
193201 if ($ this ->shipping ) {
194202 $ items [] = $ item = $ this ->createLineItemFromShipping ();
195203 $ calculated_total += $ item ->getAmountIncludingTax ();
@@ -358,7 +366,13 @@ private function createLineItemFromProduct($product){
358366 elseif ($ this ->coupon ['type ' ] == 'P ' ) {
359367 $ discount = $ product ['total ' ] / 100 * $ this ->coupon ['discount ' ];
360368 }
361- $ amount_excluding_tax -= $ discount ;
369+ $ this ->coupon_total -= $ discount ;
370+ $ line_item ->setAttributes (array (
371+ "coupon " => new \Wallee \Sdk \Model \LineItemAttributeCreate (array (
372+ 'label ' => $ this ->coupon ['name ' ],
373+ 'value ' => $ discount
374+ ))
375+ ));
362376 }
363377
364378 $ line_item ->setName ($ product ['name ' ]);
@@ -403,8 +417,21 @@ private function createUniqueIdFromProduct($product){
403417 }
404418 return $ id ;
405419 }
406-
407- private function createLineItemFromVoucher ($ voucher , $ id ){
420+
421+ private function createLineItemFromCoupon (){
422+ $ line_item = new LineItemCreate ();
423+
424+ $ line_item ->setName ($ this ->coupon ['name ' ]);
425+ $ line_item ->setQuantity (1 );
426+ $ line_item ->setType (LineItemType::DISCOUNT );
427+ $ line_item ->setSKU ($ this ->coupon ['code ' ]);
428+ $ line_item ->setUniqueId ($ this ->coupon ['coupon_id ' ]);
429+ $ line_item ->setAmountIncludingTax (\WalleeHelper::instance ($ this ->registry )->formatAmount ($ this ->coupon_total ));
430+
431+ return $ this ->cleanLineItem ($ line_item );
432+ }
433+
434+ private function createLineItemFromVoucher (){
408435 $ line_item = new LineItemCreate ();
409436
410437 $ line_item ->setName ($ this ->voucher ['name ' ]);
0 commit comments