簡體   English   中英

woocommerce_order_item_name 過濾器鈎子中無法識別自定義分類法

[英]Custom taxonomy not recognized in woocommerce_order_item_name filter hook

我在 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 上運行。

原來是 Klarna 插件導致了“缺少分類法”問題。 聯系插件開發者https://krokedil.se/ 后,我得到了解決方案。

我不得不向add_action('init', 'register_taxonomies');添加一個低於 10 的優先級add_action('init', 'register_taxonomies'); -稱呼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM