允许 WooCommerce 网关百分比费用也适用于订单支付

Allow WooCommerce Gateway Percentage Fee to work also on order pay

我一直在使用此代码段一段时间,没有遇到任何问题,直到我注意到客户可以通过信用卡付款而无需支付任何费用。他们成功地通过“订单支付”页面进行了支付。

谁能告诉我为什么我的代码在订单支付页面上不起作用?它在结帐页面上运行良好。

// Assign Credit Card Gateway Percentage Fee to Wholesaler Profiles

add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway', 10, 1);
function sm_credit_card_fee_role_gateway($cart){
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if (!(is_checkout() && !is_wc_endpoint_url()))
        return;

    if (!is_user_logged_in())
        return;

    $user = wp_get_current_user();
    $roles = (array) $user->roles;
    $roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'shop_manager');
    $compare = array_diff($roles, $roles_to_check);

    if (empty($compare)){
        $payment_method = WC()->session->get('chosen_payment_method');
        if ($payment_method == 'cardgatecreditcard'){
            $percentage = 0.085;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
        }
    }
}

要让您的代码在订单支付中生效,您可以在代码中替换:

if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
    return;

作者:

if ( ! ( is_checkout() && ! is_wc_endpoint_url('order-received') ) )
    return;

现在您的功能代码也将在 WooCommerce Order Pay 端点上执行...

But in Order Pay endpoint, the data is saved to an existing order, so the fee is already saved for this order when order was placed in checkout. The hook woocommerce_cart_calculate_fees affect only the cart object and doesn't have any effect on an exiting order in Order Pay endpoint.

You will have to rebuilt something much more complicated specifically for Order Pay, involving certainly Ajax, to be able to remove or add your custom fee in the existing order, refreshing totals…