Reputation: 11
In Woocommerce, I need to create a custom error message to let customers know that their order weight is too much (over 70lbs) and that they need to split up their order or call in to our shop to let our staff complete the order. So far I have tried adding this code into the function.php file, but it doesn't seem to be working. I'm pretty new at this, so I'm sure I have something off here. Any help would be much appreciated.
add_filter( 'woocommerce_no_shipping_available_html',
'my_custom_no_shipping_message' );
add_filter( 'woocommerce_cart_no_shipping_available_html',
'my_custom_no_shipping_message' );
function my_custom_no_shipping_message( $message ) {
$cart_weight = WC()->cart->get_cart_contents_weight();
if ($cart_weight >= 70 ){
return "Your order exceeds the shipping limit weight. Split into
multiple orders or call us to place order.";
}
Upvotes: 1
Views: 1640
Reputation: 254271
The following code, when cart exceed a specific weight limit, will:
The code:
// Add to cart validation - Add an error message when total weight exeeds a limit
add_filter( 'woocommerce_add_to_cart_validation', 'custom_price_field_add_to_cart_validation', 20, 3 );
function custom_price_field_add_to_cart_validation( $passed, $product_id, $quantity ) {
$cart_weight = WC()->cart->get_cart_contents_weight();
$product = wc_get_product($product_id);
$total_weight = ( $product->get_weight() * $quantity ) + $cart_weight;
if ( $total_weight >= 70 ) {
$passed = false ;
$message = __( "Your order exceeds the shipping limit weight. Please contact us.", "woocommerce" );
wc_add_notice( $message, 'error' );
}
return $passed;
}
// Disable all shipping methods when cart weight exeeds a limit
add_filter( 'woocommerce_package_rates', 'cart_weight_disable_shipping_methods', 20, 2 );
function cart_weight_disable_shipping_methods( $rates, $package ) {
if( WC()->cart->get_cart_contents_weight() >= 70 ) {
$rates = array();
}
return $rates;
}
// Display a custom shipping message when cart weight exeeds a limit
add_filter( 'woocommerce_no_shipping_available_html', 'weight_limit_no_shipping_message', 20, 1 );
add_filter( 'woocommerce_cart_no_shipping_available_html', 'weight_limit_no_shipping_message', 20, 1 );
function weight_limit_no_shipping_message( $message ) {
if (WC()->cart->get_cart_contents_weight() >= 70 )
$message = wpautop( __( "Your order exceeds the shipping limit weight. Split into multiple orders or call us to place order.", "woocommerce") );
return $message;
}
// Display an error notice on order submit when cart weight exeeds a limit
add_action( 'woocommerce_checkout_process', 'cart_weight_limit_submit_alert', 20, 1 );
function cart_weight_limit_submit_alert() {
if ( WC()->cart->get_cart_contents_weight() >= 70 ){
$message = __( "Your order exceeds the shipping limit weight. Split into multiple orders or call us to place order.", "woocommerce");
wc_clear_notices();
wc_add_notice( $message, 'error' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
To test that code you will need to refresh shipping methods. The best way is to enable debug mode in general shipping options (for testing this code). Then don't forget to disable it back.
Upvotes: 2