Skip to content

Commit

Permalink
fix: discounts (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
perjo927 authored Oct 1, 2022
1 parent 53d24b6 commit 80f877e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
3 changes: 3 additions & 0 deletions changelog.readme
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.3
* Bugfix: repair discount/coupon functionality

## 1.0.2
* Bugfix: billing and shipping address. Use streetAddress, not country

Expand Down
2 changes: 1 addition & 1 deletion classes/class-ledyer-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
77 changes: 42 additions & 35 deletions classes/requests/helpers/class-ledyer-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -241,45 +241,52 @@ 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() ) {
$apply_before_tax = get_option( 'woocommerce_smart_coupon_apply_before_tax', 'no' );
// 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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.2",
"version": "1.0.0",
"name": "maksimer/ledyer-checkout-for-woocommerce",
"type": "wordpress-plugin",
"license": "GPLv2",
Expand Down
2 changes: 1 addition & 1 deletion languages/ledyer-checkout-for-woocommerce.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
2 changes: 1 addition & 1 deletion ledyer-checkout-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 80f877e

Please sign in to comment.