Skip to content

Commit eae0e88

Browse files
author
root
committed
Release 2.1.9
1 parent 0e8426d commit eae0e88

11 files changed

+325
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This repository contains the PostFinance Checkout plugin that enables WooCommerc
1515

1616
## Documentation
1717

18-
* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.8/docs/en/documentation.html)
18+
* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.9/docs/en/documentation.html)
1919

2020
## Support
2121

@@ -24,4 +24,4 @@ Support queries can be issued on the [PostFinance Checkout support site](https:/
2424

2525
## License
2626

27-
Please see the [license file](https://github.com/pfpayments/woocommerce/blob/2.1.8/LICENSE) for more information.
27+
Please see the [license file](https://github.com/pfpayments/woocommerce/blob/2.1.9/LICENSE) for more information.

changelog.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,14 @@ Tested against:
527527
- Woocommerce 7.5.0
528528
- PHP SDK 3.2.0
529529

530+
= 2.1.9 - March 22, 2023 =
531+
532+
## Feature
533+
- Smart Coupons Compatibility
534+
535+
## Tested Against
536+
- PHP 8.1
537+
- Wordpress 6.1.1
538+
- Woocommerce 7.5.0
539+
- PHP SDK 3.2.0
540+

docs/en/documentation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h2>Documentation</h2> </div>
2222
</a>
2323
</li>
2424
<li>
25-
<a href="https://github.com/pfpayments/woocommerce/releases/tag/2.1.8/">
25+
<a href="https://github.com/pfpayments/woocommerce/releases/tag/2.1.9/">
2626
Source
2727
</a>
2828
</li>

includes/admin/class-wc-postfinancecheckout-admin-settings-page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function get_settings() {
187187
$settings = array(
188188
array(
189189
'links' => array(
190-
'https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.8/docs/en/documentation.html' => __( 'Documentation', 'woo-postfinancecheckout' ),
190+
'https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.9/docs/en/documentation.html' => __( 'Documentation', 'woo-postfinancecheckout' ),
191191
'https://checkout.postfinance.ch/en-ch/user/signup' => __( 'Sign Up', 'woo-postfinancecheckout' ),
192192
),
193193
'type' => 'postfinancecheckout_links',

includes/admin/class-wc-postfinancecheckout-admin.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function init_hooks() {
9999
'woocommerce_hidden_order_itemmeta',
100100
array(
101101
$this,
102-
'hide_order_unique_id_meta',
102+
'hide_postfinancecheckout_order_item_meta',
103103
),
104104
10,
105105
1
@@ -270,13 +270,16 @@ public function enque_script_and_css() {
270270
}
271271

272272
/**
273-
* Hide order unique id meta
273+
* Hide postfinancecheckout order item meta
274274
*
275275
* @param array $arr array.
276276
* @return array
277277
*/
278-
public function hide_order_unique_id_meta( $arr ) {
278+
public function hide_postfinancecheckout_order_item_meta( $arr ) {
279279
$arr[] = '_postfinancecheckout_unique_line_item_id';
280+
$arr[] = '_postfinancecheckout_coupon_discount_line_item_id';
281+
$arr[] = '_postfinancecheckout_coupon_discount_line_item_key';
282+
$arr[] = '_postfinancecheckout_coupon_discount_line_item_discounts';
280283
return $arr;
281284
}
282285

includes/class-wc-postfinancecheckout-helper.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,30 @@ public function get_currency_fraction_digits( $currency_code ) {
200200
}
201201
}
202202

203-
204203
/**
205204
* Get total amount including tax.
206205
*
207206
* @param array $line_items line items.
207+
* @param bool $excludeDiscounts exclude discounts.
208208
* @return int
209209
*/
210-
public function get_total_amount_including_tax( array $line_items ) {
210+
public function get_total_amount_including_tax( array $line_items, bool $excludeDiscounts = false) {
211211
$sum = 0;
212+
/** @var \PostFinanceCheckout\Sdk\Model\LineItemCreate $line_item */
212213
foreach ( $line_items as $line_item ) {
213-
$sum += $line_item->getAmountIncludingTax();
214+
$type = $line_item->getType();
215+
$name = $line_item->getName();
216+
217+
if ( $excludeDiscounts && $type === \PostFinanceCheckout\Sdk\Model\LineItemType::DISCOUNT && strpos( $name, WC_PostFinanceCheckout_Packages_Coupon_Discount::COUPON ) !== false ) {
218+
//convert negative values to positive in order to be able to subtract it
219+
$sum -= abs( $line_item->getAmountIncludingTax() );
220+
} else {
221+
$sum += $line_item->getAmountIncludingTax();
222+
}
214223
}
215224
return $sum;
216225
}
217226

218-
219227
/**
220228
* Cleanup line items.
221229
*
@@ -226,8 +234,11 @@ public function get_total_amount_including_tax( array $line_items ) {
226234
* @throws WC_PostFinanceCheckout_Exception_Invalid_Transaction_Amount WC_PostFinanceCheckout_Exception_Invalid_Transaction_Amount.
227235
*/
228236
public function cleanup_line_items( array $line_items, $expected_sum, $currency ) {
229-
$effective_sum = $this->round_amount( $this->get_total_amount_including_tax( $line_items ), $currency );
237+
//ensure that the effective sum coincides with the total discounted by the coupons
238+
$has_coupons = apply_filters( 'wc_postfinancecheckout_packages_coupon_cart_has_coupon_discounts_applied', $currency );
239+
$effective_sum = $this->round_amount( $this->get_total_amount_including_tax( $line_items, $has_coupons ), $currency );
230240
$rounded_expected_sum = $this->round_amount( $expected_sum, $currency );
241+
231242
$inconsistent_amount = $rounded_expected_sum - $effective_sum;
232243
if ( 0 != $inconsistent_amount ) {
233244
$enforce_consistency = get_option( WooCommerce_PostFinanceCheckout::CK_ENFORCE_CONSISTENCY );
@@ -314,7 +325,7 @@ public function get_reduction_amount( array $line_items, array $reductions ) {
314325
* @param mixed $currency_code currency code.
315326
* @return float
316327
*/
317-
private function round_amount( $amount, $currency_code ) {
328+
public function round_amount( $amount, $currency_code ) {
318329
return round( $amount, $this->get_currency_fraction_digits( $currency_code ) );
319330
}
320331

includes/class-wc-postfinancecheckout-migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public static function check_version() {
248248
public static function plugin_row_meta( $links, $file ) {
249249
if ( WC_POSTFINANCECHECKOUT_PLUGIN_BASENAME === $file ) {
250250
$row_meta = array(
251-
'docs' => '<a href="https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.8/docs/en/documentation.html" aria-label="' . esc_attr__( 'View Documentation', 'woo-postfinancecheckout' ) . '">' . esc_html__( 'Documentation', 'woo-postfinancecheckout' ) . '</a>',
251+
'docs' => '<a href="https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/2.1.9/docs/en/documentation.html" aria-label="' . esc_attr__( 'View Documentation', 'woo-postfinancecheckout' ) . '">' . esc_html__( 'Documentation', 'woo-postfinancecheckout' ) . '</a>',
252252
);
253253

254254
return array_merge( $links, $row_meta );
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/**
4+
* WC_PostFinanceCheckout_Packages_Coupon_Discount Class
5+
*
6+
* PostFinanceCheckout
7+
* This plugin will add support for all PostFinanceCheckout payments methods and connect the PostFinanceCheckout servers to your WooCommerce webshop (https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html).
8+
*
9+
* @category Class
10+
* @package PostFinanceCheckout
11+
* @author wallee AG (http://www.wallee.com/)
12+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
13+
*/
14+
15+
if ( ! defined( 'ABSPATH' ) ) {
16+
exit();
17+
}
18+
19+
/**
20+
* Class WC_PostFinanceCheckout_Packages_Coupon_Discount.
21+
*
22+
* This class handles the required unique ids.
23+
*/
24+
class WC_PostFinanceCheckout_Packages_Coupon_Discount {
25+
26+
const COUPON = 'Coupon';
27+
28+
/**
29+
* Register item coupon discount functions hooks.
30+
*/
31+
public static function init() {
32+
add_filter(
33+
'woocommerce_checkout_create_order_line_item',
34+
array(
35+
__CLASS__,
36+
'copy_total_coupon_discount_meta_data_to_order_item',
37+
),
38+
10,
39+
4
40+
);
41+
add_filter(
42+
'wc_postfinancecheckout_packages_coupon_discount_totals_including_tax',
43+
array(
44+
__CLASS__,
45+
'get_coupons_discount_totals_including_tax',
46+
),
47+
10,
48+
1
49+
);
50+
add_filter(
51+
'wc_postfinancecheckout_packages_coupon_cart_has_coupon_discounts_applied',
52+
array(
53+
__CLASS__,
54+
'has_cart_coupon_discounts_applied',
55+
),
56+
10,
57+
1
58+
);
59+
add_filter(
60+
'wc_postfinancecheckout_packages_coupon_discounts_applied_by_cart',
61+
array(
62+
__CLASS__,
63+
'get_coupon_discounts_applied_by_cart',
64+
),
65+
10,
66+
1
67+
);
68+
add_filter(
69+
'wc_postfinancecheckout_packages_coupon_percentage_discounts_by_item',
70+
array(
71+
__CLASS__,
72+
'get_coupon_percentage_discount_by_item',
73+
),
74+
10,
75+
1
76+
);
77+
}
78+
79+
/**
80+
* Store cart item total coupon discounts to order item.
81+
*
82+
* @param WC_Order_Item_Product $item item.
83+
* @param mixed $cart_item_key cart_item_key.
84+
* @param mixed $values values.
85+
* @param WC_Order $order order.
86+
*
87+
* @return WC_Order_Item_Product $item item
88+
* @throws Exception
89+
*/
90+
public static function copy_total_coupon_discount_meta_data_to_order_item( WC_Order_Item_Product $item, $cart_item_key, $values, WC_Order $order = null ) {
91+
$coupon_discount = apply_filters('wc_postfinancecheckout_packages_coupon_percentage_discounts_by_item', $cart_item_key );
92+
$item->add_meta_data( '_postfinancecheckout_coupon_discount_line_item_discounts', $coupon_discount );
93+
return $item;
94+
}
95+
96+
/**
97+
* Get the total coupons discounts amount applied to the cart.
98+
*
99+
* @param $currency
100+
* @return float|int
101+
*/
102+
public static function get_coupons_discount_totals_including_tax( $currency ) {
103+
$coupons_discount_total = 0;
104+
foreach (WC()->cart->get_coupon_discount_totals() as $coupon_discount_total) {
105+
$coupons_discount_total += WC_PostFinanceCheckout_Helper::instance()->round_amount( $coupon_discount_total, $currency );
106+
}
107+
108+
return $coupons_discount_total;
109+
}
110+
111+
/**
112+
* Check if the cart has any coupons.
113+
*
114+
* @param $currency
115+
* @return bool
116+
*/
117+
public static function has_cart_coupon_discounts_applied( $currency ) {
118+
$discount = apply_filters( 'wc_postfinancecheckout_packages_coupon_discount_totals_including_tax', $currency );
119+
return $discount > 0;
120+
}
121+
122+
/**
123+
* Get coupons with their discount applied per item based on quantity.
124+
*
125+
* @param WC_Cart $cart
126+
* @return array|array[]
127+
* @throws Exception
128+
*/
129+
public static function get_coupon_discounts_applied_by_cart( WC_Cart $cart ) {
130+
if ( empty( $cart->get_coupons() ) ) {
131+
return array();
132+
}
133+
134+
$cart_cloned = clone $cart;
135+
$wp_discount = new WC_Discounts( $cart );
136+
137+
foreach ( $cart_cloned->get_coupons() as $code => $coupon ) {
138+
$wc_coupon = new WC_Coupon( $code );
139+
$wp_discount->apply_coupon( $wc_coupon );
140+
}
141+
142+
return $wp_discount->get_discounts();
143+
}
144+
145+
/**
146+
* Get percentage coupon discount per item based on quantity.
147+
*
148+
* @param string $item_key
149+
* @return int|mixed
150+
* @throws Exception
151+
*/
152+
public static function get_coupon_percentage_discount_by_item( string $item_key ) {
153+
$coupon_discounts_applied = apply_filters( 'wc_postfinancecheckout_packages_coupon_discounts_applied_by_cart', WC()->cart );
154+
$coupon_percentage_discount = 0;
155+
foreach ( $coupon_discounts_applied as $discounts ) {
156+
if ( !empty( $discounts[ $item_key ] ) ) {
157+
$coupon_percentage_discount += $discounts[ $item_key ];
158+
}
159+
}
160+
return $coupon_percentage_discount;
161+
}
162+
}
163+
164+
WC_PostFinanceCheckout_Packages_Coupon_Discount::init();

0 commit comments

Comments
 (0)