dqiatvbi61502 2016-11-16 20:10 采纳率: 0%
浏览 133

自定义和添加woocommerce模板数据

I am having some trouble customizing the woocommerce templates in my wordpress theme. I would like to add additional data as variables in my templates.

I want to show active orders on the dashboard/my-account page. I want to do this by passing in order data variables to the template to be able to call, like how it is done in the orders.php template.

I know I can override the wc-template-functions.php in my theme and then add the data in the wc_get_templates function for the dashboard or my account. However, I don't want to do this.

What I've tried is creating a hook such as:

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 (also tried in my-account.php)

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

$fr_account_orders comes back null. However if I var_dump the array in the hook function, it comes back with data. Any help is appreciated.

  • 写回答

2条回答 默认 最新

  • doufeixuan8882 2016-11-17 02:44
    关注

    Eaasy there. If you want to return the variable, that's just not the way to do it. You should use the apply_filters like so:

    function wc_fr_add_orders_to_account() {
        /* your function */
    
        return $fr_account_orders;
    }
    add_filter( 'woocommerce_account_dashboard', 'wc_fr_add_orders_to_account' );
    

    and in your template..

    $my_var = apply_filters( 'woocommerce_account_dashboard', $fr_account_orders );
    var_dump( $my_var );
    

    now if you want to send some variables do it like so:

    function wc_fr_add_orders_to_account( $var1, $var2 ) {
        /* your function */
    
        return $fr_account_orders;
    }
    add_filter( 'woocommerce_account_dashboard', 'wc_fr_add_orders_to_account', 10, 3 );
    

    and in your template again..

    $my_var = apply_filters( 'woocommerce_account_dashboard', $fr_account_orders, $var1, $var2 );
    var_dump( $my_var );
    

    read more about apply_filters here https://developer.wordpress.org/reference/functions/apply_filters/ one more thing, try not to change templates, but use add_action on the do_action hooks from template for better compatibility. thanks!

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用