skip to Main Content

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


  1. From what I understood, you can try this

    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 ) {
    // Define an array of product IDs that should be restricted
    $restricted_product_ids = array( 1, 2, 3 );
    
    // Check if the current product ID is in the restricted list
    if ( in_array( $product_id, $restricted_product_ids ) ) {
        $cart_items = WC()->cart->get_cart();
    
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            // Check if the product ID matches one of the restricted products
            if ( in_array( $cart_item['product_id'], $restricted_product_ids ) ) {
                wc_add_notice( __( 'This product cannot be purchased more than once.', 'woocommerce' ), 'error' );
                return false; // Block the addition
            }
        }
    }
    
    return $passed; // Allow the addition to cart for other products
    }
    

    `

    Login or Signup to reply.
  2. 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 ) {
    // Array of product IDs that should be limited to one purchase
    $restricted_product_ids = array( 123, 456, 789 );
    
    // Check if the product is in the restricted list
    if ( in_array( $product_id, $restricted_product_ids ) ) {
        // Check if the product is already in the cart
        if ( wc_customer_bought_product( '', get_current_user_id(), $product_id ) ) {
            wc_add_notice( __( 'This product can only be purchased once.', 'woocommerce' ), 'error' );
            return false;
        }
    }
    return $passed;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search