• Resolved mediafuel

    (@mediafuel)


    Hi,

    I need my woocommerce order emails to show all categories listed, currently it is only showing the last subcategory. I have parent-category-subcategories-subcategories. I added this code to the functions.php file:

    function modfuel_woocommerce_before_order_add_cat($name, $item){

    $product_id = $item[‘product_id’];

    $_product = wc_get_product( $product_id );
    $htmlStr = “”;
    $cats = “”;
    $terms = get_the_terms( $product_id, ‘product_cat’ );

    $count = 0;
    foreach ( $terms as $term) {
    $count++;

    if($count > 1){
    $cats .= $term->name;
    }
    else{
    $cats .= $term->name . ‘,’;
    }

    }

    $cats = rtrim($cats,’,’);

    $htmlStr .= $_product->get_title();

    $htmlStr .= “<p>Category: ” . $cats . “</p>”;

    return $htmlStr;
    }

    add_filter(‘woocommerce_order_item_name’,’modfuel_woocommerce_before_order_add_cat’, 10, 2);

    But it only shows the last subcategory and I need it show all categories. Could you please help with editing the code to add them or advise what I should do?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @mediafuel As far as I can tell your code should work. I did clean it up to make it more efficient, though:

    
    function modfuel_woocommerce_before_order_add_cat( $name, $item ){
    
    	// Get the product and its terms.
    	$product = wc_get_product( $item['product_id'] );
    	$terms   = get_the_terms( $product->get_id(), 'product_cat' );
    
    	// Add the term names to an array.
    	foreach ( $terms as $term ){
    		$term_names[] = $term->name;
    	}
    
    	// Put the product title in the string with the term names.
    	$htmlStr = $product->get_title();
    	$htmlStr .= ' <p>Category: '. implode( ', ', $term_names ) .'</p>';
    
    	return $htmlStr;
    }
    add_filter( 'woocommerce_order_item_name','modfuel_woocommerce_before_order_add_cat', 10, 2 );
    

    This filter is used in a few places besides the emails, but it looks like it’s working fine to me:


    Link to image: https://d.pr/i/wzDIyo


    Link to image: https://d.pr/i/LzFlP5

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved. If you have any further questions, a new thread can be opened.

    Dear @jessepearson I have tried the above code. But it is only showing the last category, not the parent category.

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @ckprcms Please try with just WooCommerce active and see if that works, also please make sure WooCommerce is up to date. It may be a conflict with another plugin causing the issue. As far as I can tell, the code is correct.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show all Categories in Order Email’ is closed to new replies.