Woocommerce free shipping method for some products

I have a question I have 2 packages in woocommerce which i had made. 1 class is for threads another is for everything else in the store. What i am trying to achieve is this:

Threads shipping class is free shipping if the subtotal for that class is over 25.00

Every other product is flat rate shipping and shouldnt be affect by the free shipping.

As of now i cannot get any code or plugin to work because it is taking the sub total amount from the whole order and that is make the entire order free shipping. which is not what i want it to do.

My code as of now is

add_filter( ‘woocommerce_cart_shipping_packages’, ‘threads_woocommerce_cart_shipping_packages’ );

function threads_woocommerce_cart_shipping_packages( $packages ) {
// Reset the packages
$packages = array();

// threads items
$threads_items = array();
$regular_items = array();

// Sort threads from regular
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item[‘data’]->needs_shipping() ) {
if ( $item[‘data’]->get_shipping_class() == ‘threads’ ) {
$threads_items[] = $item;
} else {
$regular_items[] = $item;
}
}
}

// Put inside packages
if ( $threads_items ) {
$packages[] = array(
‘contents’ => $threads_items,
‘contents_cost’ => array_sum( wp_list_pluck( $threads_items, ‘line_total’ ) ),
‘applied_coupons’ => WC()->cart->applied_coupons,
‘destination’ => array(
‘country’ => WC()->customer->get_shipping_country(),
‘state’ => WC()->customer->get_shipping_state(),
‘postcode’ => WC()->customer->get_shipping_postcode(),
‘city’ => WC()->customer->get_shipping_city(),
‘address’ => WC()->customer->get_shipping_address(),
‘address_2’ => WC()->customer->get_shipping_address_2()
)
);
}
if ( $regular_items ) {
$packages[] = array(
‘ship_via’ => array( ‘flat_rate’ ),
‘contents’ => $regular_items,
‘contents_cost’ => array_sum( wp_list_pluck( $regular_items, ‘line_total’ ) ),
‘applied_coupons’ => WC()->cart->applied_coupons,
‘destination’ => array(
‘country’ => WC()->customer->get_shipping_country(),
‘state’ => WC()->customer->get_shipping_state(),
‘postcode’ => WC()->customer->get_shipping_postcode(),
‘city’ => WC()->customer->get_shipping_city(),
‘address’ => WC()->customer->get_shipping_address(),
‘address_2’ => WC()->customer->get_shipping_address_2()
)
);
}

return $packages;

}

Please help me fix this issue. All i want to do is to apply free shipping to a certain shipping class once the amount of the shipping class reaches 25 or more.

Read more here:: Woocommerce free shipping method for some products

Leave a Reply

Your email address will not be published. Required fields are marked *