2015-06-02 50 views
2

我想爲不同的用戶角色設置不同的最低訂單價格以免費送貨。如何爲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; 
} 

回答

相關問題