2015-10-30 55 views
0

我想要刪除或添加一些運輸選項,具體取決於購物車中的產品類別。例如:我有一個來自X類的產品,我想將shipping-method-X添加到購物車。如果我有一個來自X類的產品,另一個來自另一個類,則shipping-method-X將被禁用。 下面的函數檢查數組是否包含ID = 49的類別,並且如果有另一個類別,但它不起作用。什麼也沒有發生在車:(請幫Woocommerce基於類別添加或隱藏運輸方法

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

function check_cart_for_share() { 

global $woocommerce; 
$cart = $woocommerce->cart->cart_contents; 

$found = false; 

    foreach ($woocommerce->cart->cart_contents as $key => $values) { 
     $terms = get_the_terms($values['product_id'], 'product_cat'); 

     $tmp = array(); 

     foreach ($terms as $term) { 

      array_push($tmp, $term->term_id); 
     } 
     array_unique($tmp); 

     if (sizeof($tmp) > 1 AND in_array('49', $tmp)) { 

     $found = true; 
    } 
} 

return $found; 

} 

function hide_shipping_based_on_tag($available_methods) { 

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

    // remove the method you want 
    unset($available_methods['flat_rate']); 
} 

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

} 

回答

0

可能你的產品僅在與49的ID的單一類別,讓您的if語句無效。您應該檢查0或多個條目。

if (sizeof($tmp) > 0 && in_array('49', $tmp)) { \\... it is in the array