Skip to content

Commit bac5433

Browse files
committed
- Fix: redirect to Thank You page if payment is no longer _pending_
- Add: option in plugin settings to enable immediate stock reduction or not. Best use with [WooCommerce Auto Restore Stock](https://wordpress.org/plugins/woocommerce-auto-restore-stock/) Signed-off-by: Abdus Fauzi <[email protected]>
1 parent 89a8c32 commit bac5433

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ Submit issue to this [this repo](https://github.com/abdusfauzi/exn-wc-billplz/is
3737

3838
Changelog
3939
------------
40-
[2016-05-24] -- **1.2.1**
40+
[2016-05-25] -- **1.3.0**
41+
- Fix: redirect to Thank You page if payment is no longer _pending_
42+
- Add: option in plugin settings to enable immediate stock reduction or not. Best use with [WooCommerce Auto Restore Stock](https://wordpress.org/plugins/woocommerce-auto-restore-stock/)
43+
44+
[2016-05-24] -- **1.2.1 - 1.3.2**
4145
- Fix: pay button from My Account page should go back to Billplz
4246

4347
[2016-05-24] -- **1.2.0**

class/class-exn-wc-billplz.php

+32-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __construct() {
3535
// Define user setting variables.
3636
$this->title = $this->settings['title'];
3737
$this->description = $this->settings['description'];
38+
$this->immediate_reduce_stock = $this->settings['immediate_reduce_stock'];
3839
$this->secret_key = $this->settings['secret_key'];
3940
$this->collection_id = $this->settings['collection_id'];
4041
$this->collection_id_created = $this->settings['collection_id_created'];
@@ -136,6 +137,13 @@ public function init_form_fields() {
136137
'default' => __( 'Pay through Malaysian online banking such as FPX, CIMBClicks, Maybank2u, RHB Now, Bank Islam, etc.', 'exn-wc-billplz' ),
137138
'desc_tip' => true
138139
),
140+
'immediate_reduce_stock' => array(
141+
'title' => __( 'Reduce Stock', 'exn-wc-billplz' ),
142+
'type' => 'checkbox',
143+
'description' => 'This will immediately reduce your stock, even though payment has not been made. (Order status Pending). Best to be used with this <a href="https://wordpress.org/plugins/woocommerce-auto-restore-stock/">WooCommerce Auto Restore Stock</a>',
144+
'label' => __( 'Enable Reduce Stock (Immediately!)', 'exn-wc-billplz' ),
145+
'default' => 'no'
146+
),
139147
'secret_key' => array(
140148
'title' => __( 'API Secret Key', 'exn-wc-billplz' ),
141149
'type' => 'text',
@@ -187,6 +195,15 @@ public function init_form_fields() {
187195
public function process_payment( $order_id ) {
188196
$order = new WC_Order( $order_id );
189197

198+
// Remove cart
199+
WC()->cart->empty_cart();
200+
201+
// Immediately reduce stock for reservation
202+
if ( $this->immediate_reduce_stock ) {
203+
// Reduce stock levels
204+
$order->reduce_order_stock();
205+
}
206+
190207
$total = $order->order_total;
191208
$decimals = get_option( 'woocommerce_price_num_decimals' );
192209

@@ -253,13 +270,15 @@ public function process_payment( $order_id ) {
253270

254271
update_post_meta( $order->id, '_transaction_id', $billplz->id );
255272

256-
// Remove cart
257-
WC()->cart->empty_cart();
258-
259273
return array(
260274
'result' => 'success',
261275
'redirect' => $billplz->url
262276
);
277+
} else {
278+
return array(
279+
'result' => 'failed',
280+
'redirect' => $order->get_checkout_payment_url( false )
281+
);
263282
}
264283
}
265284

@@ -282,7 +301,7 @@ public function receipt_page( $order_id ) {
282301
if ( !is_wp_error( $response ) ) {
283302
$this->update_order_status( $response, $order );
284303
}
285-
} elseif ( $order->status=='completed' ) {
304+
} else {
286305
wp_redirect( $order->get_checkout_order_received_url() );
287306
exit;
288307
}
@@ -338,8 +357,17 @@ public function update_order_status( $response, $order ) {
338357
if ( $billplz->paid ) {
339358
if ( $order->status=='pending' ) {
340359
$order->add_order_note( $callback . 'Billplz Payment Status: SUCCESSFUL'.'<br>Transaction ID: ' . $billplz->id . '<br/><a href="' . $billplz->url .'" class="button">View Bill</a>' );
360+
361+
if ( $this->immediate_reduce_stock ) {
362+
add_filter( 'woocommerce_payment_complete_reduce_order_stock', '__return_false' );
363+
}
364+
341365
$order->payment_complete( $billplz->id );
342366

367+
if ( $this->immediate_reduce_stock ) {
368+
remove_filter( 'woocommerce_payment_complete_reduce_order_stock', '__return_false' );
369+
}
370+
343371
if ( !$post_request ) {
344372
wp_redirect( $order->get_checkout_order_received_url() );
345373
exit;

exn-wc-billplz.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Description: The new payment gateway in Malaysia Asia to grow your business with Billplz payment solutions: FPX, Maybank, RHB, CIMB, Bank Islam, etc.
66
* Author: Exnano Creative
77
* Author URI: https://abdusfauzi.com
8-
* Version: 1.2.1
8+
* Version: 1.3.0
99
* Tested up to: 4.5.2
1010
* License: MIT
1111
* Text Domain: exn-wc-billplz

0 commit comments

Comments
 (0)