From 80f877e1c5c7cbe7095c74a45e8cd681bce4e306 Mon Sep 17 00:00:00 2001 From: Programmer Per Date: Sat, 1 Oct 2022 11:40:43 +0200 Subject: [PATCH] fix: discounts (#7) --- changelog.readme | 3 + classes/class-ledyer-main.php | 2 +- .../requests/helpers/class-ledyer-cart.php | 77 ++++++++++--------- composer.json | 2 +- languages/ledyer-checkout-for-woocommerce.pot | 2 +- ledyer-checkout-for-woocommerce.php | 2 +- package.json | 2 +- 7 files changed, 50 insertions(+), 40 deletions(-) diff --git a/changelog.readme b/changelog.readme index eb5a2d2..61cab03 100644 --- a/changelog.readme +++ b/changelog.readme @@ -1,3 +1,6 @@ +## 1.0.3 +* Bugfix: repair discount/coupon functionality + ## 1.0.2 * Bugfix: billing and shipping address. Use streetAddress, not country diff --git a/classes/class-ledyer-main.php b/classes/class-ledyer-main.php index 041d072..725ebc9 100644 --- a/classes/class-ledyer-main.php +++ b/classes/class-ledyer-main.php @@ -51,7 +51,7 @@ class Ledyer_Checkout_For_WooCommerce { */ public $checkout; - const VERSION = '1.0.0'; + const VERSION = '1.0.3'; const SLUG = 'ledyer-checkout-for-woocommerce'; const SETTINGS = 'ledyer_checkout_for_woocommerce_settings'; diff --git a/classes/requests/helpers/class-ledyer-cart.php b/classes/requests/helpers/class-ledyer-cart.php index b3a9587..ce14b2a 100644 --- a/classes/requests/helpers/class-ledyer-cart.php +++ b/classes/requests/helpers/class-ledyer-cart.php @@ -163,7 +163,7 @@ public function process_cart() { 'description' => $this->get_item_name( $cart_item ), 'quantity' => $this->get_item_quantity( $cart_item ), 'unitPrice' => $this->get_item_price( $cart_item ), - 'unitDiscountAmount' => $this->get_item_discount_amount( $cart_item, $product ), + 'unitDiscountAmount' => $this->get_item_discount_amount( $cart_item, $product ) / $this->get_item_quantity( $cart_item ), 'vat' => $this->get_item_tax_rate( $cart_item, $product ), 'totalAmount' => $this->get_item_total_amount( $cart_item, $product ), 'totalVatAmount' => $this->get_item_tax_amount( $cart_item, $product ), @@ -241,9 +241,11 @@ public function process_shipping() { public function process_coupons() { if ( ! empty( WC()->cart->get_coupons() ) ) { foreach ( WC()->cart->get_coupons() as $coupon_key => $coupon ) { - $coupon_reference = ''; - $coupon_amount = 0; - $coupon_tax_amount = ''; + $coupon_description = 'Gift card'; + $coupon_reference = substr( (string) $coupon_key, 0, 64 ); + $coupon_amount = 0; + $coupon_discount_amount = 0; + $coupon_tax_amount = 0; // Smart coupons are processed as real line items, cart and product discounts sent for reference only. if ( 'smart_coupon' === $coupon->get_discount_type() ) { @@ -251,35 +253,40 @@ public function process_coupons() { // If Smart coupon is applied before tax calculation, // the sum is discounted from order lines so we send it as 0 for reference. if ( wc_tax_enabled() && 'yes' === $apply_before_tax ) { - $coupon_amount = 0; - $coupon_reference = __( 'Gift card', 'ledyer-checkout-for-woocommerce' ) . ' (amount: ' . WC()->cart->get_coupon_discount_amount( $coupon_key ) . ')'; + $coupon_amount = 0; + $coupon_description = __( 'Gift card', 'ledyer-checkout-for-woocommerce' ) . ' (amount: ' . WC()->cart->get_coupon_discount_amount( $coupon_key ) . ')'; } else { - $coupon_amount = - WC()->cart->get_coupon_discount_amount( $coupon_key ) * 100; - $coupon_reference = __( 'Gift card', 'ledyer-checkout-for-woocommerce' ); + $coupon_discount_amount = WC()->cart->get_coupon_discount_amount( $coupon_key ) * 100; + $coupon_amount = - WC()->cart->get_coupon_discount_amount( $coupon_key ) * 100; + $coupon_description = __( 'Discount', 'ledyer-checkout-for-woocommerce' ); } $coupon_tax_amount = - WC()->cart->get_coupon_discount_tax_amount( $coupon_key ) * 100; } + // Add separate discount line item, but only if it's a smart coupon or country is US. - if ( 'smart_coupon' !== $coupon->get_discount_type() ) { + if ( 'smart_coupon' === $coupon->get_discount_type() ) { $discount = array( 'type' => 'discount', - 'reference' => substr( (string) $coupon_key, 0, 64 ), - 'description' => $coupon_reference, + 'reference' => $coupon_reference, + 'description' => $coupon_description, 'quantity' => 1, - 'unitPrice' => $coupon_amount, - 'unitDiscountAmount' => 0, - 'vat' => 0, + 'unitPrice' => 0, + 'unitDiscountAmount' => $coupon_discount_amount, + 'vat' => 2500, 'totalAmount' => $coupon_amount, 'totalVatAmount' => - self::format_number( $coupon_tax_amount ), ); $this->order_lines[] = $discount; } - if ( 'smart_coupon' === $coupon->get_discount_type() ) { + // Standard cart, product, percentage discounts and gift cards end up here + // Discount coupons are created as a separate line item only for reference that a discount has been applied to product items + // The real discount will have been calculated and applied on each individual existing product order lines as unitDiscountAmount + if ( 'smart_coupon' !== $coupon->get_discount_type() ) { $discount = array( 'type' => 'giftCard', - 'reference' => substr( (string) $coupon_key, 0, 64 ), - 'description' => $coupon_reference, + 'reference' => $coupon_reference, + 'description' => $coupon_description . ': ' . $coupon_reference, 'quantity' => 1, 'unitPrice' => $coupon_amount, 'unitDiscountAmount' => 0, @@ -312,13 +319,13 @@ public function process_coupons() { $gift_card = array( 'type' => 'giftCard', 'reference' => $gift_card_code, - 'name' => __( 'Gift card', 'ledyer-checkout-for-woocommerce' ), + 'description' => __( 'Gift card', 'ledyer-checkout-for-woocommerce' ), 'quantity' => 1, - 'tax_rate' => 0, - 'total_discount_amount' => 0, - 'total_tax_amount' => 0, - 'unit_price' => $gift_card_amount, - 'total_amount' => $gift_card_amount, + 'vat' => 0, + 'unitDiscountAmount' => 0, + 'totalVatAmount' => 0, + 'unitPrice' => $gift_card_amount, + 'totalAmount' => $gift_card_amount, ); $this->order_lines[] = $gift_card; @@ -338,13 +345,13 @@ public function process_coupons() { $gift_card = array( 'type' => 'giftCard', 'reference' => $giftcard_sku, - 'name' => $label, + 'description' => $label, 'quantity' => 1, - 'unit_price' => $coupon_amount, - 'tax_rate' => 0, - 'total_amount' => $coupon_amount, - 'total_discount_amount' => 0, - 'total_tax_amount' => 0, + 'unitPrice' => $coupon_amount, + 'vat' => 0, + 'totalAmount' => $coupon_amount, + 'unitDiscountAmount' => 0, + 'totalVatAmount' => 0, ); $this->order_lines[] = $gift_card; } @@ -360,13 +367,13 @@ public function process_coupons() { $gift_card = array( 'type' => 'giftCard', 'reference' => $giftcard_sku, - 'name' => $label, + 'description' => $label, 'quantity' => 1, - 'unit_price' => $coupon_amount, - 'tax_rate' => 0, - 'total_amount' => $coupon_amount, - 'total_discount_amount' => 0, - 'total_tax_amount' => 0, + 'unitDiscountAmount' => $coupon_amount, + 'vat' => 0, + 'totalAmount' => $coupon_amount, + 'unitDiscountAmount' => 0, + 'totalVatAmount' => 0, ); $this->order_lines[] = $gift_card; } diff --git a/composer.json b/composer.json index 413366f..6aa5ea0 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "version": "1.0.2", + "version": "1.0.0", "name": "maksimer/ledyer-checkout-for-woocommerce", "type": "wordpress-plugin", "license": "GPLv2", diff --git a/languages/ledyer-checkout-for-woocommerce.pot b/languages/ledyer-checkout-for-woocommerce.pot index a667859..37a8ded 100644 --- a/languages/ledyer-checkout-for-woocommerce.pot +++ b/languages/ledyer-checkout-for-woocommerce.pot @@ -2,7 +2,7 @@ # This file is distributed under the same license as the Ledyer Checkout for WooCommerce plugin. msgid "" msgstr "" -"Project-Id-Version: Ledyer Checkout for WooCommerce 1.0.2\n" +"Project-Id-Version: Ledyer Checkout for WooCommerce 1.0.3\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/ledyer-checkout-for-woocommerce\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/ledyer-checkout-for-woocommerce.php b/ledyer-checkout-for-woocommerce.php index 8e71a64..3c33d6d 100644 --- a/ledyer-checkout-for-woocommerce.php +++ b/ledyer-checkout-for-woocommerce.php @@ -5,7 +5,7 @@ * Description: Ledyer Checkout payment gateway for WooCommerce. * Author: Maksimer * Author URI: https://www.maksimer.com/ - * Version: 1.0.2 + * Version: 1.0.3 * Text Domain: ledyer-checkout-for-woocommerce * Domain Path: /languages * diff --git a/package.json b/package.json index 3bf9d3a..574ef65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ledyer-checkout-for-woocommerce", - "version": "1.0.2", + "version": "1.0.0", "private": true, "description": "Ledyer Checkout payment gateway for WooCommerce developed and maintained by Maksimer AS", "author": "Maksimer AS",