I need something similar as below, but i don’t need the warehouse option.
I need to block only a few products from being purchased more than once, the other products can be bought more than once.
How to include only those product-ID’s?
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 4 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null ) {
$product = wc_get_product( $product_id );
$warehouse = $product->get_attribute('pa_warehouse');
$cart_items = WC()->cart->get_cart();
if ( WC()->cart->is_empty() ) {
return $passed;
}
// Loop through cart items
foreach ( WC()->cart->get_cart() as $values ) {
// Check if the warehouse is different
if ( $warehouse !== $values['data']->get_attribute('pa_warehouse') ) {
wc_add_notice( __( 'This product cannot be purchased because...', 'woocommerce' ), 'error' );
return false;
}
}
return $passed;
}
The code works, but i only need it for a few product id’s and i don’t know how
2
Answers
From what I understood, you can try this
`