I'm experiencing some inconsistent behaviour on a WooCommerce site.
I've added a custom taxonomy to the product post type, called 'product_brand':
add_action('init', 'register_taxonomies');
function register_taxonomies() {
$labels = array(
'name' => _x('Brands', 'taxonomy general name'),
'singular_name' => _x('Brand', 'taxonomy singular name'),
'search_items' => __('Søk Brands'),
'all_items' => __('Alle Brands'),
'parent_item' => __('Parent Brand'),
'parent_item_colon' => __('Parent Brand:'),
'edit_item' => __('Redigere Brand'),
'update_item' => __('Update Brand'),
'add_new_item' => __('Legg New Brand'),
'new_item_name' => __('Nye Brand Navn'),
'menu_name' => __('Brands'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product_brand'),
);
register_taxonomy('product_brand', array('product'), $args);
}
I want to show the selected product_brand term in front of each product name in the "New order" e-mail message.
Inspired by Display Product Brand and Name in Woocommerce Orders and email notifications answer code I've added the following code:
function wc_get_product_brand( $product_id ) {
return implode( ', ', wp_get_post_terms( $product_id, 'product_brand', ['fields' => 'names'] ));
}
// Display product brand in order pages and email notification
add_filter( 'woocommerce_order_item_name', function( $product_name, $item ) {
$product = $item->get_product(); // The WC_Product Object
$permalink = $product->get_permalink(); // The product permalink
if( taxonomy_exists( 'product_brand' ) ) {
if( $brand = wc_get_product_brand( $item->get_product_id() ) ) {
if ( is_wc_endpoint_url() )
return sprintf( '<a href="%s">%s %s</a>', esc_url( $permalink ), $brand, $product->get_name() );
else
return $brand . ' - ' . $product_name;
}
} else {
return $product_name;
}
return $product_name;
}, 10, 2 );
This works just fine on my staging site, where I'm using check payment. But on the live site, where I'm using an external payment gateway (Klarna), the taxonomy is not found. The taxonomy_exists( 'product_brand' )
is returning false
.
However, if I manually re-sends the "New order" message, from the order administration page, the taxonomy is found and the terms are successfully showing.
What could be the reason for this, and how can I fix it?
The site is running on WPEngine.