【发布时间】:2021-07-24 10:31:29
【问题描述】:
我想问是否有人知道如何在 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>";
}
}
}
}
在下面的截图中,这是我想要得到的:
【问题讨论】:
标签: php wordpress woocommerce woocommerce-subscriptions product-quantity