daymobrew

Limit Cart Weight

Jun 7th, 2018
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Limit Cart Weight
  4. Plugin URI: https://www.damiencarbery.com/
  5. Description: Prevent checkout when cart is above a certain weight.
  6. Author: Damien Carbery
  7. Author URI: https://www.damiencarbery.com
  8. Version: 0.1
  9. */
  10.  
  11. // Based on: https://www.cloudways.com/blog/set-purchase-limits-on-woocommerce/
  12. add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
  13. function cldws_set_weight_requirements() {
  14.     // Only run in the Cart or Checkout pages
  15.     if( is_cart() || is_checkout() ) {
  16.         // Set the maximum weight before checking out
  17.         $maximum_weight = 70;
  18.         // Get the Cart's content total weight
  19.         $cart_contents_weight = WC()->cart->get_cart_contents_weight();
  20.         // Compare values and add an error is Cart's total weight
  21.         if( $cart_contents_weight > $maximum_weight  ) {
  22.             // Display our error message
  23.             wc_add_notice( sprintf('<strong>Your order must be less than %s%s before checking out.</strong>'
  24.                 . '<br />Current cart weight: %s%s',
  25.                 $maximum_weight,
  26.                 get_option( 'woocommerce_weight_unit' ),
  27.                 $cart_contents_weight,
  28.                 get_option( 'woocommerce_weight_unit' ),
  29.                 get_permalink( wc_get_page_id( 'shop' ) )
  30.                 ),
  31.                 'error' );
  32.         }
  33.     }
  34. }
Add Comment
Please, Sign In to add comment