php - 对 WooCommerce 订单中按产品类别订购的产品数量求和

标签 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. 根据从产品获取的产品类别将订购的产品数量相加(因此只能是 1)
  2. 设置您想要获取其购买产品数量的特定产品类别列表

解决方案#1

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

The function parameter must be the ID of the order to be counted.

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

The function parameter must be the ID of the order to be counted.

// 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中。

关于php - 对 WooCommerce 订单中按产品类别订购的产品数量求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66265238/

相关文章:

php - yaml中的空字段

php - 使用 .htaccess 在除根目录之外的任何地方阻止 index.php

php - 如何使用下一页帖子在 wordpress 中列出我的帖子

php - 如何设置扩展 WordPress 编码标准的 PHP CodeSniffer + VSCode 中的自动修复错误?

javascript - WooCommerce 购物车页面 : Remove Item Button move place

php - 复制具有特定扩展名 php4 的文件

php - Codeigniter 弃用 : mysql_real_escape_string():

javascript - 使用 WordPress 类别的 Google map 位置过滤器

php - WooCommerce:更改相关产品标题并删除/更改 <h2>

php - 当特定产品类别位于 Woocommerce 购物车中时有条件地应用 CSS 样式