2016-03-08 56 views
-1

我有這個代碼Woocommerce 2.5:隱藏送貨方式時免費送貨可用不工作了

/* !Hide Shipping Options Woocommerce */ 

add_filter('woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1); 

function check_cart_for_share() { 

// load the contents of the cart into an array. 
global $woocommerce; 
$cart = $woocommerce->cart->cart_contents; 

$found = false; 

// loop through the array looking for the tag you set. Switch to true if the tag is found. 
foreach ($cart as $array_item) { 
    $term_list = wp_get_post_terms($array_item['product_id'], 'product_tag', array("fields" => "names")); 

    if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want 

     $found = true; 
     break; 
    } 
} 

return $found; 

} 

function hide_shipping_based_on_tag($available_methods) { 

// use the function above to check the cart for the tag. 
if (check_cart_for_share()) { 

    // remove the rate you want 
    unset($available_methods['flat_rate']); // Replace "flat_rate" with the shipping option that you want to remove. 
} 

// return the available methods without the one you unset. 
return $available_methods; 

} 

在我function.php - 但因爲woocommerce 2.5它不工作了。 其他運輸方式需要隱藏免費送貨時可用。 任何想法?

回答

0

您正在使用用於WooCommerce 2.1及更低版本的掛鉤。

相反,使用woocommerce_package_rates過濾器..

0

此代碼的工作非常適合最新woocommerce:

add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2); 

function hide_shipping_when_free_is_available($rates, $package) { 

    // Only modify rates if free_shipping is present 
    if (isset($rates['free_shipping'])) { 

     // To unset a single rate/method, do the following. This example unsets flat_rate shipping 
     unset($rates['flat_rate']); 

     // To unset all methods except for free_shipping, do the following 
     $free_shipping   = $rates['free_shipping']; 
     $rates     = array(); 
     $rates['free_shipping'] = $free_shipping; 
    } 

    return $rates; 
} 

我必須取消設置固定的運費,你可以,如果你啓用了取消設置其他運輸方式。這段代碼適合我,我昨天試過了。