【问题标题】:Wordpress date_query behaving strangelyWordpress date_query 行为异常
【发布时间】:2018-06-26 08:29:37
【问题描述】:

这里有点菜鸟。 我正在尝试编写一些代码来确定当前用户是否拥有: 超过 30 天的帐户和 0 个 woocommerce 订单。 我有这个顶部部分在过去 90 天内获取属于用户的所有订单。

但是,即使我的测试用户有 1 个已完成的订单,它也会返回 0 个订单。 我有一种预感,可能是这一行: '之前' => 日期('Y-m-d', strtotime('now')) 所以我删除了它,然后它返回了“19 个订单”,这简直是不可能的。 print_r 部分似乎只显示了 1 个订单的乱码元数据,所以它从哪里得到 19,我只是无法理解!

对于这个问题的任何帮助将不胜感激!所有的 echos 和 print_r 都用于调试目的。

function woo_reg_matured( $days_old = 30 )
{
    $cu = wp_get_current_user();
    return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( '-%d days', $days_old ) ) ) ? TRUE : FALSE;    
}
function woo_inactive_shortcode( $atts = array(), $content = '' ) {
    if ( is_user_logged_in() ):
        $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' ),
'date_query' => array(
    'after' => date('Y-m-d', strtotime('-90 days'))
) ) );
    $total = 0;
        print_r($customer_orders);
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
        }
        echo 'Orders: ' . $total . '<br>';
        if ($total <= 5) {
                if( woo_reg_matured(  $days_old = 30 ) ) {
            echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
            return;         
            }
        echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
        return;
        }
        echo 'You are active with over 30 days age and ' . $total . ' orders';
        return;
    endif;
}
add_shortcode( 'woo_inactive',   'woo_inactive_shortcode' );

print_r 显示:

数组 ( [0] => WP_Post 对象 ( [ID] => 75675 [post_author] => 1 [post_date] => 2018-01-17 12:16:28 [post_date_gmt] => 2018-01-17 12:16:28 [post_content] => [post_title] => 订购 – 2018 年 1 月 17 日下午 12:16 [post_excerpt] => [post_status] => wc-completed [comment_status] => 打开 [ping_status] =>已关闭 [post_password] => order_5a5f3e9c442d6 [post_name] => order-jan-17-2018-1216-pm [to_ping] => [pinged] => [post_modified] => 2018-01-17 12:17:24 [post_modified_gmt ] => 2018-01-17 12:17:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.sheersense.com/?post_type=shop_order&p=75675 [menu_order] => 0 [post_type] => shop_order [post_mime_type] => [ comment_count] => 3 [过滤器] => raw ) )

然而$total 显示:Orders: 19

19 是从哪里来的!!!!哈哈

【问题讨论】:

  • 在将$customer_orders 的输出传递给foreach() 之前,您是否已将其转储?
  • 我错误地认为这不会有问题。它只返回“数组()”。我删除了 date_query 的“之前”部分,但它确实显示了我的 1 个订单中的大量元数据。

标签: php wordpress woocommerce orders


【解决方案1】:

像这样试试。

    if ( is_user_logged_in() ):
        $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' ),
        'date_query' => 
        array('after' =>
                array(
                  'year'  => 2017,
                  'month' => 03,
                  'day'   => 12,
                ),
            'before' =>
                array(
                  'year'  => 2018,
                  'month' => 01,
                  'day'   => 17,
                ),  
        ) ) );
    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        print_r($customer_order);
        //$total++;
          $total += $order->get_total();
        }
        echo 'Orders: ' . $total . '<br>';
        if ($total <= 5) {
              /*  if( woo_reg_matured(  $days_old = 30 ) ) {
            echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
            return;         
            }*/
        echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
        return;
        }
        echo 'You are active with over 30 days age and ' . $total . ' orders';
        return;
    endif;

【讨论】:

  • 谢谢!但它没有用。首先,我尝试再次替换 date_query 部分 - “Array ()”结果,然后我用你的替换了整个函数,但它失败了(使网站崩溃)。我刚刚意识到我还没有把整个功能,也许这就是原因。更新置顶帖!
  • 在 wp-config 文件中启用error report 并检查错误
  • 错误太多了,我都不知道该找什么了!上帝这很难考虑它是什么!!!搜索“woo_inactive”或“inactive”一无所获。
  • 你能分享我的截图吗?
  • 什么时候调用这个脚本?我的意思是你用的是哪个钩子?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2012-12-05
  • 2017-01-22
相关资源
最近更新 更多