如何为woocommerce中的不同用户角色设置不同的最低订单金额免费送货

时间:2015-06-02 11:53:37

标签: php wordpress woocommerce

我想为不同的用户角色设置不同的最低订单价格以免费送货。

我该如何实施?

我目前正在使用以下代码。

add_filter( 'woocommerce_available_shipping_methods', 'filter_woocommerce_available_shipping_methods');

filter_woocommerce_available_shipping_methods()功能:

function filter_woocommerce_available_shipping_methods( $methods ) {

    global $woocommerce;
            get_currentuserinfo();
            global $current_user;
             $roles = $current_user->roles[0];
            if($roles == '')
                {
                    $roles = 'subscriber';
                }

    $shippingrate = new WC_Shipping_Rate();

    $shippingrate->id = 'free_shipping';

    $shippingrate->label = 'Kostenloser Versand';

    $shippingrate->cost = '0';

    $shippingrate->method_id = 'free_shipping';

     if ( $roles == 'corporate' && $woocommerce->cart->subtotal_ex_tax >= 50   )  {
      unset($methods);
      $methods = array();
      $methods['free_shipping'] = $shippingrate;
    }

   else if ( ( $roles == 'retail' && $woocommerce->cart->subtotal_ex_tax >= 80 ) || ( $roles == 'gastronomy' && $woocommerce->cart->subtotal_ex_tax >= 80) ) {
      unset($methods);
      $methods = array();
      $methods['free_shipping'] = $shippingrate;
    }

    else if( $roles == 'distributor' && $woocommerce->cart->subtotal_ex_tax >= 400 )  {
      unset($methods);
      $methods = array();
      $methods['free_shipping'] = $shippingrate;
    }

    else if( $roles == 'distributor' && $woocommerce->cart->subtotal_ex_tax >= 400 )  {
      unset($methods);
      $methods = array();
      $methods['free_shipping'] = $shippingrate;
    }

    else if( $roles == 'subscriber' && $woocommerce->cart->subtotal_ex_tax >= 45 )  {
      unset($methods);
      $methods = array();
      $methods['free_shipping'] = $shippingrate;
    }

    return $methods;
}

1 个答案:

答案 0 :(得分:0)

您是否考虑过使用插件?我创建了一个可以配置条件的插件,包括基于用户角色。

https://wordpress.org/plugins/woocommerce-advanced-free-shipping/