【问题标题】:Shipping discount based on user roles in Woocommerce基于 Woocommerce 中用户角色的运费折扣
【发布时间】:2018-11-19 18:00:14
【问题描述】:

我试图为functions.php实现一个代码sn-p,它将适用 当“管理员”角色并想要时,运费可享受 50% 的折扣 隐藏它进入免费送货模式。

它不像我想的那样工作。我做错了什么?

add_action( 'woocommerce_cart_calculate_fees','discount_based_on_user_role_and_payment', 20, 1 );
function discount_based_on_user_role_and_payment( $cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;
    $discount = WC()->cart->shipping_total/2;
if ( $discount >0 && !current_user_can('administrator') )
    return;
    $cart->add_fee( sprintf( __("Chiết khấu", "woocommerce")), -$discount, true );
}

谁能帮忙完成这个?或者至少指向正确的方向?

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

    如果您在添加费用之前添加return;,则不会发生任何事情。 而是使用这个稍作改动的代码版本:

    add_action( 'woocommerce_cart_calculate_fees','discount_based_on_user_role_and_payment', 20, 1 );
    function discount_based_on_user_role_and_payment( $cart) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return; 
    
        $discount = $cart->shipping_total / 2;
    
        if ( $discount > 0 && current_user_can('administrator') ) {
            $cart->add_fee( sprintf( __("Chiết khấu", "woocommerce")), -$discount, true );
        }
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2021-01-06
      • 2014-02-12
      • 2018-12-23
      • 2020-10-18
      • 2021-04-12
      • 1970-01-01
      • 2019-07-20
      • 2015-09-08
      相关资源
      最近更新 更多