B2BKing partial Company Credit transaction example

Example helper for recording a partial B2BKing Company Credit transaction from a custom checkout flow. Test and adapt it on a staging site first.
Anonymous
php
09/27/2026 5:38 PM
3.7 KB
7
Indexable
/**
 * Record part of an order total as a Company Credit transaction.
 *
 * Call this only after the order exists and the credit amount is final.
 */
function recharged_record_partial_company_credit( $order_id, $credit_amount ) {
    $order = wc_get_order( $order_id );
    $credit_amount = (float) wc_format_decimal( $credit_amount );

    if ( ! $order || $credit_amount <= 0 ) {
        return new WP_Error(
            'invalid_credit_transaction',
            'Invalid order or credit amount.'
        );
    }

    // Prevent the same order from being recorded more than once.
    if ( '' !== $order->get_meta( '_recharged_partial_credit_amount' ) ) {
        return true;
    }

    $original_user_id = (int) $order->get_customer_id();
    $credit_user_id = $original_user_id;

    // Use the parent company's credit balance for a subaccount order.
    $is_subaccount = 'subaccount' === get_user_meta(
        $credit_user_id,
        'b2bking_account_type',
        true
    );
    $uses_own_credit = apply_filters(
        'b2bking_subaccount_use_own_credit',
        false,
        $credit_user_id
    );

    if ( $is_subaccount && ! $uses_own_credit ) {
        $credit_user_id = (int) get_user_meta(
            $credit_user_id,
            'b2bking_account_parent',
            true
        );
    }

    if ( ! $credit_user_id ) {
        return new WP_Error(
            'invalid_credit_user',
            'No Company Credit account was found for this order.'
        );
    }

    $consumed_balance = (float) get_user_meta(
        $credit_user_id,
        'b2bking_user_credit_consumed_balance',
        true
    );
    $new_balance = $consumed_balance + $credit_amount;
    $note = 'Partial Company Credit used for order #' . $order->get_id();

    $transaction = implode(
        ':',
        array(
            wp_date( 'Y/m/d' ),
            'purchase',
            wc_format_decimal( $credit_amount ),
            wc_format_decimal( $new_balance ),
            $note,
            $original_user_id,
        )
    );

    $history = sanitize_text_field(
        get_user_meta(
            $credit_user_id,
            'b2bking_user_credit_history',
            true
        )
    );

    update_user_meta(
        $credit_user_id,
        'b2bking_user_credit_history',
        $history . ';' . $transaction
    );
    update_user_meta(
        $credit_user_id,
        'b2bking_user_credit_consumed_balance',
        $new_balance
    );

    $order->update_meta_data(
        '_recharged_partial_credit_amount',
        $credit_amount
    );
    $order->add_order_note( 'Partial Company Credit amount recorded.' );
    $order->save();

    return true;
}

// Example call from the existing custom checkout function:
// $result = recharged_record_partial_company_credit( $order_id, $credit_amount );
Editor is loading...
Leave a Comment