Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created September 11, 2025 03:11
Show Gist options
  • Select an option

  • Save shameemreza/36e3ce926d352d0a4ed7727d7385bbc3 to your computer and use it in GitHub Desktop.

Select an option

Save shameemreza/36e3ce926d352d0a4ed7727d7385bbc3 to your computer and use it in GitHub Desktop.
Fix for Product Add-Ons bypassing the Sold individually setting in WooCommerce.
/**
* Fix for Product Add-ons bypassing Sold individually setting
* Add this to the theme's functions.php or as a custom plugin
*/
add_filter( 'woocommerce_add_to_cart_validation', 'enforce_sold_individually_with_addons', 999, 5 );
function enforce_sold_individually_with_addons( $passed, $product_id, $quantity, $variation_id = 0, $variations = array() ) {
// Get the product
$product = wc_get_product( $variation_id ? $variation_id : $product_id );
// Only check if product is sold individually
if ( ! $product || ! $product->is_sold_individually() ) {
return $passed;
}
// Check if this product is already in cart (ignoring add-on variations)
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] === $product_id ) {
// If it's a variable product, also check variation
if ( $variation_id && isset( $cart_item['variation_id'] ) && $cart_item['variation_id'] !== $variation_id ) {
continue;
}
wc_add_notice(
sprintf(
__( 'You cannot add another "%s" to your cart. This product is limited to one per order regardless of the options selected.' ),
$product->get_name()
),
'error'
);
return false;
}
}
return $passed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment