I'm trying to add extra cost (with $multiplier) to user selected shipping method based on product category.
So, I need to check if any of products in cart is on $product_category_id . And if so then I need to multiply selected shipping cost to $multiplier value.
This $multiplier should be applied to total shipping cost calculated by shipping method (not to every item in cart).
add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
global $woocommerce;
if ( get_field( 'multiplier', 'option' ) ) :
$multiplier = get_field( 'multiplier', 'option' );
else :
$multiplier = 1;
endif;
$product_category_id = array( 86, 21, 47 );
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
$product_category_id_check = false;
foreach( $products as $cart_item ){
$product_id = $cart_item['product_id'];
}
foreach ( $product_category_id as $key => $value ) {
if ( in_array( $value, $product_cats_ids ) ) {
$product_category_id_check = true;
}
}
if ( ! is_admin() && $product_category_id_check ) {
// If Shiptor 1
if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) {
$rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier;
}
// If Shiptor 2
if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) {
$rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier;
}
//If anothee shipping method
if ( isset( $rates['rpaefw_post_calc:16'] )) {
$rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier;
}
}
return $rates;
}
No result after executing the code. No $multiplier is applied shipping methods I provide in code.