1

我一直在使用此代码段一段时间,没有任何问题,直到我注意到客户能够通过信用卡付款而无需支付任何费用。他们设法通过“订单支付”页面付款。

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

// 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 );
        }
    }
}

4

1 回答 1

1

为了让您的代码在订单支付中有效,您可以在代码中替换:

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

经过:

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

现在您的函数代码也将在 WooCommerce Order Pay端点上执行……</p>

但是在Order Pay端点中,数据会保存到现有订单中,因此在结账时已经为该订单保存了费用。该钩子woocommerce_cart_calculate_fees仅影响购物车对象,对Order Pay 端点中的现有订单没有任何影响。

您将不得不专门为 Order Pay 重建更复杂的东西,当然涉及 Ajax,以便能够在现有订单中删除或添加您的自定义费用,刷新总数……</p>

于 2020-10-13T23:07:33.237 回答