[英]Replace Upsells with products from a specific product category in Woocommerce 3
I have the following case - So far I've always set the Upsell products custom, 1 by 1, as choosing a product category in the admin is not possible. 我遇到以下情况-到目前为止,我始终会按1设置Upsell产品自定义设置,因为无法在管理员中选择产品类别。
Now after a big cleaning of products that are no longer available, there are many product pages on the site with 1, 2 or 3 instead of the default number of 4 Upsell products, which breaks the design. 现在,在对不再使用的产品进行大范围清洗之后,站点上的许多产品页面上都有1、2或3,而不是默认数量的4个Upsell产品,这破坏了设计。
Is there a way to replace those upsells with products from a specific product category? 有没有一种方法可以用特定产品类别的产品代替那些加价销售?
Any help is appreciated. 任何帮助表示赞赏。
To replace woocommerce upsells by products from a specific product category use the following code (where you will define your product category(ies) slug(s)) : 要用来自特定产品类别的产品替换woocommerce加售,请使用以下代码(您将在其中定义您的产品类别块) :
add_filter( 'woocommerce_product_get_upsell_ids', 'custom_upsell_ids', 20, 2 );
function custom_upsell_ids( $upsell_ids, $product ){
// HERE define your product categories slugs in the array
$product_categories = array( 't-shirts' ); // <===== <===== <===== <===== <=====
// Return the custom query
return wc_get_products( array(
'status' => 'publish', // published products
'limit' => 4, // 4 products
'category' => $product_categories, // Product categories
'orderby' => 'rand', // Random order
'exclude' => array( $product->get_id() ), // Exclude current product
'return' => 'ids', // Query returns only IDs
) );
}
Code goes in function.php file of the active child theme (or active theme). 代码进入活动子主题(或活动主题)的function.php文件中。 Tested and works.
经过测试和工作。
The query is set to display 4 products from a specific product category in a random order (excluding the current product).
查询设置为按随机顺序显示特定产品类别中的4个产品(当前产品除外)。
Official documentation: The wc_get_products()
and WC_Product_Query
官方文档:
wc_get_products()
和WC_Product_Query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.