Home » Php » php – Allow Specific Product to Bought Alone Woocommerce

php – Allow Specific Product to Bought Alone Woocommerce

Posted by: admin February 25, 2020 Leave a comment

Questions:

Let’s Say I have Product A which is allow to bought alone. If something else already in cart it shows the message. “Not Allow”. I tried various methods but not able to find proper solution for that.

When somebody tries to click on the “ADD TO CART” button then it must check via code that if other items in cart it is not allow to put in cart and shows the message.

PRODUCT A is allow bought alone.

I tried with the category comparison and it works. but I want to do only with the Product ID.

    add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 5);
function dont_add_paint_to_cart_containing_other($validation, $product_id) {

// Set flag false until we find a product in cat paint
    $cart_has_paint = false;

// Set $cat_check true if a cart item is in paint cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $products_ids = 137817;
        $product = $cart_item['data'];



        if (has_term('miscellaneous', 'product_cat', $product->id)) {



            $cart_has_paint = true;
            // break because we only need one "true" to matter here
            break;
        }

    }

    $product_is_paint = false;
    if (has_term('miscellaneous', 'product_cat', $product_id)) {



        $product_is_paint = true;
    }

// Return true if cart empty

    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains paint and product to be added is not paint, display error message and return false.
        if ($cart_has_paint && !$product_is_paint) {
             echo '<script type="text/javascript">';
            echo ' alert("Hello,Sorry, “custom order” items must be purchased separately! To purchase this item, please either checkout with your current cart or remove any “custom order” items from your cart to enable you add the regular items.")';  //not showing an alert box.
            echo '</script>';


            $validation = false;
        }
        // If cart contains a product that is not paint and product to be added is paint, display error message and return false.

       elseif (!$cart_has_paint && $product_is_paint) {
            echo '<script type="text/javascript">';
            echo ' alert("Sorry, “custom order” item must be purchased separately! To purchase any “custom order” items, please either checkout with your current cart or empty cart to enable you add the “custom order” items.")';  //not showing an alert box.
            echo '</script>';


            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

This code only works with the miscellaneous category I want to allow only with the Product ID.. not category..

How to&Answers:

Assuming that only 1 quantity can be purchased from product A

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Product id to bought alone 
    $product_id_alone = 30;

    // Set variable
    $alone = true;

    // If passed
    if ( $passed ) {
        // If cart is NOT empty when a product is added
        if ( !WC()->cart->is_empty() ) {

            // If product id added = product id alone
            if ( $product_id_alone == $product_id ) {
                $alone = false;
            } else {
                // Generate a unique ID for the cart item
                $product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );

                // Check if product is in the cart
                $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

                // If product is already in cart
                if ( $in_cart ) {
                    $alone = false;
                }
            }
        } else {
            // If product is added when cart is empty but $quantity > 1
            if ( $product_id_alone == $product_id && $quantity > 1 ) {
                $alone = false;         
            }
        }
    }

    if ( $alone == false ) {
        // Set error message
        $message = 'PRODUCT A is allow bought alone.';
        wc_add_notice( __( $message, 'woocommerce' ), 'error' );
        $passed = false;

        // Empty the cart
        WC()->cart->empty_cart();

        // Add specific product with quantity 1
        WC()->cart->add_to_cart($product_id_alone, 1 );
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );