Skip to content

Instantly share code, notes, and snippets.

@asharirfan
Last active August 1, 2016 17:19
Show Gist options
  • Save asharirfan/848d6d64a281d4828740214568d13282 to your computer and use it in GitHub Desktop.
Save asharirfan/848d6d64a281d4828740214568d13282 to your computer and use it in GitHub Desktop.
This functions limits the number of items in WooCommerce cart to only one.
<?php
if ( ! function_exists( 'allow_only_one_subscription' ) ) {
/**
* allow_only_one_subscription.
*
* This functions limits the number of items in
* WooCommerce cart to only one.
*
* @since 1.3.2
*/
function allow_only_one_subscription( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Whoa hold up! You can buy only one subscription at a time.', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'allow_only_one_subscription', 10, 3 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment