1
votes

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.

1
When using some answer code in your own code, you should always add the link in your question, as this is a fair reward for the original code author...LoicTheAztec
Your question problem is mostly impossible to reproduce… Did you tried to disable Klarna payments and enable some other payment method to see what happen when you make a new order for testing with a different payment method? If the problem comes from klarna payments only, you should report that in their support pages. Also try to disable hosting cache to see what happen, as this can be a hosting setting problem.LoicTheAztec
@LoicTheAztec I apologize, I'll do that i the future. Yes, I did understand that it would be hard to reproduce, I was kind of hoping that someone had encountered this issue before and could give me quick: "Try this or that". I tried to disable the hosting cache, but that did not help.Robert Ahlin Toftegaard

1 Answers

1
votes

It turned out to be the Klarna plugin that caused the "missing taxonomy" issue. After contacting the plugin developer, https://krokedil.se/, I got the solution.

I had to add a priority lower than 10 to the add_action('init', 'register_taxonomies');-call.

Ergo: add_action('init', 'register_taxonomies', 9);