處理WooCommerce的自訂分類法在wc_get_products函數中。
P粉404539732
P粉404539732 2023-07-19 16:17:09
0
1
473

我使用標準動作註冊一個分類法:

add_action( 'init', 'product_brand_order_taxonomy' );
function product_brand_order_taxonomy()  {
    $labels = array(
        'name'                       => 'Brand Heirarchy',
        'singular_name'              => 'Brand Heirarchy',
        'menu_name'                  => 'Brand Heirarchy',
        'all_items'                  => 'All Brand Heirarchies',
        'parent_item'                => 'Parent Brand Heirarchy',
        'parent_item_colon'          => 'Parent Brand Heirarchy:',
        'new_item_name'              => 'New Brand Heirarchy Name',
        'add_new_item'               => 'Add New Brand Heirarchy',
        'edit_item'                  => 'Edit Brand Heirarchy',
        'update_item'                => 'Update Brand Heirarchy',
        'separate_items_with_commas' => 'Separate Brand Heirarchy with commas',
        'search_items'               => 'Search Brand Heirarchies',
        'add_or_remove_items'        => 'Add or remove Brand Heirarchies',
        'choose_from_most_used'      => 'Choose from the most used Brand Heirarchies',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'brand_heirarchy', 'product', $args );
    register_taxonomy_for_object_type( 'brand_heirarchy', 'product' );
}

後來,我希望根據一些參數來取得產品。我還希望品牌層級(brand_heirarchy)包含在結果中。

$products = array(
    'post_status' => 'publish',
    'limit' => -1,
    'category' => $parent_brand_slugs
);

$products = wc_get_products($product_args);

在傳回的資料中,它會給我類別ID,例如:

[category_ids] => Array
    (
        [0] => 30
        [1] => 27
        [2] => 25
        [3] => 24
    )

但我希望它還能回傳與產品相關聯的品牌層級的ID。

我看到的大多數內容都是關於如何透過自訂分類法篩選產品,我只想知道哪些分類法與產品相關聯。

我發現Woocommerce不允許這樣做:https://github.com/woocommerce/woocommerce/issues/13138

我嘗試使用過濾器woocommerce_product_object_query_args,但無法弄清楚如何使用。還有一個產品搜尋過濾器,但似乎不適用。

目前,我覺得最好的方法是循環遍歷每個產品,並使用類似wp_get_object_terms($data['id'], 'brand_heirarchy')的方法,但這似乎效率不高。

我很久沒有使用WordPress了,所以我真的想知道是否有更有效率的方法可以取得與每個產品相關聯的分類法。

謝謝。

哦,wp_get_object_terms和wp_get_post_terms似乎傳回空數組,而產品確實被分配了品牌層級。

P粉404539732
P粉404539732

全部回覆(1)
P粉770375450

使用wc_get_products()完全可以處理自訂分類法。你沒有在正確的地方搜索,因為這是在WC_Product_Data_Store_CPT類別中使用可用的過濾器鉤子woocommerce_product_data_store_cpt_get_products_query來處理的。

因此,對於你的自訂產品分類法brand_heirarchy,你將使用以下程式碼:

add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
function handle_custom_query_var( $query_args, $query_vars ) {
    if ( ! empty( $query_vars['brand'] ) ) {
        $query_args['tax_query'][] = array(
            'taxonomy'          => 'brand_heirarchy',
            'field'             => 'slug',
            'terms'             => $query_vars['brand'],
        );
    }
    return $query_args;
}

然後現在你可以在wc_get_products()函數中使用你的自訂分類法,例如:

$products = wc_get_products( array(
    'status' => 'publish',
    'limit' => -1,
    'brand' => $brand_slugs // array
) );

現在應該可以了

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!