Woocommerce - 在类别页面顶部显示特色产品 [英] Woocommerce - Display Featured Products at top of Category Page

查看:32
本文介绍了Woocommerce - 在类别页面顶部显示特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个产品类别页面的顶部有一个部分,显示该类别中随机的三个特色产品.在此之下将是常规存档循环.

I want to have a section at the top of each product category page that shows three featured products at random from that category. Beneath this would be the regular archive loop.

在不使用插件的情况下实现这一目标的最佳方法是什么?

What's the best way to achieve this, without using a plugin?

推荐答案

下面的代码可以帮到你:

Below code can help you:

    add_filter('posts_orderby', 'show_featured_products_orderby',10,2);
function show_featured_products_orderby($order_by, $query){
  global  $wpdb ;
  if( ($query->get('post_type')=='product') && (!is_admin()) ){
    $orderby_value = ( isset( $_GET['orderby'] ) ? wc_clean( (string) $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ) );
    $orderby_value_array = explode( '-', $orderby_value );
    $orderby = esc_attr( $orderby_value_array[0] );
    $order = ( !empty($orderby_value_array[1]) ? $orderby_value_array[1] : 'ASC' );
    $feture_product_id = wc_get_featured_product_ids();
    if ( is_array( $feture_product_id ) && !empty($feture_product_id) ) {
      if ( empty($order_by) ) {
        $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC ";
      } else {
        $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC, " . $order_by;
      }
    }  
  }
  return $order_by;
}

将此代码添加到活动主题的function.php文件中

Add this code to active themes function.php file

这篇关于Woocommerce - 在类别页面顶部显示特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆