ResolvedFunction for if user forgot order Drink + Code

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #27772
    01.mrlast
    Participant

    Hi
    I wrote a code and said maybe someone needs it, and because it was a good template, I said I would write it for you too
    You must add this code inside the function

    And if the user forgets to order something like a drink, he will tell them that you have forgotten

    
    add_action('woocommerce_before_cart', 'bbloomer_check_category_in_cart');
     
    function bbloomer_check_category_in_cart() {
     
    // Set $cat_in_cart to false
    $cat_in_cart = true;
     
    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
     
        // If Cart has category "download", set $cat_in_cart to true
        if ( has_term( 'drink', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = false;
            break;
        }
    }
       
    // Do something if category "download" is in the Cart      
    if ( $cat_in_cart ) {
     
    // For example, print a notice
    echo "<h1 style='background-color:red'>";
    wc_print_notice( 'You forgot to order a drink!', 'notice' );
    echo "</h1>";
     
    // Or maybe run your own function...
    // ..........
     
    }
     
    }
    add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_category_is_in_cart' );
    function webroom_check_if_product_category_is_in_cart() {
     
    $cat_in_cart = true;
          
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
     
        if ( has_term( 'drink', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = false;
            break;
        }
    }
       
    // Do something if the category is in the Cart      
    if ( $cat_in_cart ) {
    echo "<h1 style='background-color:red'>";
    wc_print_notice( 'You forgot to order a drink!', 'notice' );
    echo "</h1>";
    }
    }
    #27784
    Althemist
    Keymaster

    Hello 01.mrlast,

    Thanks for sharing.

    #27821
    01.mrlast
    Participant

    You’re welcome <3

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in and have valid license to reply to this topic.

License required for the following item
Login and Registration Log in · Register