skip to Main Content

This is similar to a question I posted previously here but the calculation I need for a new project is different.

Rather than showing a notice based on the total quantity of items in the cart, I need to show a notice if any one product in the cart has a quantity of 1. If no items in the cart have a quantity of 1 then it doesn’t need to show, but if any item does then it needs to show. Is this possible?

2

Answers


  1. Chosen as BEST ANSWER

    This is what I used in the end, thanks to mpx9910's answer (thank you!):

    add_action( 'woocommerce_before_cart_contents', 'display_notice_based_on_item_quantity' );
    function display_notice_based_on_item_quantity() {
        foreach ( WC()->cart->get_cart() as $cart_item ) {    
                if ( $cart_item['quantity'] == 1 )
                {
                 $terms = get_the_terms( $cart_item['product_id'], 'product_cat' );
                 foreach ( $terms as $term )
                    {
                    if ($term->slug == 'your-category-slug')
                        continue 2;
                    }
                    if ( is_cart() )
                        echo '<div class="cart-qty-notice">';
                        printf( __("Message goes here", "woocommerce"));
                        echo '</div>';
                    break;
                }
           }
        }
    

  2. This function loops over cart items, if quantity of a cart item is equal to 1 the notice is shown and the function ends.

       function display_notice_based_on_item_quantity() {
    
           foreach ( WC()->cart->get_cart() as $cart_item ) {    
                if ( $cart_item['quantity'] == 1 )
                {
                    if ( is_cart() )
                        wc_print_notice( sprintf( __("there is an item with qunatity of 1 in the cart", "woocommerce") ), 'notice' );
                    break;
                }
           } 
    
        }
        add_action( 'woocommerce_check_cart_items', 'display_notice_based_on_item_quantity' );
    

    You can use ‘woocommerce_check_cart_items’ hook, or other cart hooks based on where you need to print the notice.

    Here is also a modified answer to exclude products in certain category from triggering the notice (question from comment):

    function display_notice_based_on_item_quantity() {
    
       foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['quantity'] == 1 )
            {
                $terms = get_the_terms( $cart_item['product_id'], 'product_cat' );
                foreach ( $terms as $term )
                {
                   if ($term->slug == 'your-category-slug')
                        continue 2;
                }
                if ( is_cart() )
                    wc_print_notice( sprintf( __("there is an item with qunatity of 1 in the cart", "woocommerce") ), 'notice' );
                break;
            }
       } 
    
    }
    

    Whenever a product with quantity 1 is found, there is another nested loop that iterates over the product’s categories, if any of the categories matches the one that you don’t want to trigger the notice then notice is not displayed and the outer loop continues.

    Don’t forget to replace ‘your-category-slug’ with slug of the category you don’t want to trigger the notice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search