Skip to content

Commit

Permalink
allow checkout for orders with total = 0
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Nov 11, 2023
1 parent 3477845 commit 9952c98
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,3 @@ jobs:
rm -rf temp_dir
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


21 changes: 9 additions & 12 deletions includes/Gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
namespace WCPOS\WooCommercePOS;

class Gateways {


public function __construct() {
add_action( 'woocommerce_payment_gateways', array( $this, 'payment_gateways' ) );
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'available_payment_gateways' ), 99 );
Expand All @@ -23,7 +21,7 @@ public function __construct() {
* BEWARE: some gateways/themes/plugins call this very early on every page!!
* We cannot guarantee that $wp is set, so we cannot use woocommerce_pos_request.
*
* @param array | null $gateways
* @param null|array $gateways
*
* @return array
*/
Expand All @@ -45,13 +43,13 @@ public function payment_gateways( array $gateways ) {
/**
* Get available payment POS gateways,
* - Order and set default order
* - Also going to remove icons from the gateways
* - Also going to remove icons from the gateways.
*
* - NOTE: lots of plugins/themes call this filter and I can't guarantee that $gateways is an array
*
* @param array | null $gateways
* @param null|array $gateways
*
* @return array | null
* @return null|array
*/
public function available_payment_gateways( ?array $gateways ): ?array {
// early exit
Expand All @@ -60,8 +58,8 @@ public function available_payment_gateways( ?array $gateways ): ?array {
}

// use POS settings
$settings_service = new Services\Settings();
$settings = $settings_service->get_payment_gateways_settings();
$settings_service = new Services\Settings();
$settings = $settings_service->get_payment_gateways_settings();

// Get all payment gateways
$all_gateways = WC()->payment_gateways->payment_gateways;
Expand All @@ -73,17 +71,17 @@ public function available_payment_gateways( ?array $gateways ): ?array {
if ( isset( $settings['gateways'][ $gateway->id ]['title'] ) ) {
$gateway->title = $settings['gateways'][ $gateway->id ]['title'];
}
/**
/*
* There is an issue over-writing the description field because some gateways use this for info,
* eg: Account Funds uses it to show the current balance.
*/
// if ( isset( $settings['gateways'][ $gateway->id ]['description'] ) ) {
// $gateway->description = $settings['gateways'][ $gateway->id ]['description'];
// }

$gateway->icon = '';
$gateway->icon = '';
$gateway->enabled = 'yes';
$gateway->chosen = $gateway->id === $settings['default_gateway'];
$gateway->chosen = $gateway->id === $settings['default_gateway'];

$_available_gateways[ $gateway->id ] = $gateway;
}
Expand All @@ -96,5 +94,4 @@ public function available_payment_gateways( ?array $gateways ): ?array {

return $_available_gateways;
}

}
35 changes: 22 additions & 13 deletions includes/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ class Orders {
public function __construct() {
$this->register_order_status();
add_filter( 'wc_order_statuses', array( $this, 'wc_order_statuses' ), 10, 1 );
add_filter('woocommerce_valid_order_statuses_for_payment', array(
$this,
'valid_order_statuses_for_payment',
), 10, 2);
add_filter('woocommerce_valid_order_statuses_for_payment_complete', array(
$this,
'valid_order_statuses_for_payment_complete',
), 10, 2);
add_filter('woocommerce_payment_complete_order_status', array(
$this,
'payment_complete_order_status',
), 10, 3);

add_filter( 'woocommerce_order_needs_payment', array( $this, 'order_needs_payment' ), 10, 3 );
add_filter( 'woocommerce_valid_order_statuses_for_payment', array($this,'valid_order_statuses_for_payment' ), 10, 2);
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', array( $this, 'valid_order_statuses_for_payment_complete' ), 10, 2);
add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'payment_complete_order_status' ), 10, 3);

add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );

Expand Down Expand Up @@ -69,6 +60,24 @@ public function wc_order_statuses( array $order_statuses ): array {
return $order_statuses;
}

/**
* WooCommerce order-pay form won't allow processing of orders with total = 0.
*
* @param bool $needs_payment
* @param WC_Order $order
* @param array $valid_order_statuses
*
* @return bool
*/
public function order_needs_payment( bool $needs_payment, WC_Order $order, array $valid_order_statuses ): bool {
// If the order total is zero and status is a POS status, then allow payment to be taken, ie: Gift Card
if ( 0 == $order->get_total() && \in_array( $order->get_status(), array( 'pos-open', 'pos-partial' ), true ) ) {
return true;
}

return $needs_payment;
}

/**
* Note: the wc- prefix is not used here because it is added by WooCommerce.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Templates/Receipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function get_template(): void {
* @return null|mixed
*/
private function get_template_path( string $file_name ) {
/**
/*
* Filters the path to the receipt template file.
*
* @param {string} $path Full server path to the template file.
Expand Down
17 changes: 11 additions & 6 deletions includes/Templates/Received.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Received {
public function __construct( int $order_id ) {
$this->order_id = $order_id;

add_filter('show_admin_bar', '__return_false');
}
add_filter('show_admin_bar', '__return_false');
}


public function get_template(): void {
Expand All @@ -37,11 +37,16 @@ public function get_template(): void {
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
// }

$server = new Server();
$order_json = $server->wp_rest_request( '/wc/v3/orders/' . $this->order_id );
/**
* @TODO - this is a hack and needs to be fixed
*
* - hardcoding the rest endpoint is a receipe for disaster
*/
$server = new Server();
$order_json = $server->wp_rest_request( '/wcpos/v1/orders/' . $this->order_id );
$completed_status_setting = woocommerce_pos_get_settings( 'checkout', 'order_status' );
$completed_status = 'wc-' === substr( $completed_status_setting, 0, 3 ) ? substr( $completed_status_setting, 3 ) : $completed_status_setting;
$order_complete = $order->has_status( array( $completed_status, 'pos-partial' ) );
$completed_status = 'wc-' === substr( $completed_status_setting, 0, 3 ) ? substr( $completed_status_setting, 3 ) : $completed_status_setting;
$order_complete = $order->has_status( array( $completed_status, 'pos-partial' ) );

// @TODO - display message for errors

Expand Down

0 comments on commit 9952c98

Please sign in to comment.