Untitled

 avatar
unknown
plain_text
a year ago
3.0 kB
6
Indexable

add_action( 'woocommerce_checkout_order_processed', 'b2bking_credit_on_purchase', 10, 3 );
function b2bking_credit_on_purchase($order_id, $posted_data = array(), $order = array()){

	$order = wc_get_order($order_id);
	$user_id = $order->get_customer_id();

	$total = round(floatval($order->get_total()*5/100), 2);

	$note = esc_html__('Credit points','b2bkingcredit').' #'.$order_id;
	// get user history
	$user_credit_history = sanitize_text_field(get_user_meta($user_id,'b2bking_user_credit_history', true));

	// create transaction
	$date = date_i18n( 'Y/m/d', time()+(get_option('gmt_offset')*3600) ); 
	$operation = 'reimburse';
	$consumed_balance = get_user_meta($user_id,'b2bking_user_credit_consumed_balance', true);
	$new_consumed_balance = floatval($consumed_balance) - floatval($total);		
	$transaction_new = $date.':'.$operation.':'.$total.':'.$new_consumed_balance.':'.$note;

	// update credit history
	update_user_meta($user_id,'b2bking_user_credit_history',$user_credit_history.';'.$transaction_new);
	// update user consumed balance
	update_user_meta($user_id,'b2bking_user_credit_consumed_balance',$new_consumed_balance);

	$order->update_meta_data('credit_given', 'yes');
	$order->update_meta_data('credit_amount', $total);
	$order->save();
}

add_action( 'woocommerce_order_status_changed', 'check_order_meta_on_status_change', 10, 4 );
function check_order_meta_on_status_change( $order_id, $old_status, $new_status, $order ) {
    // Check if the new status is one of the specified statuses
    if ( in_array( $new_status, array( 'cancelled', 'failed', 'refunded' ) ) ) {

    	$user_id = $order->get_customer_id();
        // Get the order meta to see if credit has already been given
        $credit_given = $order->get_meta( 'credit_given' );
        // Check if 'credit_given' is set to 'yes'
        if ( $credit_given == 'yes' ) {

        	$note = esc_html__('Credit removed','b2bkingcredit').' #'.$order_id;
        	// get user history
        	$user_credit_history = sanitize_text_field(get_user_meta($user_id,'b2bking_user_credit_history', true));
        	$total = $order->get_meta('credit_amount');

        	// create transaction
        	$date = date_i18n( 'Y/m/d', time()+(get_option('gmt_offset')*3600) ); 
        	$operation = 'purchase';
        	$consumed_balance = get_user_meta($user_id,'b2bking_user_credit_consumed_balance', true);
        	$new_consumed_balance = floatval($consumed_balance) + floatval($total);		
        	$transaction_new = $date.':'.$operation.':'.$total.':'.$new_consumed_balance.':'.$note;

        	// update credit history
        	update_user_meta($user_id,'b2bking_user_credit_history',$user_credit_history.';'.$transaction_new);
        	// update user consumed balance
        	update_user_meta($user_id,'b2bking_user_credit_consumed_balance',$new_consumed_balance);
            
            $order->update_meta_data('credit_given', 'no');
            $order->save();
        }
    }
}
Editor is loading...
Leave a Comment