Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Limit Cart Weight
- Plugin URI: https://www.damiencarbery.com/
- Description: Prevent checkout when cart is above a certain weight.
- Author: Damien Carbery
- Author URI: https://www.damiencarbery.com
- Version: 0.1
- */
- // Based on: https://www.cloudways.com/blog/set-purchase-limits-on-woocommerce/
- add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
- function cldws_set_weight_requirements() {
- // Only run in the Cart or Checkout pages
- if( is_cart() || is_checkout() ) {
- // Set the maximum weight before checking out
- $maximum_weight = 70;
- // Get the Cart's content total weight
- $cart_contents_weight = WC()->cart->get_cart_contents_weight();
- // Compare values and add an error is Cart's total weight
- if( $cart_contents_weight > $maximum_weight ) {
- // Display our error message
- wc_add_notice( sprintf('<strong>Your order must be less than %s%s before checking out.</strong>'
- . '<br />Current cart weight: %s%s',
- $maximum_weight,
- get_option( 'woocommerce_weight_unit' ),
- $cart_contents_weight,
- get_option( 'woocommerce_weight_unit' ),
- get_permalink( wc_get_page_id( 'shop' ) )
- ),
- 'error' );
- }
- }
- }
Add Comment
Please, Sign In to add comment