Struggling with Per-Item Shipping and Classes

[ad_1]

Needless to say, I’m in Woo Shipping hell. I can’t figure this out. I’m trying to set a rate table shipping method to a shipping class and then assign that shipping class to a product. If you look at the screen shot, I’ve made some progress, but I’m having a few problems.

I have Four shipping methods. Ordinary, Fast Ordinary, Free Shipping, and Sensitive. All products should default to Ordinary with the choice of Fast Ordinary

For the Sensitive products, no other method should be allowed. Including free shipping.

For the ordinary products, the default (selected) should be Ordinary, and the option to select Fast Ordinary should be available.

For free shipping, Sensitive products are never eligible, but Ordinary products are once 200 dollars is in that basket.

So, let me sum it up:

Ordinary products default to Ordinary shipping with the option for Fast Ordinary. Free shipping only kicks in for Ordinary products once 200 dollars is reached. All shipping methods should be available (conditionally) except Sensitive shipping.

Sensitive products must only be allowed Sensitive shipping. Ordinary, Fast Ordinary, and Free shipping must never be an option.

Take a look at the screenshot to get an idea of where I’m up to.

Note: I have some custom code that splits up the cart using this function:

/**
* Set default shipping method for specific shipping classes in WooCommerce cart.
*/
function set_default_shipping_method_for_shipping_class( $rates, $package ) {
// Define an array of shipping class slugs for which you want to set the default method
$shipping_class_slugs = array(
'cjpacket-ordinary',
'cjpacket-fast-ordinary',
'flat-rate-shipping'
);

// Loop through cart items to check if any product belongs to the specified shipping classes
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$shipping_class = $cart_item['data']->get_shipping_class();
// Check if the shipping class of the current item matches any of the defined shipping class slugs
if ( in_array( $shipping_class, $shipping_class_slugs ) ) {
// Loop through available shipping rates to set the default method
foreach ( $rates as $rate_key => $rate ) {
// Change 'flat_rate:1' with your desired shipping method ID
if ( 'flat_rate:1' === $rate_key ) { // Change 'flat_rate:1' to match your desired method
// Make this method the default selected
$rates[$rate_key]->set_taxes( WC_Tax::calc_shipping_tax( $rates[$rate_key]->cost, WC_Tax::get_shipping_tax_rates() ) );
$rates[$rate_key]->set_shipping_total( $rates[$rate_key]->cost );
$rates[$rate_key]->set_method_id( $rates[$rate_key]->method_id );
$rates[$rate_key]->set_instance_id( $rates[$rate_key]->instance_id );
$rates[$rate_key]->set_label( $rates[$rate_key]->label );
$rates[$rate_key]->set_cost( $rates[$rate_key]->cost );
$rates[$rate_key]->set_id( $rates[$rate_key]->id );
$rates[$rate_key]->set_package_id( $rates[$rate_key]->package_id );
$rates[$rate_key]->set_meta_data( $rates[$rate_key]->meta_data );
$rates[$rate_key]->set_method_title( $rates[$rate_key]->method_title );
break; // Stop the loop after setting the default method
}
}
break; // Stop the loop after finding the specified shipping class
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'set_default_shipping_method_for_shipping_class', 10, 2 );

add_filter( 'woocommerce_cart_shipping_packages', 'bbloomer_split_shipping_packages_by_class' );

function bbloomer_split_shipping_packages_by_class( $packages ) {

$destination = $packages[0]['destination'];
$user = $packages[0]['user'];
$applied_coupons = $packages[0]['applied_coupons'];
$packages = array();

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$key = $cart_item['data']->get_shipping_class_id();
$packages[$key]['contents'][$cart_item_key] = $cart_item;
}

foreach ( $packages as $index => $package ) {
$total = array_sum( wp_list_pluck( $packages[$index]['contents'], 'line_total' ) );
$packages[$index]['destination'] = $destination;
$packages[$index]['user'] = $user;
$packages[$index]['applied_coupons'] = $applied_coupons;
$packages[$index]['contents_cost'] = $total;
}

return $packages;

}

It seems my shipping classes are begin totally ignored and I don’t know how to fill the other conditions, such as hiding/showing/default selecting various shipping methods based on shipping classes assigned to a product.

Your help is greatly appreciated. Suggestions for reasonably priced plugins (<$100) are welcome.

Thanks

  • This topic was modified 8 hours, 12 minutes ago by wiserrhino.

The page I need help with: [log in to see the link]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer