【问题标题】:How to Assign a Gateway Fee in WooCommerce to Multiple User Roles?如何将 WooCommerce 中的网关费用分配给多个用户角色?
【发布时间】:2020-09-11 04:42:42
【问题描述】:

我在 WooCommerce 上建立了一个在线商店,销售零售和批发。但是信用卡费用太贵了。我很乐意为我的零售客户支付信用卡费用,但如果我的经销商选择使用信用卡付款,我想向他们收取费用。

我设法提出了以下运行良好的代码。但是我有两种不同类型的经销商,所以我需要扩展代码,我不知道该怎么做。

我正在尝试扩展这段代码以包含三个不同的用户角色:

角色 1:未注册的批发商 角色 2:default_wholesaler 角色3:管理员

知道怎么做吗?

   add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );

function bbloomer_add_checkout_fee_for_gateway() {
    if ( ! current_user_can( 'wholesaler-non-vat-registered' ) )
    return;

  global $woocommerce;

  $chosen_gateway = $woocommerce->session->chosen_payment_method;

  if ( $chosen_gateway == 'cardgatecreditcard' ) {

    $percentage = 0.085;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
  $woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');

  }

}

此代码也有效,但它也会向已登录的客户添加信用卡费用。因此,该代码适用于客人,但一旦客户登录他们的帐户,他们就会被收取信用卡费用。

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
function bbloomer_add_checkout_fee_for_gateway() {
global $woocommerce;
$user_role = wp_get_current_user();
$order_fee_roles = array( 'customer', 'wholesale', 'administrator' );
if (! is_user_logged_in() && !in_array($order_fee_roles, $user_role->roles ))
return;
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( $chosen_gateway == 'cardgatecreditcard' ){
$percentage = 0.085;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
$woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');  
}}

add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' ); 
function bbloomer_refresh_checkout_on_payment_methods_change(){ ?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}

感谢 Sara McMahon,这就是解决方案。像魅力一样工作!

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

【问题讨论】:

    标签: php wordpress logic hook-woocommerce user-roles


    【解决方案1】:

    您对第二个代码的问题可能是您设置的特定条件。

    $order_fee_roles = array('客户', '批发', '管理员'); if (!is_user_logged_in() && !in_array($order_fee_roles, $user_role->roles ))

    您可以考虑将它们拆分,以便于测试。因此,首先检查用户是否已登录。然后在其中检查他们是否已登录,然后检查用户角色。

    您在设置条件时遇到的一个问题是,您说“如果用户未登录并且不是这些角色之一,则不要运行”。当用户注销时,他们没有角色,并且当用户登录时,此代码不适用。这就是它为所有人运行的原因。要设置条件以便您检查用户是否已登录,只需删除感叹号即可。这会将您的检查更改为“如果用户已登录并且不是以下角色之一”...

    【讨论】:

    • 谢谢@david-proctor 我认为我真正需要的逻辑如下:1)检查用户是否登录。2)检查他们是否是特定的用户角色。然后运行。如果1不满足,不要勾选2,不要运行。如果1和2不满足,不要运行。
    【解决方案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()))
            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');
        $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 );
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2023-02-14
      • 2022-11-27
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      相关资源
      最近更新 更多