簡體   English   中英

將 WooCommerce 訂單中按產品類別訂購的產品數量相加

[英]Sum the quantities of products ordered by product categories from a WooCommerce order

我真的堅持這一點,我非常感謝任何幫助。

目標是計算 Woocommerce 訂單上每個類別中的商品數量,以便每個部分都可以以類別名稱和產品數量為標題。 例如:

漢堡 x 5

下面將是該訂單上所有漢堡的列表。

現在我以為我可以使用以下代碼:

$categories = [];
foreach ($order->get_items() as $item) {
  $product_id = $item['product_id'];
  $meta = $item['item_meta'];
  $meta = array_filter($meta, function ($key) {
                    return !in_array($key, Order::getHiddenKeys());
  }, ARRAY_FILTER_USE_KEY);
  $terms = get_the_terms($product_id, 'product_cat');
  $cat_id = $terms[0]->term_id;
  $categories[$cat_id][] = $item;
  $cat_name = $terms[0]->name;
}

foreach($categories as $category => $items){
?>
  <tr>
    <td colspan="3">
      <h4>
        <?php 
        if( $term = get_term_by( 'id', $category, 'product_cat' ) ){
          echo $term->name.' ('.$term->count.')';
          echo " &times; ";
          echo count($items);
        }
        ?>
      </h4>
    </td>
  </tr>
  <?php

但我意識到這只是計算該類別中已訂購的產品數量,而不是數量 因此,如果其中一個有多個相同的產品,則不會計算在內。 例如,如果類別包含以下三個項目:

芝士漢堡 x 2
漢堡 x 1

將返回兩個而不是三個。

我一直在查看類別和術語 arrays 但似乎找不到任何可行的方法。 我確定我錯過了一些東西,但我現在只是不知所措。

誰能指出我正確的方向?

(另外,如果它有任何用處,這是上一個問題Filtering the Woocommerce $order items By Category (Term)的間接后續)

您可以通過兩種方式做到這一點:

  1. 根據從產品中獲得的產品類別將訂購的產品數量相加(因此必須只有1)
  2. 設置您想要獲得購買產品數量計數的特定產品類別列表

解決方案#1

您可以使用get_the_terms() function 來獲取產品類別。

function參數必須是要統計的訂單ID。

請注意,只會檢查get_the_terms() function 返回的第一個產品類別。 如果一個產品有多個,你可能會得到意想不到的結果。

// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {

    // initializes the array that will contain the product categories with the ordered quantities of each
    $count_by_cat = array();

    // get the order object
    $order = wc_get_order( $order_id );

    // for each product ordered
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        // get product categories
        $terms = get_the_terms( $product->get_id(), 'product_cat' );
        foreach ( $terms as $term ) {
            $cat_name = $term->name;
            // if the product category is already present in the array, add the quantities
            if ( array_key_exists( $cat_name, $count_by_cat ) ) {
                $count_by_cat[$cat_name] += $item->get_quantity();
            // otherwise it adds the category and quantity to the array
            } else {
                $count_by_cat[$cat_name] = $item->get_quantity();
            }
            break;
        }
    }

    return $count_by_cat;
}

解決方案#2

在這種情況下,您必須使用要獲取訂購產品數量的產品類別名稱列表來初始化一個數組

使用has_term() function 您可以檢查產品是否屬於特定產品類別。

function參數必須是要統計的訂單ID。

// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {

    // initializes the array that will contain the product categories with the ordered quantities of each
    $count_by_cat = array();

    // set the product categories you want to get counts for
    $categories = array(
        'Clothes',
        'Shoes',
        'Pants',
        'Jackets',
        'Hats'
    );

    // get the order object
    $order = wc_get_order( $order_id );

    // for each product ordered
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        // for each product category
        foreach ( $categories as $cat_name ) {
            // check if the product belongs to one of the product categories
            if ( has_term( $cat_name, 'product_cat', $product->get_id() ) ) {
                // if the product category is already present in the array, add the quantities
                if ( array_key_exists( $cat_name, $count_by_cat ) ) {
                    $count_by_cat[$cat_name] += $item->get_quantity();
                // otherwise it adds the category and quantity to the array
                } else {
                    $count_by_cat[$cat_name] = $item->get_quantity();
                }
                break;
            }
        }
    }

    return $count_by_cat;
}

這兩個函數都將返回一個類似於此的數組

$count_by_cat = array(
    'Shoes' => 4,
    'Pants' => 2,
    'Hats'  => 11
)

因此,您可以打印類似於以下內容的 HTML 代碼

// print the counts for each product category
foreach ( $count_by_cat as $category_name => $count ) {
    echo "<p><strong>" . $category_name . ":</strong> " . $count . "</p>";
}

兩種代碼都已經過測試並且可以工作。 將它們添加到您的活動主題的功能中。php。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM