MainWP Community

Cart Validation that a specific product is added to cart before other products in WooCommerce

Check that a specific product is added to the cart before other products can be added if using WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Product (ID) in cart
    $product_first_in_cart = 30;
    
    // Compare
    if ( $product_first_in_cart != $product_id ) {
        // Set variable
        $in_cart = false;
        
        // Cart NOT empty
        if ( ! WC()->cart->is_empty() ) {
            // Loop trough cart
            foreach( WC()->cart->get_cart() as $cart_item ) {
                // Search for the specific product
                if ( $cart_item['data']->get_id() == $product_first_in_cart ) {
                    // Found, break loop
                    $in_cart = true;
                    break;
                }
            }
        }
        
        // NOT in cart
        if ( ! $in_cart ) {
            wc_add_notice( __( 'Please add product "A" before adding other products', 'woocommerce' ), 'error' );
            $passed = false;
        }
    }

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

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.