1

I need to have some text added to the bottom of the Order Processing email that's sent to the customer when an order is placed if they purchase a product in the category of "Registration". BUT I DO NOT want the text to show if a product from another category was bought as well. I am able to display the desired text when a product in the "Registration" category is purchased successfully with the following code:

$order_id = $order->get_id();
$order      = wc_get_order( $order_id );
$items      = $order->get_items();

foreach ( $items as $item_id => $item_data ) {
    $product    = $item_data->get_product();
    $categories = array();

    $terms = get_the_terms( $product->get_id(), 'product_cat' );

    if ( is_wp_error( $terms ) || empty( $terms ) ) {
            continue;
    }

    foreach ( $terms as $term ) {
        if(strtolower($term->name) === 'registration'){
            // Run any html code after this closing php tag -> ?>
            <p>
                Didn't get your gear yet? Click <a href="https://flexfootball.com/shop/">HERE</a> to order yours!
            </p>
        <?php } // Close "product is a registration form" condition
    }
}

And the text DOES NOT display if a product from another category is purchased. This is good, but if someone purchases a product from the "Registration" category AND another category, the code displays.

So to reiterate:
If an item is bought in the category "Registration", text IS displayed.
If an item is bought in a category OTHER than "Registration" text is NOT displayed.
If an item is bought in the category "Registration" AND another category, text is NOT displayed

I am thinking I need an if statement that is strict in that it is only true if "Registration" is the only category in the array.

Any help is greatly appreciated!

1

Try something like this:

<?php
$order_id = $order->get_id();
$order    = wc_get_order( $order_id );
$items    = $order->get_items();
$is_valid = null;

foreach ( $items as $item_id => $item_data ) {
    $product    = $item_data->get_product();
    $categories = array();

    $terms = get_the_terms( $product->get_id(), 'product_cat' );

    if ( is_wp_error( $terms ) || empty( $terms ) ) {
        continue;
    }

    foreach ( $terms as $term ) {
        if( false !== $is_valid && strtolower($term->name) === 'registration'){
            $is_valid = true;
            continue;
        }

        $is_valid = false;
    }
}

if ( $is_valid ) {
    // Run any html code after this closing php tag -> ?>
    <p>
        Didn't get your gear yet? Click <a href="https://flexfootball.com/shop/">HERE</a> to order yours!
    </p>
<?php // Close "product is a registration form" condition
}

The basic gist is that you are setting up $is_valid as a NULL value. If any category is not registration, then $is_valid becomes boolean false. If the category is registration AND $is_valid is not strictly (!==) false, set $is_valid to boolean true.

Finally, after the loops are done, if $is_valid is true, add the message.

  • 1
    Perfect! This did exactly what I needed. Thanks a million! – Nick Taylor May 1 at 19:29
  • No problem, happy coding! – phatskat May 1 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.