dongwei1895 2018-09-08 16:39
浏览 101

在Woocommerce中列出没有短代码的特色产品和On Sale产品

I understand WooCommerce has provided shortcodes to use for featured and sale products.

However, I can use the shortcode on a page or widget where I want to display them.

What I actually want to do is create a link for sale and one for featured products which will list on sale products and featured products respectively.

Is there anyway I can do that without the shortcode in the way product categories are listed?

  • 写回答

1条回答 默认 最新

  • duanlun1955 2018-09-10 16:21
    关注

    The following code will filter products from woocommerce product query to:

    • Display featured products using https://www.example.com/shop/?featured=1 query string
    • Display on sale products using https://www.example.com/shop/?onsale=1 query string

    The code:

    // Featured products
    add_filter( 'woocommerce_product_query_tax_query', 'filter_featured_products', 20, 1 );
    function filter_featured_products( $tax_query ){
        if( isset($_GET['featured']) && $_GET['featured'] ){
            $tax_query[] = array(
                'taxonomy'  => 'product_visibility',
                'field'     => 'name', // name or term_id
                'terms'     => array('featured')
            );
        }
        return $tax_query;
    }
    
    // On sale products    
    add_filter( 'woocommerce_product_query_meta_query', 'filter_on_sale_products', 20, 1 );
    function filter_on_sale_products( $meta_query ){
        if( isset($_GET['onsale']) && $_GET['onsale'] ){
            $meta_query[] = array(
                'key' => '_sale_price',
                'value' => 0,
                'compare' => '>'
            );
        }
        return $meta_query;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧