我通过用户登录在其帐户页面中显示购买的产品。我使用此链接https://www.codesd.com/item/woocommerce-list-all-the-products-previously-ordered.html上的说明实施了该操作。我只想显示产品类型为“简单”的购买产品,而不显示所有购买的产品。
我使用以下查询:
global $wpdb;
$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;
$customer_data = array( $customer_email );
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$result = $wpdb->get_col( "
SELECT im.meta_value FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND im.meta_key IN ( '_product_id', '_variation_id' )
AND im.meta_value != 0
AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
" );
要仅显示产品类型为“简单”的产品,将插入正确的sql查询是什么?
答案 0 :(得分:1)
仅可以使用以下代码将此查询限制为简单产品,在该代码中,我们基于product_type
Woocommerce分类法(管理产品类型)进行子“分类法查询” 词条simple
(简单产品):
global $wpdb;
$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;
$customer_data = array( $customer_email );
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$result = $wpdb->get_col( "
SELECT im.meta_value
FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm
ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i
ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im
ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND im.meta_key = '_product_id'
AND im.meta_value IN (
SELECT tr.object_id
FROM {$wpdb->prefix}term_relationships as tr
INNER JOIN {$wpdb->prefix}term_taxonomy as tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t
ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'product_type'
AND t.slug = 'simple'
)
AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
GROUP BY im.meta_value
" );
// The raw output (testing
echo '<pre>'; print_r($result); echo '</pre>';
经过测试可以正常工作。