将 WooCommerce 订单中按产品类别订购的产品数量相加

时间:2021-02-18 17:41:13

标签: php wordpress woocommerce

我真的很坚持这个,我真的很感激这方面的任何帮助。

目标是计算 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

将返回两个而不是三个。

我一直在查看类别和术语数组,但似乎找不到任何可行的方法。我确定我错过了一些东西,但我现在不知所措。

有人能指出我正确的方向吗?

(另外,如果它有任何用处,这是对上一个问题 Filtering the Woocommerce $order items By Category (Term) 的间接后续)

1 个答案:

答案 0 :(得分:2)

您可以通过两种方式执行此操作:

  1. 根据从产品中获取的产品类别将订购产品的数量相加(因此必须只有 1 个)
  2. 设置一个特定产品类别的列表,您希望获得所购买产品的数量计数

解决方案#1

您可以使用 get_the_terms() 函数获取产品类别。

<块引用>

函数参数必须是要统计的订单ID。

请注意,只会检查 get_the_terms() 函数返回的第一个产品类别。 如果一个产品有多个,您可能会得到意想不到的结果。

// 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() 函数,您可以检查产品是否属于特定的产品类别。

<块引用>

函数参数必须是要统计的订单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>";
}

两个代码都已经过测试并且可以工作。将它们添加到您的活动主题的functions.php。