【问题标题】:Display the Min and Max prices in Woocommerce Product Category Archives在 Woocommerce 产品类别档案中显示最低和最高价格
【发布时间】:2018-05-25 08:19:13
【问题描述】:

如何显示当前类别中相关产品的最低和最高价格。

只有 2 个数字 - 最便宜产品类别的价格和最昂贵产品类别的价格。

【问题讨论】:

    标签: wordpress woocommerce categories product hook-woocommerce


    【解决方案1】:

    这是一个在 single_term_title Wordpress 特定过滤器挂钩中挂钩的自定义函数示例,它将产品价格范围添加到类别标题。

    它从 SQL 查询中获取当前产品类别的所有产品价格。然后在标题后显示相关产品的价格范围:

    add_filter( 'single_term_title', 'product_category_term_title', 10, 1 );
    function product_category_term_title( $term_title ){
        // Only for product category archive pages
        if( ! (is_tax() && is_product_category() ) )
            return $term_title;
    
        // Get the current product category term object
        $term = get_queried_object();
    
        global $wpdb;
    
        # Get ALL related products prices related to a specific product category
        $results = $wpdb->get_col( "
            SELECT pm.meta_value
            FROM {$wpdb->prefix}term_relationships as tr
            INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
            INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
            INNER JOIN {$wpdb->prefix}postmeta as pm ON tr.object_id = pm.post_id
            WHERE tt.taxonomy LIKE 'product_cat'
            AND t.term_id = {$term->term_id}
            AND pm.meta_key = '_price'
        ");
    
        // Sorting prices numerically
        sort($results, SORT_NUMERIC);
    
        // Get the min and max prices
        $min = current($results);
        $max = end($results);
    
        // Format the price range after the title
        $price_range = sprintf( __( ' <small>(from %1$s to %2$s)</small>', 'woocommerce' ), wc_price( $min ), wc_price( $max ) );
        return $term_title . $price_range;
    }
    

    经过测试并且有效。你会得到类似的东西:

    【讨论】:

    • 只是为了记住。排除目录中不可见的产品会很好。我目前正在尝试完成这项工作并编辑您的帖子。
    【解决方案2】:
    // remove blank price element from array.
    $resultDirect = array_filter($results);
    // min and max value from array
    $min = min($resultDirect);
    $max = max($resultDirect);
    

    【讨论】:

    • 为您的答案添加一些描述。
    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多