在WooCommerce的wc_get_products函数中处理自定义分类法

huangapple go评论68阅读模式
英文:

Handling a custom taxonomy in WooCommerce wc_get_products function

问题

我使用标准操作注册了一个分类法:

add_action('init', 'product_brand_order_taxonomy');
function product_brand_order_taxonomy() {
	$labels = array(
		'name'                       => '品牌层次结构',
		'singular_name'              => '品牌层次结构',
		'menu_name'                  => '品牌层次结构',
		'all_items'                  => '所有品牌层次结构',
		'parent_item'                => '父级品牌层次结构',
		'parent_item_colon'          => '父级品牌层次结构:',
		'new_item_name'              => '新品牌层次结构名称',
		'add_new_item'               => '添加新的品牌层次结构',
		'edit_item'                  => '编辑品牌层次结构',
		'update_item'                => '更新品牌层次结构',
		'separate_items_with_commas' => '使用逗号分隔品牌层次结构',
		'search_items'               => '搜索品牌层次结构',
		'add_or_remove_items'        => '添加或移除品牌层次结构',
		'choose_from_most_used'      => '从最常用的品牌层次结构中选择',
	);
	$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_hierarchy', 'product', $args);
	register_taxonomy_for_object_type('brand_hierarchy', 'product');
}

稍后,我想根据一些参数获取产品。我还希望结果中包含 brand_hierarchy

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

$products = wc_get_products($product_args);

在返回的数据中,它会给我 category_ids,如下所示:

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

但我也想让它返回与产品关联的 brand_hierarchy 的ID。

大多数我看到的内容都是关于如何按自定义分类法筛选产品,我只是想知道与产品关联的分类法是什么。

我看到了这个说 Woocommerce 不支持这个功能:https://github.com/woocommerce/woocommerce/issues/13138

我尝试使用了这个过滤器 woocommerce_product_object_query_args,但是无法弄清楚。还有一个产品搜索过滤器,但看起来似乎不适用。

此时,似乎最好的选择是循环遍历每个产品,并使用类似 wp_get_object_terms( $data['id'], 'brand_hierarchy' ); 的方式,但这似乎效率不高。

已经很长时间没有做过 WordPress 了,所以真正想知道是否有更高效的方法可以获取与每个产品关联的分类法。

谢谢。

英文:

I register a taxonomy using the standard action:

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

Later I'd like to get products based on some arguments. I'd also like the brand_heirarchy to be included in the result.

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

$products = wc_get_products($product_args);

In the returned data it will give me category_ids like

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

But I would like to have it also return the ids of the brand_heirarchy that are associated with the products.

Most things I see is about how to filter products by a custom taxonomy, I just want to know what taxonomies are as associated with a product.

I ran across this saying Woocommerce doesn't allow this: https://github.com/woocommerce/woocommerce/issues/13138

I looked at using this filter woocommerce_product_object_query_args but couldn't figure it out. There is also a product search filter, but it didn't look like it applied.

At this point, it seems my best bet is to loop over each product and use something like wp_get_object_terms( $data['id'], 'brand_heirarchy' ); but that seems inefficient.

Been a long time since I've done WordPress so really asking if there's a more efficient way I should be getting the taxonomies associated with each product.

Thanks.

答案1

得分: 1

使用 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 // 数组
) );

现在应该可以工作。


相关文档:

英文:

It is completely possible to handle a custom taxonomy using wc_get_products(). You didn't search in the right place as this is handled within WC_Product_Data_Store_CPT class using the available filter hook woocommerce_product_data_store_cpt_get_products_query.

So for your custom product taxonomy brand_heirarchy, you will use the following:

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

Then now you can use your custom taxonomy in wc_get_products() function like:

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

Now it should work.


Related documentation:

huangapple
  • 本文由 发表于 2023年7月18日 07:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定