Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Limit Cart Weight
Plugin URI: https://www.damiencarbery.com/
Description: Prevent checkout unless 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', 'dcwd_set_minimum_cart_weight' );
function dcwd_set_minimum_cart_weight() {
// Set the minimum cart weight.
$minimum_weight = 5;
// Get the cart's total weight
$cart_contents_weight = WC()->cart->get_cart_contents_weight();
// Compare values and add an error if less than the minimum weight
if( $cart_contents_weight < $minimum_weight ) {
// Display our error message
wc_add_notice( sprintf('<strong>Your order must be at least %s%s before checking out.</strong><br />Current cart weight: %s%s',
$minimum_weight, get_option( 'woocommerce_weight_unit' ),
$cart_contents_weight, get_option( 'woocommerce_weight_unit' ) ),
'error' );
}
}
<?php
/*
Plugin Name: Limit Cart Weight
Plugin URI: https://www.damiencarbery.com/
Description: Prevent checkout unless 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', 'dcwd_set_maximum_cart_weight' );
function dcwd_set_maximum_cart_weight() {
// Set the maximum cart weight.
$maximum_weight = 5;
// Get the cart's total weight
$cart_contents_weight = WC()->cart->get_cart_contents_weight();
// Compare values and add an error if less than the maximum 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' ) ),
'error' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.