Snippet to add discount on shipping in WordPress.
/* Add code in your plugin "Snnipet".
add_filter( 'woocommerce_package_rates', 'fa_woocommerce_package_rates', 10, 2 );
function fa_woocommerce_package_rates( $rates, $package ) {
foreach ( $rates as $key => $rate ) {
$cost = $rate->get_cost();
// correios-pac is my method_id,
// checking the shipping id
if ( 'correios-pac' == $rate->get_method_id() ) {
$id = $rate->get_instance_id();
if ( 15 == $id || 17 == $id || 19 == $id || 21 == $id ){
$rate->set_cost( $cost * 0 ); // free shipping
}
else if( 13 == $id ){
$rate->set_cost( $cost * 0.5 ); // 0.5 = 50% discount
}
else if( 27 == $id ){
$rate->set_cost(( $cost * 67.7 ) / 100); // 15% discount
}
}
$rates[ $key ] = $rate;
}
return $rates;
}
After add to code in your plugin Snippet in WordPress, your shipping_method update cost for 0 (free shipping) - first condition , 50% discount - second condition, 30% discount -- third condition
/* Add code in your plugin "Snnipet".
add_filter( 'woocommerce_package_rates', 'fa_woocommerce_package_rates', 10, 2 );
function fa_woocommerce_package_rates( $rates, $package ) {
foreach ( $rates as $key => $rate ) {
$cost = $rate->get_cost();
// correios-pac is my method_id,
// checking the shipping id
if ( 'correios-pac' == $rate->get_method_id() ) {
$id = $rate->get_instance_id();
if ( 15 == $id || 17 == $id || 19 == $id || 21 == $id ){
$rate->set_cost( $cost * 0 ); // free shipping
}
else if( 13 == $id ){
$rate->set_cost( $cost * 0.5 ); // 0.5 = 50% discount
}
else if( 27 == $id ){
$rate->set_cost(( $cost * 67.7 ) / 100); // 15% discount
}
}
$rates[ $key ] = $rate;
}
return $rates;
}
After add to code in your plugin Snippet in WordPress, your shipping_method update cost for 0 (free shipping) - first condition , 50% discount - second condition, 30% discount -- third condition