根据用户角色为 WooCommerce 支付方式分配百分比费用

Assign a percentage fee to WooCommerce payment methods based on user roles

如何扩展此代码段以包含另外 2 个费用不同的支付网关?

我要添加的支付网关是'cardgategiropay'和'cardgateideal',费用分别是3%和2%。


    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('order-pay') ) )
        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 );
        }
    }
}

这段代码工作正常。只是检查我的语法是否正确。


    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 ($payment_method == 'cardgateideal'){
            $percentage = 0.02;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Card Fee (2%)', $surcharge, true );
        }
        if ($payment_method == 'cardgategiropay'){
            $percentage = 0.03;
            $surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
            $cart->add_fee( 'Card Fee (3%)', $surcharge, true );
        }
    }
}

您还可以array_intersect()检查用户角色,使用 IF / ELSE 语句,而不是仅使用多个 IF 语句,并优化和压缩代码如下:

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

    // Only on checkout page and logged in users
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() )
        return;

    $wpuser_object = wp_get_current_user();
    $allowed_roles = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'shop_manager');

    if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
        $payment_method = WC()->session->get('chosen_payment_method');

        if ( 'cardgatecreditcard' === $payment_method ){
            $percentage = 8.5;
        }
        elseif ( 'cardgateideal' === $payment_method ){
            $percentage = 2;
        }
        elseif ( 'cardgategiropay' === $payment_method ){
            $percentage = 3;
        }
    }
    if ( isset($percentage) ) {
        $surcharge = ($cart->cart_contents_total + $cart->shipping_total) * $percentage / 100;
        $cart->add_fee( sprintf( __('Card Fee (%s)', 'woocommerce'), $percentage . '%' ), $surcharge, true );
    }
}

还缺少允许在支付网关更改时刷新结帐的内容:

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    if ( ( is_checkout() && ! is_wc_endpoint_url() ) && is_user_logged_in() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。


相关: