我正在使用WooCommerce创建自定义报告,并且该报告正在运行,但是它正在使用大量资源,并且它需要更大,并且如果我增加它的大小将会超时。我相信有更好的方法可以做到这一点。
我正在运行多个查询,以查看订购了多少种特定颜色和尺寸的产品。当前,这些是单独的查询,效率不高。这是我的代码:
这是获取订购数量的函数:
function get_order_by_product_id($product_id, $product_color, $product_size) {
$args = array (
'limit' => '-1',
'status' => 'processing',
);
$orders = wc_get_orders( $args );
if($orders) :
$total = 0;
foreach( $orders as $order) :
$order_id = $order->get_id();
$single_order = wc_get_order( $order_id );
foreach ($single_order->get_items() as $item_id => $item_data) :
$product = $item_data->get_product();
$productID = $single_order->get_item_meta($item_id, '_product_id', true);
$product_name = $product->get_name();
$color = $single_order->get_item_meta($item_id, 'pa_colors', true);
$size = $single_order->get_item_meta($item_id, 'pa_sizes', true);
$item_quantity = $item_data->get_quantity();
if($productID == $product_id && $color == $product_color && $size == $product_size ) :
$total += $item_quantity;
endif;
endforeach;
endforeach;
echo $total;
endif;
}
这是对该函数的调用:
get_order_by_product_id(47652, 'black', 'xs');
当前,为了使此功能生效,我要针对每个产品ID,颜色和尺寸一遍又一遍地调用该函数。就像我说的那样,它有效,但是却是资源浪费。
任何提高效率的想法将不胜感激!
答案 0 :(得分:1)
这可以在具有以下功能的简单的SQL查询中完成:
/*
* @param (int) $product_id
* @param (string) $colors slug term for "pa_colors" product attribute
* @param (string) $sizes slug term for "pa_sizes" product attribute
* @param (string) $status Optional: Order status ("processing" by default)
**/
function get_ordered_product_count( $product_id, $colors, $sizes, $status='processing' ){
return $wpdb->get_var( "
SELECT SUM(woim.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim1 ON woi.order_item_id = woim1.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim3 ON woi.order_item_id = woim3.order_item_id
WHERE p.post_type LIKE 'shop_order'
AND p.post_status LIKE 'wc-$status'
AND woim.meta_key LIKE '_qty'
AND woim1.meta_key LIKE 'pa_colors'
AND woim1.meta_value LIKE '$colors'
AND woim2.meta_key LIKE 'pa_sizes'
AND woim2.meta_value LIKE '$sizes'
AND woim3.meta_key LIKE '_product_id'
AND woim3.meta_value = $product_id
" );
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
用法<示例>示例:
get_ordered_product_count(47652, 'black', 'xs');
get_ordered_product_count(47652, 'black', 'xs', 'completed');
答案 1 :(得分:0)
要对所有具有特定属性eq(颜色为“蓝色”或“黑色”)的所有(已发布)产品进行计数,可以使用:
$colors = 'black, blue';
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'pa_color',
'terms' => explode(",",$colors),
'field' => 'slug',
'operator' => 'IN'
)
)
);
echo count (wc_get_products($args));