微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

woocommerce_order_item_name过滤器挂钩中无法识别自定义分类法

如何解决woocommerce_order_item_name过滤器挂钩中无法识别自定义分类法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。