与许多其他用户一样,我也在商店中使用相关产品。我试图对其进行修改,以使其将最后一个单词替换为活动类别名称。因此,当产品属于床类别时,相关的产品文本为这些是我们最喜欢的床。是枕头类别,是我们最喜欢的枕头,依此类推。提示是,它始终使用活动类别。
示例: 睡眠(父母类别)>枕头(子类别)>产品名称。在这种情况下,它将显示枕头。
示例:睡眠>产品名称。在这种情况下,它使用单词 sleep 。
我现在遇到的问题是,它只能使用同一类别的产品。因此,当它是枕头时,它应该仅显示枕头,但就我而言,它还显示父类别的产品。我该如何解决,使其只显示活动类别中的产品?
// Rename Related Products
function get_favorite_category_title_for( $product_id ) {
$title = __('This could be interesting', 'woocommerce');
$cats = $cats = wp_get_post_terms( $product_id, 'product_cat', array('orderby'=> 'id', 'order'=>'DESC') );
if( count($cats) > 0 ) {
$title = __( 'Unsere beliebtesten ', 'woocommerce' ) . $cats[0]->name;
}
return $title;
}
答案 0 :(得分:0)
请尝试以下未经测试的代码,没有任何保证(基于其他一些相关答案):
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products( $related_posts, $product_id, $args ){
// Get the product categories
$terms = wp_get_post_terms( $product_id, 'product_cat' );
$related_posts = wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'category' => array(reset($terms)->slug),
'orderby' => 'ID',
'order' => 'DESC',
'return' => 'ids',
) );
return $related_posts;
}
代码在您的活动子主题(或活动主题)的function.php文件上。可以。