Customize and add woocommerce template data

I am having problems setting up woocommerce templates in my Wordpress theme. I would like to add additional data as variables to my templates.

I want to show active orders on the panel / my account page. I want to do this by passing data variables to the template for invocation, for example, how it is done in the template orders.php.

I know that I can redefine wc-template-functions.phpin my subject, and then add data to the function wc_get_templatesfor the dashboard or my account. However, I do not want to do this.

What I tried creates a hook like:

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 null. , var_dump hook, . .

+4
2

, . .

$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;
+1

. , . apply_filters :

function wc_fr_add_orders_to_account() {
    /* your function */

    return $fr_account_orders;
}
add_filter( 'woocommerce_account_dashboard', 'wc_fr_add_orders_to_account' );

.

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

, , :

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 );

.

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

apply_filters https://developer.wordpress.org/reference/functions/apply_filters/ , , add_action do_action . !

0

Source: https://habr.com/ru/post/1660989/


All Articles