根据用户角色为WooCommerce付款方式分配百分比费用 [英] Assign a percentage fee to WooCommerce payment methods based on user roles

查看:72
本文介绍了根据用户角色为WooCommerce付款方式分配百分比费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I expand this snippet to include 2 more payment gateways with different fees?

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

The payment gateways I want to add are 'cardgategiropay' and 'cardgateideal' and the fees are 3% and 2% respectively .


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

此代码可以正常工作.只是检查我的语法是否正确.

This code works fine. Just checking if my syntax is correct.


    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语句并进行优化和压缩代码,如下所示:

You could also array_intersect() to check user roles, use IF / ELSE statements, instead of only multiple IF statements and optimize and compact the code as follows:

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

还缺少一些东西,以允许在付款网关更改时进行刷新结帐:

Also something is missing to allow refresh checkout on payment gateway change:

// 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文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

相关:根据具体情况添加费用WooCommerce中的付款方式

这篇关于根据用户角色为WooCommerce付款方式分配百分比费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆