【问题标题】:Assign a percentage fee to WooCommerce payment methods based on user roles根据用户角色为 WooCommerce 付款方式分配百分比费用
【发布时间】:2020-12-19 23:10:54
【问题描述】:

如何扩展此 sn-p 以包含另外 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 );
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce payment-method


    【解决方案1】:

    您也可以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 文件中。经过测试并且可以工作。


    相关:Add fee based on specific payment methods in WooCommerce

    【讨论】:

    • 谢谢。效果很好。我可以通过向每个支付网关添加描述来扩展它,以便费用描述与支付网关匹配。我已将代码包含在原始问题中。
    • @WanderlustConsulting 该规则当时是 StackOverFlow 上的一个问题……您不能包含有效的答案代码来编辑您的问题。您需要在这里提出一个新问题,并附上我的答案的链接。
    猜你喜欢
    • 2018-12-23
    • 2016-11-17
    • 2019-02-07
    • 1970-01-01
    • 2013-05-03
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多