I am using B2BKing and need assistance in creating a custom snippet that ensures orders placed by sub-accounts are automatically paid using the parent account’s credit line once they are approved.
Currently, sub-accounts can place orders that require parent approval, but I would like to automate the payment process so that when the parent account approves an order, the amount is immediately deducted from their available credit line without requiring manual intervention.
Could you please provide guidance or a code snippet to achieve this functionality?
The plugin by default does not have any features for that.
I tried writing a code snippet for it now, but please note this is experimental and I'd advise significantly testing this before using it on a production site.
The code snippet is:
add_action('b2bking_after_approve_order', function($orderobj){
$order_id = $orderobj->get_id();
$customer_id = $orderobj->get_customer_id();
$total = $orderobj->get_total();
$note = esc_html__('Paid for order','b2bkingcredit').' #'.$order_id;
// get user history
$user_credit_history = sanitize_text_field(get_user_meta($customer_id,'b2bking_user_credit_history', true));
// create transaction
$date = date_i18n( 'Y/m/d', time()+(get_option('gmt_offset')*3600) );
$operation = 'purchase';
$consumed_balance = get_user_meta($customer_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($customer_id,'b2bking_user_credit_history',$user_credit_history.';'.$transaction_new);
// update user consumed balance
update_user_meta($customer_id,'b2bking_user_credit_consumed_balance',$new_consumed_balance);
$consumed_balance = $new_consumed_balance;
if ($consumed_balance > 0){
$outstanding_balance = '-'.wc_price($consumed_balance);
} else if ($consumed_balance === 0){
$outstanding_balance = wc_price(0);
} else if ($consumed_balance < 0){
$outstanding_balance = wc_price(substr($consumed_balance,1)); // remove the minus
}
$order_note_for_backend = esc_html__('Paid for via Company Credit.','b2bkingcredit');
// add note
$order->add_order_note( $order_note_for_backend);
$order->update_meta_data('b2bking_credited_already', 'yes');
$order->update_meta_data('b2bking_modified_already', 'yes');
}, 10, 1);
This can be added to functions.php or any snippets plugin. It should automatically pay for the order using credit when the order is placed.
A potential issue that arises:
-> Normally the credit gateway would only be available if there's enough credit available (if the credit limit is sufficient). However since here it's automatic, there is no way to check, so the order would go through regardless of the credit limit.
When orders are approved they are automatically paid for using company credit.
If the above does not work for you for some reason, it would be great if we could troubleshoot it directly on the site. For that we would need a backend site or staging site access,
Hello,
I am using B2BKing and need assistance in creating a custom snippet that ensures orders placed by sub-accounts are automatically paid using the parent account’s credit line once they are approved.
Currently, sub-accounts can place orders that require parent approval, but I would like to automate the payment process so that when the parent account approves an order, the amount is immediately deducted from their available credit line without requiring manual intervention.
Could you please provide guidance or a code snippet to achieve this functionality?
Thank you for your support!
Hello Loranz,
The plugin by default does not have any features for that.
I tried writing a code snippet for it now, but please note this is experimental and I'd advise significantly testing this before using it on a production site.
The code snippet is:
This can be added to functions.php or any snippets plugin. It should automatically pay for the order using credit when the order is placed.
A potential issue that arises:
-> Normally the credit gateway would only be available if there's enough credit available (if the credit limit is sufficient). However since here it's automatic, there is no way to check, so the order would go through regardless of the credit limit.
Kind regards,
Stefan
Thanks, I understand the budget control problem... I'll try some tests! In the meantime, thank you very much!
I tried the script you indicated but unfortunately the double step remains
Hi again,
I re-reviewed that and indeed it looks like there was an issue in the code.
Please use this updated code snippet:
https://pastecode.io/s/6ucrfk9m
I have tested this on my local test site and it appears to work: https://www.loom.com/share/097910c0fe76466f928375f6e36acbd6?sid=7dd7a992-8ce4-4a20-851d-075bf8fac520
When orders are approved they are automatically paid for using company credit.
If the above does not work for you for some reason, it would be great if we could troubleshoot it directly on the site. For that we would need a backend site or staging site access,
Kind regards,
Stefan