所以我在网上查了很多资料,但找不到解决方案......
基本上我想做的是显示用户在商店中购买的所有产品的产品循环,就像显示普通产品一样。
如果你还是不明白,也许这会帮助你明白我的意思。
这是 WooCommerce 文档中的示例产品循环...
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
那么,如果我想显示基本上相同的产品循环,但是将其过滤掉,以便它只显示用户已经购买的产品,该怎么办呢?
老实说,我不知道该去哪里,而且我确信过去还有其他人对此进行过研究,所以也许这会帮助很多人!
提前致谢!
您至少可以采取两种不同的方法来解决此问题。
第一个是从每个帖子中获取产品,然后从每个产品中获取产品 ID,然后使用 if 语句使用 wc_customer_bought_product 或 woocommerce_customer_bought_product 进行过滤(如果您使用的是旧版 WooCommerece)。
The second is to pass the correct arguments to filter the WP_Query to only include orders purchased by a user and then filter products only in those orders. More information on the second approach is available at Get All User Orders and Products bought by user in WooCommerce based shop (archive.org) https://web.archive.org/web/20150209150105/http://fusedpress.com/blog/get-all-user-orders-and-products-bought-by-user-in-woocommerce/.
第一种方法的一个例子是这样的
<!-- code started -->
<ul class="products">
<?php
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$customer_email = $current_user->email;
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
if (wc_customer_bought_product($customer_email, $user_id,$_product->id)){
woocommerce_get_template_part( 'content', 'product' );
}
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)