在 WooCommerce 中按类别获取全球订单商品数量总和

Get global order items quantity sum by category in WooCommerce

我想问一下有没有人知道如何在WooCommerce中统一产品。我有订单,我只需要最后的值和 INT 形式的变量,这样我就可以继续使用它。

这是我的代码:

// set the product categories you want to get counts for
$categories = array(
    'pro',
    'medium',
    'basic',
);

if ( count( $subscriptions ) > 0 ) {
    // Loop through customer subscriptions
    foreach ( $subscriptions as $subscription ) {
        // Get the initial WC_Order object instance from the subscription
        $order = wc_get_order( $subscription->get_parent_id() );

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product(); // Get the product object instance
            // Target only subscriptions products type
            // print_r($product->get_name());
            if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                $quantity = $item->get_quantity(); // Get the quantity
                echo '<p>Quantity: ' . $quantity . '</p>';
            }
            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;
                }
            }
            foreach ( $count_by_cat as $category_name => $count ) {
                echo "<p><strong>" . $category_name . ":</strong> " . $count . "</p>";
            }
        }
    }
}

下面的截图,就是我要得到的:

要按类别获取全球订单商品总数,请使用以下重新访问的代码:

$count_by_cat = array(); // Initializing

// Below set the product categories you want to get counts for
$categories = array( 'pro', 'medium', 'basic' );

if ( count( $subscriptions ) > 0 ) {
    // Loop through customer subscriptions
    foreach ( $subscriptions as $subscription ) {
        // Get the initial WC_Order object instance from the subscription
        $order = wc_get_order( $subscription->get_parent_id() );

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product(); // Get the product object instance
            
            // Target only subscriptions products type
            if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                foreach ( $categories as $category ) {
                    // check if the product belongs to one of the product categories
                    if ( has_term( $category, 'product_cat', $item->get_product_id() ) ) {
                        // if the product category is already present in the array, add the quantities
                        if ( array_key_exists( $category, $count_by_cat ) ) {
                            $count_by_cat[$category] += $item->get_quantity();
                        } 
                        // otherwise it adds the category and quantity to the array
                        else {
                            $count_by_cat[$category] = $item->get_quantity();
                        }
                    }
                }
            }
        }
    }
    if ( ! empty($count_by_cat) ) {
        foreach ( $count_by_cat as $category => $quantity ) {
            echo '<p><strong>' . $category . '</strong>: ' . $quantity . '</p>';
        }
    }
}

它现在应该给出预期的输出。