Created
January 18, 2023 13:03
-
-
Save malsubrata/9c091f6c91de3faf535d67e17d6580a8 to your computer and use it in GitHub Desktop.
Auto deduct wallet balance while creating orders from backend.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_update_order', 'woocommerce_update_order_callback' ); | |
if ( ! function_exists( 'woocommerce_update_order_callback' ) ) { | |
/** | |
* Auto deduct wallet balance while creating orders from backend. | |
* | |
* @param integer $order_id Order ID. | |
* @return void | |
*/ | |
function woocommerce_update_order_callback( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( 'wallet' === $order->get_payment_method( 'edit' ) && ! $order->get_transaction_id( 'edit' ) && $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', array( 'on-hold', 'pending', 'failed', 'cancelled' ), $order ) ) ) { | |
if ( woo_wallet()->wallet->get_wallet_balance( $order->get_customer_id( 'edit' ), 'edit' ) >= $order->get_total( 'edit' ) ) { | |
$wallet_response = woo_wallet()->wallet->debit( $order->get_customer_id( 'edit' ), $order->get_total( 'edit' ), apply_filters( 'woo_wallet_order_payment_description', __( 'For order payment #', 'woo-wallet' ) . $order->get_order_number(), $order ) ); | |
if ( $wallet_response ) { | |
$order->set_transaction_id( $wallet_response ); | |
do_action( 'woo_wallet_payment_processed', $order_id, $wallet_response ); | |
$order->save(); | |
} else { | |
$order->add_order_note( __( 'Something went wrong with processing payment please try again.', 'woo-wallet' ) ); | |
} | |
} else { | |
$order->add_order_note( __( 'Insufficient wallet balance', 'woo-wallet' ) ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment