Woocommerce - 显示特色产品订单的数量

时间:2015-12-29 14:49:43

标签: php wordpress function woocommerce featured

我想在子页面上显示一种摘要,目前我的商店销售多少特色产品。每个月我将只有一个特色项目(30天后我添加新的,将以前产品的状态从特色更改为正常)。

我目前的代码如下所示:

function woo_featured_product_sales_qty( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed',
        'meta_query' => array(
            array(
                'post_type' => 'product',  
                'meta_key' => '_featured',  
                'meta_value' => 'yes', 
                'order' => 'DESC'
            )
        ),
    ), $atts );

    $status_list = $args['status'];

    $statuses = array_map( 'trim', explode( ',', $status_list ) );

    $order_count = 0;

    foreach ( $statuses as $status ) {

        $status = str_replace( $status, 'wc-' . $status, $status );

        $total_orders = wp_count_posts( 'shop_order' )->$status;

        $order_count += $total_orders;
    }
    ob_start();

    echo $order_count;

    return ob_get_clean();
}

add_shortcode( 'featured_product_sales_qty', 'woo_featured_product_sales_qty' );

此代码确实显示总订单但是总和。怎么做,让它正常工作?如何根据当前特色产品显示该订单数量?

1 个答案:

答案 0 :(得分:1)

我会简化一些事情,为这些帖子做一个简单的WP_Query

function woo_featured_product_sales_qty( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed',
        'meta_query' => array(
            array(
                'post_type' => 'product',  
                'meta_key' => '_featured',  
                'meta_value' => 'yes', 
                'order' => 'DESC'
            )
        ),
    ), $atts );

    $status_list = $args['status'];

    $statuses = array_map( 'trim', explode( ',', $status_list ) );

    $query_args = array(
        'post_status' => $statuses,
        'meta_query' => $args['meta_query'],
    );
    $posts = new WP_Query( $query_args );

    return $posts->have_posts() ? $posts->found_posts : 0;
}

add_shortcode( 'featured_product_sales_qty', 'woo_featured_product_sales_qty' );