1

我在 WooCommerce 网站上遇到了一些不一致的行为。

我为产品帖子类型添加了一个自定义分类,称为“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);
}

我想在“新订单”电子邮件消息中的每个产品名称前显示选定的 product_brand 术语。
在 Woocommerce 订单和电子邮件通知应答代码中显示产品品牌和名称的启发,我添加了以下代码:

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 );

这在我使用支票付款的暂存站点上运行良好。但是在我使用外部支付网关(Klarna)的实时站点上,找不到分类法。正在taxonomy_exists( 'product_brand' )回归。false

但是,如果我从订单管理页面手动重新发送“新订单”消息,则会找到分类并成功显示条款。

这可能是什么原因,我该如何解决?

该站点在 WPEngine 上运行。

4

1 回答 1

1

原来是 Klarna 插件导致了“分类缺失”问题。联系插件开发者https://krokedil.se/后,我得到了解决方案。

add_action('init', 'register_taxonomies');我必须在-call中添加低于 10 的优先级。

尔格: add_action('init', 'register_taxonomies', 9);

于 2020-10-07T09:03:11.033 回答