在页面上显示 Woocommerce 中低于特定价格的所有产品

编程入门 行业动态 更新时间:2024-10-27 08:24:58
本文介绍了在页面上显示 Woocommerce 中低于特定价格的所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 Woocommerce 商店,里面有 1000 多种产品.我希望所有价格低于 999 的产品都显示在单独的页面上,以便我可以在菜单中标记该页面.

有可能吗?

解决方案

更新: (将 'type' => 'DECIMAL', 添加到meta_query 数组)

这可以使用

您将获得:

2) 展示超过特定数量的产品

您将粘贴以下短代码示例,并带有 class 参数值 above-50 (对于价格高于 <代码>50):

[products limit="16";分页=真"列=4"班级=50 岁以上"]


可用的简码参数和设置:Woocommerce 简码文档

I have a Woocommerce store with more than 1000 products. I would like that all products which have a price below 999 should be shown on a separate page, so I can tag that Page in my menu.

Is it possible?

解决方案

Update: (added 'type' => 'DECIMAL', to the meta_query array)

This can be done using Woocommerce shortcode [products] to be used on a page, with the following additional code (that will add the possibility to define a price to be compared through an existing argument):

add_filter( 'woocommerce_shortcode_products_query', 'products_based_on_price', 10, 3 );
function products_based_on_price( $query_args, $atts, $loop_name ) {
    if( ! ( isset($atts['class']) && ! empty($atts['class']) ) )
        return $query_args;

    if (strpos($atts['class'], 'below-') !== false) {
        $compare   = '<';
        $slug    = 'below-';
    } elseif (strpos($atts['class'], 'above-') !== false) {
        $compare   = '<';
        $slug    = 'above-';
    }

    if( isset($compare) ) {
        $query_args['meta_query'][] = array(
            'key'     => '_price',
            'value'   => (float) str_replace($slug, '', $atts['class']),
            'type'    => 'DECIMAL',
            'compare' => $compare,
        );
    }
    return $query_args;
}

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


##USAGE:

Here we use the unused class argument to pass the price and the comparison operator.

1) DISPLAY PRODUCTS BELOW A SPECIFIC AMOUNT (YOUR CASE)

You will paste the following shortcode example with as class argument value below-999 (for products that have a price below 999):

[products limit="16" paginate="true" columns="4" class="below-999"]

The wordpress page text content editor:

You will get:

2) DISPLAY PRODUCTS ABOVE A SPECIFIC AMOUNT

You will paste the following shortcode example with as class argument value above-50 (for products that have a price above 50):

[products limit="16" paginate="true" columns="4" class="above-50"]


Available shortcode arguments and settings: Woocommerce shortcodes documentation

这篇关于在页面上显示 Woocommerce 中低于特定价格的所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 06:47:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   价格   产品   Woocommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!