0

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.

ADv
  • 57
  • 7

1 Answers1

0

Add the follows code snippet in your active theme's functions.php -

function add_extra_woocommerce_shipping_rate_cost( $cost, $method ) {
    /* let's assume its applicable for flat rate and local pickup
     * And instance id for flat rate = 12
     * And instance id for local pickup = 14 
     * 
     */
    if ( in_array( $method->get_instance_id(), array( 12, 14) ) && WC()->cart ) {
        if ( get_field( 'multiplier', 'option' ) ) :
            $multiplier = get_field( 'multiplier', 'option' );
        else :
            $multiplier = 1;
        endif;

        $product_category_id = array( 86, 21, 47 ); // applicable categories
        $product_category_id_check = false;
        $items = WC()->cart->get_cart();
        foreach( $items as $item => $values ) { 
            $product_cats_ids = wp_get_post_terms($values['data']->get_id(),'product_cat',array('fields'=>'ids'));
            foreach ( $product_cats_ids as $id ) {
                if( in_array( $id, $product_category_id ) ) 
                    $product_category_id_check = true;
            }
        }
        if( $product_category_id_check ){
            $cost = $cost * $multiplier;
        }
    }
    return $cost;
}
add_filter( 'woocommerce_shipping_rate_cost', 'add_extra_woocommerce_shipping_rate_cost', 99, 2 );
itzmekhokan
  • 2,597
  • 2
  • 9
  • 9
  • But how can I provide conditions for certain shipping methods? I do not need to apply $multiplier to all, but only to shiptor-shiptor:14:delivery-point_shiptor_terminal + shiptor-shiptor:14:to-door_shiptor_courier + rpaefw_post_calc:16 (I have this condition in my code above) – ADv Aug 30 '19 at 12:56
  • Just do a check in first if statement with in array shipping method condition like ` in_array($method->get_instance_id(), array('for_ship1_id', 'for_ship2_id')` – itzmekhokan Aug 30 '19 at 13:04