在WooCommerce中在相关产品之前显示交叉销售 [英] Show cross-sells before same category on related products in WooCommerce
本文介绍了在WooCommerce中在相关产品之前显示交叉销售的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一些代码来修改相关产品部分,如下所示:
I'm writing some code to modify the related products section as follows:
- 如果某个产品具有交叉销售产品,请首先显示这些产品,并与相同类别的其他产品一起填充多达4种产品*
或
- 如果一个产品没有交叉销售产品,请显示4个相同类别的产品*
到目前为止,这是我过滤相关产品的功能:
Here's my function to filter the related products so far:
add_filter( 'woocommerce_related_products', 'fivem_add_linked_to_related_products', 9999, 3 );
function fivem_add_linked_to_related_products( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$cross_sell_ids = $product->get_cross_sell_ids();
$product_categories = $product->get_category_ids();
// Get cross sell products
$cross_sell_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'post__in' => $cross_sell_ids,
'posts_per_page' => 4,
'exclude' => array( $product_id ),
));
// Calculate how many filler products are needed
$category_product_count = 4 - count( $cross_sell_products );
// Exclude main product and cross sell products
$excluded_products = array_push( $cross_sell_ids, $product_id );
// Get filler products from same category
$category_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'rand',
'fields' => 'ids',
'post__not_in' => $excluded_products,
'posts_per_page' => $category_product_count,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_categories,
'operator' => 'IN',
)
)
));
// Merge cross sell products with filler products
$related_products = array_merge( $cross_sell_products, $category_products );
// Return related products
return $related_products;
}
当前,以上代码大部分有效.
Currently, the above code mostly works.
- 如果设置了交叉销售,则仅显示那些交叉销售的产品,即.总数不足4张
- 如果未设置交叉销售,则会显示与预期相同类别的产品.
我要解决两个问题:
- 以上代码未填充类别产品.如果我删除了 post__not_in 和 tax_query 参数,则会填写该参数,但显然不会填充相同类别的产品.
- 我想先展示交叉销售的产品,然后再展示与类别相关的产品.似乎还有另一个随机因素将顺序混在一起,我无法弄清其来源.
- Code above doesn't fill with category products. If I remove the post__not_in and tax_query arguments, it fills out, but obviously not with products from the same category.
- I want to show cross-sell products first, then the category-related products. There appears to be another randomization somewhere that mixes the order up, and I can't work out where that comes from.
有什么想法可以解决此问题吗?预先感谢.
Any ideas how I can fix this? Thanks in advance.
推荐答案
代码包含
- 如果某个产品具有交叉销售产品,请首先显示这些产品,并与同类别的其他产品一起填充多达4种产品
- 如果一个产品没有交叉销售产品,请显示4个相同类别的产品
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {
// Taxonomy
$taxonomy = 'product_cat';
// Show products
$show_products = 4;
// Get product
$product = wc_get_product( $product_id );
// Get cross sell IDs
$cross_sell_ids = $product->get_cross_sell_ids();
// Calculate how many filler products are needed
$category_product_needed_count = $show_products - count( $cross_sell_ids );
// If category product needed
if ( $category_product_needed_count >= 1 ) {
// Retrieves product term ids for a taxonomy.
$product_cats_ids = wc_get_product_term_ids( $product_id, $taxonomy );
// Get product id(s) from a certain category, by category-id
$product_ids_from_cats_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => $category_product_needed_count,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $product_cats_ids,
'operator' => 'IN',
)
),
));
// Merge array
$related_products = array_merge( $cross_sell_ids, $product_ids_from_cats_ids );
} else {
// Slice array until show products
$related_products = array_slice( $cross_sell_ids, 0, $show_products );
}
// Return
return $related_products;
}
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
// Order by
function filter_woocommerce_output_related_products_args( $args ) {
$args['orderby'] = 'id';
$args['order'] = 'ASC';
return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'filter_woocommerce_output_related_products_args', 10, 1 );
这篇关于在WooCommerce中在相关产品之前显示交叉销售的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文