问题描述
我在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术语。
受 Display Product Brand and Name in Woocommerce Orders and email notifications 答案代码的启发,我添加了以下代码:
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上运行。
解决方法
原来是导致“缺少分类法”问题的Klarna插件。与插件开发者https://krokedil.se/联系后,我得到了解决方案。
我必须为add_action('init','register_taxonomies');
呼叫添加低于10的优先级。
Ergo:
add_action('init','register_taxonomies',9);