admin管理员组

文章数量:1429446

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="/">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!

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/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!

Share Improve this question asked May 1, 2019 at 18:23 Nick TaylorNick Taylor 1379 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/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.

本文标签: phpCustom Text in WooCommerce Order Processing Email Based on Products