【发布时间】:2018-09-06 19:05:01
【问题描述】:
首先我想说这是我关于 Stack Overflow 的第一个问题,但是多年来我找到了许多有用的解决方案,所以我要感谢所有参与这里的人。
现在,我的第一个问题...
在我的 woocommerce 设置中,每个产品都是数字下载,客户仅限购买一次。出于安全原因,所有下载将在 14 天后过期。
我们有一些客户已经与我们合作长达 5 年,因此,在浏览产品时,我们决定在他们的帐户中提供一个页面,显示他们尚未购买的产品。只是让他们更容易。
现在,我的问题有两个。首先是下面的代码,它显示了他们帐户仪表板中的页面,我无法让 paginav 工作,所以如果你能看到那里可能有什么问题,请告诉我。
但更大的问题是,如何将查询转换为排序函数? 这意味着,如果客户选择他们可以按“尚未购买”进行排序
或者,如果有人知道更简单的方法来完成此操作,请告诉我。
这是我目前用来显示一个页面的代码,该页面显示他们“尚未购买”的产品
// *NOT PURCHASED
add_filter ( 'woocommerce_account_menu_items', 'custom_purchased_products_link', 40 );
add_action( 'init', 'custom_add_products_endpoint' );
add_action( 'woocommerce_account_purchased-products_endpoint', 'custom_populate_products_page' );
// here we hook the My Account menu links and add our custom one
function custom_purchased_products_link( $menu_links ){
// we use array_slice() because we want our link to be on the 3rd position
return array_slice( $menu_links, 0, 2, true )
+ array( 'purchased-products' => 'Not Purchased Yet' )
+ array_slice( $menu_links, 2, NULL, true );
}
// here we register our rewrite rule
function custom_add_products_endpoint() {
add_rewrite_endpoint( 'purchased-products', EP_PAGES );
}
// here we populate the new page with the content
function custom_populate_products_page() {
global $wpdb;
// this SQL query allows to get all the products purchased by the current user
// in this example we sort products by date but you can reorder them another way
$purchased_products_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT itemmeta.meta_value
FROM " . $wpdb->prefix . "woocommerce_order_itemmeta itemmeta
INNER JOIN " . $wpdb->prefix . "woocommerce_order_items items
ON itemmeta.order_item_id = items.order_item_id
INNER JOIN $wpdb->posts orders
ON orders.ID = items.order_id
INNER JOIN $wpdb->postmeta ordermeta
ON orders.ID = ordermeta.post_id
WHERE itemmeta.meta_key = '_product_id'
AND ordermeta.meta_key = '_customer_user'
AND ordermeta.meta_value = %s
ORDER BY orders.post_date DESC
",
get_current_user_id()
) );
// some orders may contain the same product, but we do not need it twice
$purchased_products_ids = array_unique( $purchased_products_ids );
// if the customer purchased something
if( !empty( $purchased_products_ids ) ) :
// it is time for a regular WP_Query
$purchased_products = new WP_Query( array(
'post_type' => 'product',
'paginate' => true,
'posts_per_page' => 50,
'paginate' => true,
'post_status' => 'publish',
'post__not_in' => $purchased_products_ids,
) );
echo '<div class="woocommerce columns-3">';
echo '<h3>Some Products you have not yet purchased (Max display 39)
</h3>';
woocommerce_product_loop_start();
while ( $purchased_products->have_posts() ) : $purchased_products-
>the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
else:
echo 'Nothing purchased yet.';
endif;
}
add_action( 'woocommerce_after_shop_loop_item',
'user_logged_in_product_already_bought', 30 );
【问题讨论】:
标签: php sql wordpress woocommerce shortcode