wordpresswoocommerceproductcheckoutcoupon

Prevent checkout if no coupon has been applied when certain products in cart


I am working on a WooCommerce website and I am trying to restrict a product to be purchased only if a coupon is applied for it, so it should not be processed without adding a coupon code.

User must enter a coupon code to be able to order this specific product (not on all other products).

We don't need it to target a specific coupon to allow checkout, we need it to require any coupon because for this specific product we have around 150+ coupons.

Based on Allow specific products to be purchase only if a coupon is applied in Woocommerce code thread:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    $targeted_ids   = array(37); // The targeted product ids (in this array)
    $coupon_code    = 'summer2'; // The required coupon code

    $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
            break; // stop the loop
        }
    }
}

How to handle: prevent checkout if no coupon has been applied when certain products in cart.


Solution

  • It is a matter of removing or adjusting a few conditions. For example, checking whether coupons have been applied with

    So you get:

    function action_woocommerce_check_cart_items() {
        // The targeted product ids (in this array)
        $targeted_ids = array( 813, 30 );
    
        // Get applied coupons
        $coupon_applieds = WC()->cart->get_applied_coupons();
        
        // Empty coupon applieds
        if ( empty ( $coupon_applieds ) ) {
    
            // Loop through cart items
            foreach( WC()->cart->get_cart() as $cart_item ) {
                // Check cart item for defined product Ids
                if ( in_array( $cart_item['product_id'], $targeted_ids )  ) {
                    // Clear all other notices          
                    wc_clear_notices();
    
                    // Avoid checkout displaying an error notice
                    wc_add_notice( sprintf( 'The product "%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
                    
                    // Optional: remove proceed to checkout button
                    remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
                    
                    // Break loop
                    break;
                }
            }
        }
    }   
    add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );