I’ve seen a couple of similar answers, but not quite what I need. Basically I just want to display a notice to show at checkout when the total order weight is 225kg or over.
Based on Display weight and the remaining weight message on woocommerce cart and checkout answer code, here is my adapted code:
// Display a custom shipping message when cart weight exeeds a limit
add_filter( 'woocommerce_before_cart', 'display_total_weight_notice' );
add_filter( 'woocommerce_before_checkout_form', 'display_total_weight_notice' );
function display_total_weight_notice( $message ) {
// DEFINE the allowed weight limit
$cart_total_weight = WC()->cart->get_cart_contents_weight();
if( cart_total_weight >= 225 ) :
wc_print_notice( sprintf(
__( 'Your order has a total weight of %s. This will be shipped via Pallet Delivery, by continuing you agree to the following:<br><br>
> Ground/Road around the property will be flat, level and solid. <br>
> Standard Delivery will take place between 9am-5pm and will require a signature. You can advise a safe place to leave your order, however this is at the driver’s discretion to do so, we hold no responsibility if left. <br>
> Where a delivery cannot be made due to access, a re-delivery can be made at your request for a charge of £45.00 per pallet on the following day or when arranged. <br>
> In any cases of Damages/Shortages you will note this on the delivery note when you Sign for it, and then contact us right away. No refunds/replacements will be given if damages are not noted at delivery. <br>
> Pallet Orders cancelled en route, or returned due to ground Conditions/Access Restrictions will incur a return fee of £45 per pallet (subject to location), deducted from any refunds given. <br>
> Orders over 1000kg will require your own forklift.
' ),
'<strong>' . wc_format_weight($cart_total_weight) . '</strong>',
),'notice' );
endif;
}
But it’s not working for me.
2
Answers
Managed it with
I just tested your code, and it correctly displays a message on cart page.
There’s just a typo in your code, your missing the
$
before your variable:When setting at least one product with a weight, I get the correct total weight from
$cart_total_weight = WC()->cart->get_cart_contents_weight();
. If no product in cart as a weight defined, it correctly returns 0.If you haven’t edited the cart template, it should work. If you edited it, check that the
do_action()
is still here.Here I edited the limit to 20kg to display the message, with a product of 25Kg to match the criterias.