При оформлении товара мы можем задавать условия для вывода методов доставки, а также менять их стоимость.
add_filter('woocommerce_package_rates','test_overwrite_fedex', 100, 2);
function test_overwrite_fedex($rates, $package) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
global $woocommerce;
$cart_subtotal = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
foreach ($rates as $rate) {
if ($rate->id == 'flat_rate:14') {
if ($cart_subtotal >= 1000) {
$rate->cost = 0;
}
}
if ($rate->id == 'flat_rate:15') {
if ($cart_subtotal >= 5000) {
$rate->cost = 0;
}
}
/*if ($rate->id == 'free_shipping:11') {
unset($rates['free_shipping:11']);
}*/
}
return $rates;
}
В данном примере при достижении сумм 1000 и 5000 методы доставки становятся бесплатными. Закомментирован код в котором отключается метод доставки.
Можно также создать условие в зависимости от состава корзины, т.е. содержится ли в ней определенный товар:
add_filter('woocommerce_package_rates', 'product_hide_shipping_method', 10, 2);
function product_hide_shipping_method( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your Shipping Method ID to be removed
$shipping_method_id = 'flat_rate:12';
// HERE set your targeted product ID
$product_id = 37;
$found = false;
// Loop through cart items and checking for the specific product ID
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_id() == $product_id )
$found = true;
}
if( $found ){
// Loop through available shipping methods
foreach ( $rates as $rate_key => $rate ){
// Remove the specific shipping method
if( $shipping_method_id === $rate->id )
unset($rates[$rate->id]);
}
}
return $rates;
}