Skip to content

Instantly share code, notes, and snippets.

@wbcomdev
Created December 27, 2023 11:57
Show Gist options
  • Select an option

  • Save wbcomdev/f7fd1f7e398bd0dab363fa13c212698a to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/f7fd1f7e398bd0dab363fa13c212698a to your computer and use it in GitHub Desktop.
Restrict the user to checkout more than one services
<?php
function restrict_cart_quantity( $item ) {
$cart_items = edd_get_cart_contents();
if ( ! empty( $cart_items ) ) {
foreach ( $cart_items as $cart_item ) {
$product_type = get_post_meta( $cart_item['id'], '_edd_product_type', true );
if ( 'service' === $product_type ) {
edd_set_error( 'edd_one_product_limit', __( 'You can only add one service product to the cart.', 'your-textdomain' ) );
// Optionally, redirect to the checkout page
// wp_safe_redirect( edd_get_checkout_uri() );
// Clear the added item
$item = '';
// Exit the loop, assuming you want to restrict to one service product
break;
}
}
}
return $item;
}
add_filter( 'edd_add_to_cart_item', 'restrict_cart_quantity', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment