Skip to content

Instantly share code, notes, and snippets.

@stevegrunwell
Created February 4, 2021 19:34
Show Gist options
  • Save stevegrunwell/fa7fd5a1d4583e028b8e887567971283 to your computer and use it in GitHub Desktop.
Save stevegrunwell/fa7fd5a1d4583e028b8e887567971283 to your computer and use it in GitHub Desktop.
Prevent WooCommerce from emptying carts after the order limit has been reached — https://wordpress.org/support/topic/cart-empties-when-limit-is-hit/
<?php
/**
* Plugin Name: Limit Orders for WooCommerce - Prevent Empty Carts
* Description: Prevent WooCommerce from emptying carts after the order limit has been reached.
* Author: Nexcess
* Author URI: https://nexcess.net
*/
/**
* Prevent WooCommerce from removing non-purchasable items from the cart.
*
* Limit Orders for WooCommerce marks all products as non-purchasable once the threshold has been
* reached, but WC_Cart_Session::get_cart_from_session() will remove any non-purchasable items from
* the cart.
*
* This callback lets items be purchasable (unless they were being prevented for some other reason)
* for the sake of loading the cart, but will still disable "Add to Cart" buttons across the site
* until the next interval starts.
*
* @ticket https://wordpress.org/support/topic/cart-empties-when-limit-is-hit/
*/
add_action( 'woocommerce_load_cart_from_session', function () {
// Limit Orders registers the filter callback at a priority of 10.
if ( 10 !== has_action( 'woocommerce_is_purchasable', '__return_false' ) ) {
return;
}
// Temporarily remove the filter.
remove_filter( 'woocommerce_is_purchasable', '__return_false' );
// Once the cart is loaded, put the filter back in place.
add_action( 'woocommerce_cart_loaded_from_session', function () {
add_filter( 'woocommerce_is_purchasable', '__return_false' );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment