2017-02-23 1 views
0

각 카테고리 페이지 상단에 총 상품 판매량을 표시하려고합니다.WooCommerce 카테고리 총 판매량 표시

이 총액의 기부금이 기부되며 고객이 금액을 광고하기를 원합니다. 나는 지금 몇 시간 동안 물건을보고, 시험해보고 있었고, 어떤 운도 가지지 않고 있었다. 전체 매장의 총 판매량을 표시하는 플러그인을 찾았지만 한 카테고리 만 있으면됩니다.

이 플러그인에 다른 필터를 추가하려고했지만 행운이 없습니다. 이견있는 사람?

+0

질문을 다시 말하고 확장하고 현재 코드를 추가해야합니다. 당신이 무엇을 요구하고 있는지는 매우 불분명합니다. –

답변

1
function getSaleAmountbyCategoryID($product_cat_id) 
{ 
    //first getting all the Product ID by Category ID 
    $pro_args = [ 
     'post_type' => 'product', 
     'pages_per_post' => -1, 
     'post_status' => 'publish', 
     'fields' => 'ids', 
     'tax_query' => [ 
      [ 
       'taxonomy' => 'product_cat', 
       'field' => 'term_id', //This is optional, as it defaults to 'term_id' 
       'terms' => $product_cat_id, //(int) 
      ] 
     ] 
    ]; 
    $product_ids_query = new WP_Query($pro_args); 

    $include_product_id = $product_ids_query->posts; 

    //getting all the success orders 
    $args = [ 
     'post_type' => 'shop_order', 
     'posts_per_page' => '-1', 
     'post_status' => ['wc-processing', 'wc-completed', 'wc-onhold'] 
    ]; 
    $my_query = new WP_Query($args); 

    $total = 0; 

    $orders = $my_query->posts; 
    foreach ($orders as $ctr => $value) 
    { 
     $order_id = $value->ID; 
     //getting order object 
     $order = wc_get_order($order_id); 

     $items = $order->get_items(); 

     foreach ($items as $item_data) 
     { 
      $product_id = $item_data['item_meta']['_product_id'][0]; 
      if (in_array($product_id, $include_product_id)) 
      { 
       //getting product object 
       //$_product = wc_get_product($product_id); 
       //$qty = $item_data['item_meta']['_qty'][0]; 
       $pro_price = $item_data['item_meta']['_line_total'][0]; 
       $total += $pro_price; 
      } 
     } 
    } 
    return $total; 
} 

제품 카테고리 페이지 템플릿에서 ^^ 위의 코드 활성 테마 functions.php

USAGE
추가 참고

echo getSaleAmountbyCategoryID(21); //replace it by your product caregory ID 

처럼 전화 : I 1000 개 주문이있는 경우 속도가 느려지므로 이것이 최고의 해결책이라고 말하지 않을 것입니다. 더 나은 해결책을 찾으면 내 대답을 업데이트 할 것입니다.

희망이 있습니다.

+0

감사! 이것은 매력처럼 작동합니다! – bigbluehouse

+0

@bigbluehouse : 도움이 되었기 때문에 기쁩니다. 그러나 질문을 해결할 때 내 대답을 수락하는 것을 잊지 마십시오. –

관련 문제