To customize advanced shipping rules in WooCommerce using code, you can leverage the following hooks and functions:
1. Restrict Shipping by Postcode
php
add_filter( 'woocommerce_shipping_packages', 'restrict_shipping_by_postcode' );
function restrict_shipping_by_postcode( $packages ) {
$customer_postcode = WC()->customer->get_shipping_postcode();
$allowed_postcodes = array( '12345', '67890' );
foreach ( $packages as $package_key => $package ) {
if ( ! in_array( $customer_postcode, $allowed_postcodes ) ) {
unset( $packages[ $package_key ] );
}
}
return $packages;
}
This code will remove shipping methods for packages where the customer's shipping postcode is not in the `$allowed_postcodes` array.
2. Restrict Shipping by Product Category
php
add_filter( 'woocommerce_package_rates', 'restrict_shipping_by_product_category', 10, 2 );
function restrict_shipping_by_product_category( $rates, $package ) {
$restricted_categories = array( 'category-slug-1', 'category-slug-2' );
foreach ( $package['contents'] as $item_key => $item ) {
$product_categories = get_the_terms( $item['product_id'], 'product_cat' );
if ( $product_categories ) {
foreach ( $product_categories as $category ) {
if ( in_array( $category->slug, $restricted_categories ) ) {
unset( $rates[ $item_key ] );
}
}
}
}
return $rates;
}
This code will remove shipping methods for packages that contain products from the specified restricted categories.
3. Restrict Shipping by Product Weight
php
add_filter( 'woocommerce_package_rates', 'restrict_shipping_by_product_weight', 10, 2 );
function restrict_shipping_by_product_weight( $rates, $package ) {
$max_weight = 10; // Maximum allowed weight per package
$total_weight = 0;
foreach ( $package['contents'] as $item_key => $item ) {
$total_weight += $item['data']->get_weight() * $item['quantity'];
}
if ( $total_weight > $max_weight ) {
return array();
}
return $rates;
}
This code will remove all shipping methods for packages that exceed the maximum allowed weight per package.
4. Restrict Shipping by Cart Subtotal
php
add_filter( 'woocommerce_package_rates', 'restrict_shipping_by_cart_subtotal', 10, 2 );
function restrict_shipping_by_cart_subtotal( $rates, $package ) {
$min_subtotal = 50; // Minimum cart subtotal required
$cart_subtotal = WC()->cart->get_subtotal();
if ( $cart_subtotal < $min_subtotal ) {
return array();
}
return $rates;
}
This code will remove all shipping methods for packages where the cart subtotal is less than the specified minimum.
These are just a few examples of how you can customize advanced shipping rules in WooCommerce using code. You can combine these techniques and create more complex rules based on your specific requirements.
Citations:[1] https://calcurates.com/features/woocommerce-shipping-restrictions
[2] https://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html
[3] https://wordpress.org/plugins/advanced-free-flat-shipping-woocommerce/
[4] https://woocommerce.com/products/woocommerce-advanced-shipping-packages/
[5] https://woocommerce.com/document/woocommerce-advanced-shipping-packages/