Woocommerce Custom shipping price for any shipping method

May 26, 2016 | Blog |

woocommerce_package_rates is a 2.1+ hook which allows you to modify the any shipping rate through hooks.

Modify the price for flat rate.

function kd_custom_flat_rate_cost( $rates, $package ) {
// Make sure flat rate is available
if ( isset( $rates['flat_rate'] ) ) {
// Set the cost to $50
$rates['flat_rate']->cost = 50;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'kd_custom_flat_rate_cost', 10, 2 );

Modify the price for flat rate if product exists.

function kd_custom_flat_rate_cost( $rates, $package ) {
// Make sure flat rate is available
if ( isset( $rates['flat_rate'] ) ) {
// Set the cost to $50
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->id ) {
$rates['flat_rate']->cost = 50;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'kd_custom_flat_rates_cost', 10, 2 );