0

To the good folk of Stack Overflow,

I'm trying to create an "Upgrade Safe" way (as a function in my Theme's functions.php file) of customising the footer of the standard WooCommerce email notification email that customers receive when placing orders.

What I'm trying to achieve is to add custom text to said email footer when products from specific categories are ordered, but in all honesty, I'm struggling a bit with the in_array test for the categories, and I'm pretty sure I haven't got it quite right!

If any of you had any pointers on where I've gone wrong with it, I'd be eternally grateful!

function farm_shop_woocommerce_email_footer( $order, $sent_to_admin, $plain_text, $email ) { 

        $items = $order->get_items();
    
        foreach ( $items as $item ) {        
                   
            $terms = wp_get_post_terms( $item['product_id'], 'product_cat' );
            
            foreach($terms as $term){
                
                $term_names[] = $term->name; // Product cat Name
                
                //$term->term_id; Product cat Id
                //$term->slug; Product cat slug
                
            }
            
        }
        
        if ( in_array('Farm Shop', 'Cowdray Kitchen', 'Cowdray Living', 'The Meditator', 'Cowdray Hampers', 'Cowdray Supper Kits', 'Grocery', 'Butchery', 'Deli', 'Pantry', 'Houseplants and Flowers', 'Picnic Hampers', 'Afternoon Tea', 'Drinks', 'Wreaths', $term_names) ) {
            
            echo 'Cowdray Farm Shop Ltd<br>VAT Number: 970407718';
            
        }
    
    }; 

    add_action( 'woocommerce_email_footer', 'farm_shop_woocommerce_email_footer', 10, 4 ); 
  • 1
    It all depends on how you would want to implement it. Do you want to check if **all** of those are in the `$term_names`? Or Do you want to check if **any** of those exists in the array? [This answer might be useful](https://stackoverflow.com/a/29167363/15040627), as well as [this](https://stackoverflow.com/a/15880509/15040627) and [this one](https://stackoverflow.com/a/11040612/15040627). – Ruvee Nov 23 '21 at 22:51

1 Answers1

0

I noticed that you aren't using in_array correctly:

Can you put your set of strings in an Array like this example -> play close attention to the "array('x', 'y')":

if (in_array(array('x', 'y'), $yourTermsArray)) {
   echo 'something matched';
}

Getting the term array together looks accurate as far as I can see.

Hendrik Vlaanderen
  • 808
  • 1
  • 7
  • 13