Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    See here first https://docs.woothemes.com/document/template-structure/

    You can add this to the email template files.

    You can get the list of brands with https://codex.www.ads-software.com/Function_Reference/wp_get_post_terms

    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    I can get the output of Brands on a product page using the code below. But I am unable to figure out which hook to use to get the brand for each product in wocommerce email notifications. I’m on Woocommerce 2.6, WordPress latest.

    <?php $brands = wp_get_post_terms( $post->ID, 'product_brand', array("fields" => "all") );
      foreach( $brands as $brand ) {
      echo $brand->name;
    }?>
    Plugin Contributor Mike Jolley

    (@mikejolley)

    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    Hi Mike,
    I added this function into function.php and nothing is happening. Brands are not showing up in the email. Where did I do the mistake ?

    function add_brands_to_email () {
    
      $brands = wp_get_post_terms( $post->ID, 'product_brand', array("fields" => "all") );
        foreach( $brands as $brand ) {
        echo $brand->name;
      }
    }
    // add the action
    add_action( 'woocommerce_order_item_meta_end', 'add_brands_to_email', 10, 3 );
    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    I’m not getting any woocommerce emails after adding that function, Where have I done wrong ?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You probably don’t have $post declared anywhere, so $post->ID is causing an error. You need to get the post/product id using the parameters passed in this hook: https://github.com/woothemes/woocommerce/blob/master/templates/emails/email-order-items.php#L59

    If you aren’t comfortable with custom code like this, you might want to consider https://jobs.wordpress.net/ or https://codeable.io/

    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    Stumbled upon this old post of mine, thought of updating how I did this, in case someone comes wondering with this same question. Here is how I did it.
    1. Copy the whole woocommerce template plugin into your child theme
    /path/to/plugins/woocommerce/templates to /path/to/child-theme/woocommerce/
    2. Edit the email-order-items.php in /path/to/child-theme/woocommerce/emails
    3. Before the action hook
    do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

    add this code:

    $brands = wp_get_post_terms( $_product->id, 'product_brand', array("fields" => "all") );
        echo '<div class="email-brand"><p><strong>Brand:</strong>' . ' ';
        foreach( $brands as $brand ) {
            echo $brand->name . '  ';
        }
        echo '</p></div>';

    _product->id will fetch your productID and pass that as if it is post->ID to wp_get_post_terms(). <div> and <p> are for styling.

    This will output the brands list (separated with a space) of each product in woocommerce emails to customers and to store admin.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to output brands in woocommerce emails to admin & customer ?’ is closed to new replies.