Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active November 27, 2022 17:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/89c74306b79bca6b83466939517fa518 to your computer and use it in GitHub Desktop.
Save damiencarbery/89c74306b79bca6b83466939517fa518 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Limit Cart Weight to increments
Plugin URI: https://www.damiencarbery.com/
Description: Prevent checkout unless cart is above a minimum weight and increments of a weight.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_action( 'woocommerce_check_cart_items', 'dcwd_cart_weight_min_and_increments' );
function dcwd_cart_weight_min_and_increments() {
// Set the minimum cart weight.
$minimum_weight = 6;
$increments = 6;
// 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 % $increments != 0 ) || ( $cart_contents_weight / $minimum_weight < 0 ) ) {
// 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_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