php – 自定义和添加woocommerce模板数据

我在wordpress主题中定制woocommerce模板时遇到了一些麻烦.我想在模板中添加其他数据作为变量.

我想在信息中心/我的帐户页面上显示有效订单.我想通过将数据变量传递给模板以便能够调用来实现这一点,就像在orders.php模板中完成它一样.

我知道我可以覆盖主题中的wc-template-functions.php,然后在仪表板或我的帐户的wc_get_templates函数中添加数据.但是,我不想这样做.

我试过的是创建一个钩子,如:

的functions.php

function wc_fr_add_orders_to_account( $fr_account_orders, $current_page ) {
  global $fr_account_orders;
  $current_page = empty( $current_page ) ? 1 : absint( $current_page );

  $customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', 
    array( 
      'customer' => get_current_user_id(), 
      'page' => $current_page, 
      'paginate' => true,
      'status' => array( 'wc-pending' )
      ) ) );

  $fr_account_orders = array(
    'current_page' => absint( $current_page ),
    'customer_orders' => $customer_orders,
    'has_orders' => 0 < $customer_orders->total
  );

  return $fr_account_orders;
}
add_action( 'woocommerce_account_content', 'wc_fr_add_orders_to_account' );

/theme-directory/woocommerce/templates/myaccount/dashboard.php(也在my-account.php中尝试过)

do_action( 'woocommerce_account_dashboard', $fr_account_orders);
var_dump($fr_account_orders);

$fr_account_orders返回null.但是,如果我在钩子函数中var_dump数组,它会返回数据.任何帮助表示赞赏.

最佳答案 我尝试了几种方法,无法弄清楚如何保持分页正确.这种方式列出了我的仪​​表板上的所有订单.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 
    'posts_per_page' => 3, 
    'paged' => $paged,
    'meta_key'    => '_customer_user',
  'meta_value'  => get_current_user_id(),
  'post_type'   => wc_get_order_types(),
  'post_status' => array ('wc-pending'),
);

$customer_waiting_orders = new WP_Query( $args );

if ( $customer_available_orders->have_posts() ) :

  while ( $customer_available_orders->have_posts() ) : $customer_available_orders->the_post();

    //code here    
    wp_reset_postdata();

  endwhile;

endif;
点赞