我正在使用Woocommerce创建一个会员网站,需要显示membershop还剩多长时间,我猜我需要进行DB查询才能获得它,但是否有我错过的功能显示产品信息,我需要产品名称和客户购买的日期?
提前感谢
编辑:我有点接近了,但仍然没有得到订单日期。。。这是我唯一需要的信息:
function customer_membership(){
global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$customer_email = $current_user->email;
if ( wc_customer_bought_product( $customer_email, $user_id,\'2258\')) {
$args = array(
\'post_type\' => \'shop_order\',
\'post_status\' => \'publish\',
\'meta_key\' => \'_customer_user\',
\'posts_per_page\' => \'-1\'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = \'2258\';
if ( $product_id == XYZ ) {
echo \'Your membership ends on DATE\';
} // end if product
} // end orders
} // endif customer bought
} // end function
我在WC\\U Abstract\\u order中找到了我需要的摘录($order\\u date),但不知道如何使用它,有人能帮我在echo中显示订单日期吗:)
SO网友:koko5678
我认为你太难从数据库中保存和提取这些信息了。
我建议您对订单使用自定义元字段,如下所示:
//Save it after the process
add_action( \'woocommerce_checkout_update_order_meta\', \'purchase_date_save\' );
function purchase_date_save( $order_id ) {
$get_current_date_time = date(\'Y-m-d\');
update_post_meta( $order_id, \'my_purchase_date\', sanitize_text_field( $get_current_date_time ) );
}
您可以这样访问它:
$get_value = get_post_meta( $order->id, \'my_purchase_date\', true );