Echo woocommerce product thumbnail? - php

Im trying to echo WooCommerce thumbail of producs onto an orderform.
Not sure how though though. This is what I've got so far:
<?php echo woocommerce_get_product_thumbnail();?>
This give me the WooCommerce thumbnail placeholder. How do I pull the right one for each item in my orderform? The orderform has fields for order id, creationdate, status and price, so it does pull the proper id´s somewhere.
This is examples of the meta keys and other fields that pull info for each order on the same form, if any of this makes any sense.
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
$customer_orders = get_posts(
apply_filters( 'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types('view-orders'),
'post_status' => array_keys(wc_get_order_statuses())
)
)
);
This is how the php is set up. I added the first table class for the immage.
<tr>
<th class="order-number"><span class="nobr"><?php _e( 'Image', 'woocommerce' ); ?></span></th>
<th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th>
<th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th>
</tr>
Then Im trying to add the image.
<td class="order-image”>
<?php echo woocommerce_get_product_thumbnail(); ?>
</td>
<td class="order-number">
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
</a>
</td>
<td class="order-date”>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
</td>

Keep in mind that WooCommerce already has all the items/costs/thumbnails for previous orders in the My Accounts section and customers can "order again" directly from their My Account area.
But, assuming you have the order ID you can get to the product thumbnails with the following. This will output a small list of items with their thumbnails and product name:
$order = wc_get_order( $order_id );
$items = $order->get_items();
if( $items ) {
echo '<ul class="ordered-items">';
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
echo '<li>'. $product->get_image() . $product->get_title() . '</li>';
}
echo '</ul>';
}
Edit: To integrate into your code I would probably do the following
function so_28179558_get_order_thumbnail( $order ){
if( is_numeric( $order ) ){
$order = wc_get_order( $order_id );
}
if( is_wp_error( $order ) ){
return;
}
$order_thumb = '';
$items = $order->get_items();
if( $items ) {
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
$order_thumb = $product->get_image();
continue;
}
}
return $order_thumb;
}
Then you can use it in your template like so:
<td class="order-image”>
<?php echo so_28179558_get_order_thumbnail( $order ); ?>
</td>

Related

pagination does not work in orders WooCommerce

i changed the pagination of WooCommerce orders as follows :
before : example.com/my-account/orders/2
after : example.com/my-account/orders/page/2
i added pagination to WooCommerce orders
path : /plugins/woocommerce/templates/myaccount/orders.php
<?php
/**
* Orders
*
* Shows orders on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_account_orders', $has_orders );
?>
<?php if ( $has_orders ) : ?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo $order->get_formatted_order_total(); ?>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
<nav class="woocommerce-pagination">
<?php
$args = array(
'total' => $customer_orders->max_num_pages
);
echo paginate_links( $args );
?>
</nav>
<?php endif; ?>
<?php else : ?>
<p class="woocommerce_message"><?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
i also added the following code to functions :
function woocommerce_my_account_my_orders_query1() {
$current_page = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
$customer_orders = array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
);
return $customer_orders;
} add_filter( 'woocommerce_my_account_my_orders_query', 'woocommerce_my_account_my_orders_query1', 10, 1 );
problem : after clicking on any of the page numbers, it will go to the correct address, that is:
example.com/my-account/orders/page/2
example.com/my-account/orders/page/3
example.com/my-account/orders/page/4
but in all pages it brings the same results as the first page
i did a lot of searching on the internet and i tried everything, but it didn't work, for example :
https://wordpress.stackexchange.com/questions/185600/pagination-not-working
if you would like to answer my question, thanks
To solve your issue, make use of WP_Query as it seems that you are using a custom page template here.
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
should be
'paged' => ( get_query_var('page') ? get_query_var('page') : 1),
as static front pages use page and not paged
Update 2 with solution, tested and working
I write the result directly, so if you don't like it, don't waste time implementing the code. Not quite exactly what you were looking for, but the most I managed to do is this: example.com/my-account/orders/page/?n=2
I looked at the topic all day, looked for similar questions and studied the reference https://developer.wordpress.org/reference/functions/paginate_links/, but couldn't get it to work as described in your question. It is also in my interest to achieve a similar result, I want to understand how it works. In any case, we are waiting for someone who knows the subject well, maybe he will be able to give a better answer.
As for the previous update, I will write you what I did step by step
1. Go to /themes/theme-child/woocommerce/myaccount
2. If you have the order.php file open it, otherwise create it. Then you will have /themes/theme-child/woocommerce/myaccount/orders.php
3. Inside the orders.php file paste the following template, this time I added custom queries at the beginning and changed the final part slightly.
<?php
/**
* Orders
*
* Shows orders on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_account_orders', $has_orders );
?>
<?php if ( $has_orders ) : ?>
<?php
$paged = max( 1, (int) filter_input( INPUT_GET, 'n' ) );
$customer_orders = wc_get_orders( apply_filters('woocommerce_my_account_my_orders_query',array(
'customer_id' => get_current_user_id(),
'posts_per_page' => 3, // post per page
'paged' => $paged,
'paginate' => true,
)));
?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo $order->get_formatted_order_total(); ?>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links(array(
'base' => esc_url( wc_get_endpoint_url( 'orders') ) . 'page/%_%', // Base of the paginated url.
'format' => '?n=%#%', // Format for the pagination structure.
'total' => $customer_orders->max_num_pages, // The total amount of pages. Default is the value WP_Query's max_num_pages or 1.
'current' => $paged, // The current page number. Default is 'paged' query var or 1.
'show_all' => false, // Whether to show all pages. Default false.
'end_size' => 3, // How many numbers on either the start and the end list edges. Default 1.
'mid_size' => 3, // How many numbers to either side of the current pages. Default 2.
'prev_next' => true, // Whether to include the previous and next links in the list. Default true.
'prev_text' => __('<span>« Prev</span>'), // The previous page text. Default '« Previous'.
'next_text' => __('<span>Next »</span>'), // The next page text. Default 'Next »'.
));
?>
</nav>
<?php else : ?>
<p class="woocommerce_message"><?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
4. In the functions.php file remove the following function, I had it added to you in the previous update, but since you now have a custom query you don't need the code below.
// Query Post - How many orders do you want to display per page in the orders.php template ?
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
function custom_my_account_orders( $args ) {
// Set the post per page
$args['limit'] = 6;
return $args;
}
5. Still in the functions.php file, delete what you did previously, you don't need it.
function woocommerce_my_account_my_orders_query1() {
$current_page = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
$customer_orders = array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
);
return $customer_orders;
} add_filter( 'woocommerce_my_account_my_orders_query', 'woocommerce_my_account_my_orders_query1', 10, 1 );
Try and let us know if it works, hope this helps.

Display latest orders on WooCommerce "My account" Dashboard

I'm trying to show customer last 3 orders on WooCommerce dashboard. But get_template() does not work at all and shows nothing.
I also tried to put orders.php form table in to the dashboard.php template file but It displays no orders here, while already I have some active orders. I'm so confused and have no idea about this problem.
This is my dashboard code:
<?php get_header();?>
<?php
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 4.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
$allowed_html = array(
'a' => array(
'href' => array(),
),
);
?>
<p>
<!-- <?php
printf(
/* translators: 1: user display name 2: logout url */
wp_kses( __( 'Hello %1$s (not %1$s? Log out)', 'woocommerce' ), $allowed_html ),
'<strong>' . esc_html( $current_user->display_name ) . '</strong>',
esc_url( wc_logout_url() )
);
?> -->
</p>
<p>
<?php
/* translators: 1: Orders URL 2: Address URL 3: Account URL. */
// $dashboard_desc = __( 'From your account dashboard you can view your recent orders, manage your billing address, and edit your password and account details.', 'woocommerce' );
// if ( wc_shipping_enabled() ) {
// /* translators: 1: Orders URL 2: Addresses URL 3: Account URL. */
// $dashboard_desc = __( 'From your account dashboard you can view your recent orders, manage your shipping and billing addresses, and edit your password and account details.', 'woocommerce' );
// }
// printf(
// wp_kses( $dashboard_desc, $allowed_html ),
// esc_url( wc_get_endpoint_url( 'orders' ) ),
// esc_url( wc_get_endpoint_url( 'edit-address' ) ),
// esc_url( wc_get_endpoint_url( 'edit-account' ) )
// );
?>
</p>
<?php
/**
* My Account dashboard.
*
* #since 2.6.0
*/?>
<?php do_action( 'woocommerce_account_dashboard' );?>
<div class="row">
<div class="col-10 d-flex dashboard-custom">
<h4>Addresses</h4>
<div class="questionMark">
?
</div>
</div>
<div class="col-2 text-center pt-5 mt-2">
<a href="#" class="viwAdress">
See
</a>
</div>
</div>
<div class="row pt-5">
<div class="adress-dashboard">
<div class="col-12 pt-4 d-flex">
<?php
$fname = get_user_meta( $current_user->ID, 'first_name', true );
$lname = get_user_meta( $current_user->ID, 'last_name', true );
$address_1 = get_user_meta( $current_user->ID, 'billing_address_1', true );
$address_2 = get_user_meta( $current_user->ID, 'billing_address_2', true );
$phonenumber = get_user_meta( $current_user->ID, 'billing_mobile_phone', true );
$city = get_user_meta( $current_user->ID, 'billing_city', true );
$postcode = get_user_meta( $current_user->ID, 'billing_postcode', true );
// echo $fname . "<BR>";
// echo $lname . "<BR>";
// echo $address_1 . "<BR>";
// echo $address_2 . "<BR>";
// echo $city . "<BR>";
// echo $postcode . "<BR>";
?>
<svg id="a47b7a8a-e7bb-4135-b65b-2487268cdcff" width="14" height="17" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 17"><defs><style>.b21eb9b6-977e-44fb-a0e8-fad7fb47a827{fill:#909298;fill-rule:evenodd;}</style></defs><path class="b21eb9b6-977e-44fb-a0e8-fad7fb47a827" d="M10.32,7.26A3.11,3.11,0,1,1,7.21,4.13,3.12,3.12,0,0,1,10.32,7.26Zm-1.55,0A1.56,1.56,0,1,1,7.21,5.7,1.56,1.56,0,0,1,8.77,7.26Z" transform="translate(0 -0.01)"/><path class="b21eb9b6-977e-44fb-a0e8-fad7fb47a827" d="M2.17,12.15a7.07,7.07,0,0,1-.24-10A7,7,0,0,1,11.83,2a7.07,7.07,0,0,1,.24,10L7.24,17Zm8.77-1.32-3.75,4L3.25,11a5.5,5.5,0,0,1-.19-7.75,5.42,5.42,0,0,1,7.69-.19A5.5,5.5,0,0,1,10.94,10.83Z" transform="translate(0 -0.01)"/>
</svg>
<h5><?php echo $address_1; ?></h5>
</div>
<div class="col-12 d-flex pt-4">
<svg id="bd6f5e62-b10a-4e71-b77c-d5f9368c28e6" width="14" height="17" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 18"><defs><style>.a30aed3e-ff50-49ef-a803-e1cfe573925c,.fa1d0d58-d038-41f3-bd11-17bb673b76c2{fill:#909298;}.a30aed3e-ff50-49ef-a803-e1cfe573925c{fill-rule:evenodd;}</style></defs><path class="a30aed3e-ff50-49ef-a803-e1cfe573925c" d="M9.17,4A3.84,3.84,0,0,1,5.5,8,3.84,3.84,0,0,1,1.83,4,3.84,3.84,0,0,1,5.5,0,3.84,3.84,0,0,1,9.17,4ZM7.33,4A1.92,1.92,0,0,1,5.5,6,1.92,1.92,0,0,1,3.67,4,1.92,1.92,0,0,1,5.5,2,1.92,1.92,0,0,1,7.33,4Z"/><path class="fa1d0d58-d038-41f3-bd11-17bb673b76c2" d="M9.17,12a1,1,0,0,0-.92-1H2.75a1,1,0,0,0-.92,1v6H0V12A2.88,2.88,0,0,1,2.75,9h5.5A2.88,2.88,0,0,1,11,12v6H9.17Z"/>
</svg>
<h5>Customer :<?php echo $fname ; ?> </h5>
</div>
<div class="col-12 d-flex pt-4">
<h5>Phone :<?php echo $phonenumber; ?> </h5>
</div>
<div class="col-12 d-flex pt-4">
<h5>ZipCode:<?php echo $postcode ; ?> </h5>
</div>
</div>
<!-- adres box -->
</div>
<?php
/**
* Deprecated woocommerce_before_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_before_my_account' );
/**
* Deprecated woocommerce_after_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_after_my_account' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */?>
</section>
And this is my Order template code that work on Orders Tab but don't work on dashboard Tab:
<?php
/**
* My Orders - Deprecated
*
* #deprecated 2.6.0 this template file is no longer used. My Account shortcode uses orders.php.
* #package WooCommerce\Templates
*/
defined('ABSPATH') || exit;
$my_orders_columns = apply_filters(
'woocommerce_my_account_my_orders_columns',
array(
'order-number' => esc_html__('Order', 'woocommerce'),
'order-date' => esc_html__('Date', 'woocommerce'),
'order-status' => esc_html__('Status', 'woocommerce'),
'order-total' => esc_html__('Total', 'woocommerce'),
'order-actions' => ' ',
)
);
$customer_orders = get_posts(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types('view-orders'),
'post_status' => array_keys(wc_get_order_statuses()),
)
)
); ?>
<?php if ($customer_orders) : ?>
<h2><?php echo apply_filters('woocommerce_my_account_my_orders_title', esc_html__('Recent orders', 'woocommerce')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h2>
<table class="shop_table shop_table_responsive my_account_orders">
<thead>
<tr>
<?php foreach ($my_orders_columns as $column_id => $column_name) : ?>
<th class="<?php echo esc_attr($column_id); ?>"><span
class="nobr"><?php echo esc_html($column_name); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ($customer_orders as $customer_order) :
$order = wc_get_order($customer_order); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count();
?>
<tr class="order">
<?php foreach ($my_orders_columns as $column_id => $column_name) : ?>
<td class="<?php echo esc_attr($column_id); ?>" data-title="<?php echo esc_attr($column_name); ?>">
<?php if (has_action('woocommerce_my_account_my_orders_column_' . $column_id)) : ?>
<?php do_action('woocommerce_my_account_my_orders_column_' . $column_id, $order); ?>
<?php elseif ('order-number' === $column_id) : ?>
<a href="<?php echo esc_url($order->get_view_order_url()); ?>">
<?php echo _x('#', 'hash before order number', 'woocommerce') . $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</a>
<?php elseif ('order-date' === $column_id) : ?>
<time datetime="<?php echo esc_attr($order->get_date_created()->date('c')); ?>"><?php echo esc_html(wc_format_datetime($order->get_date_created())); ?></time>
<?php elseif ('order-status' === $column_id) : ?>
<?php echo esc_html(wc_get_order_status_name($order->get_status())); ?>
<?php elseif ('order-total' === $column_id) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
printf(_n('%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce'), $order->get_formatted_order_total(), $item_count); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
<?php elseif ('order-actions' === $column_id) : ?>
<?php
$actions = wc_get_account_orders_actions($order);
if (!empty($actions)) {
foreach ($actions as $key => $action) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html($action['name']) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php
wc_get_template( 'myaccount/orders.php' );
?>
No need to overwrite template files, you can use the woocommerce_account_dashboard hook
Most of the code was copied from myaccount/orders.php template file,
here and there, however, there is an adjustment so that directly copying the template file is not a (complete) solution.
So you get:
function action_woocommerce_account_dashboard() {
// Set limit
$limit = 3;
// Get customer $limit last orders
$customer_orders = wc_get_orders( array(
'customer' => get_current_user_id(),
'limit' => $limit
) );
// Count customers orders
$count = count( $customer_orders );
// Greater than or equal to
if ( $count >= 1 ) {
// Message
echo '<p>' . sprintf( _n( 'Your last order', 'Your last %s orders', $count, 'woocommerce' ), $count ) . '</p>';
?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
?>
<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_account_dashboard', 'action_woocommerce_account_dashboard' );
Code goes in functions.php file of the active child theme (or active theme).

How to get and display BACS account details in Woocommerce

I have a very simple idea, but I do not know how to do it in WooCommerce.
In my store I have enabled a few payment options, also paying via bank transfer. But when client chose bank transfer he sees data needed to make a transfer. But after that, there is no option to display that data on thank you page, where everybody looking for that.
There is an easy way to show that data again?
The Bacs account details are stored in wp_options table as most of all Wordpress and Woocommerce settings.
They can be accessed using (which gives a multi-dimensional array of different bank accounts, as you can set many):
$bacs_accounts_info = get_option( 'woocommerce_bacs_accounts');
Normally this details are displayed by default on woocommerce thankyou page and in some customer email notifications…
To display the formatted bank accounts information, I have built this custom function:
// Utility function, to display BACS accounts details
function get_bacs_account_details_html( $echo = true, $type = 'list' ) {
ob_start();
$gateway = new WC_Gateway_BACS();
$country = WC()->countries->get_base_country();
$locale = $gateway->get_country_locale();
$bacs_info = get_option( 'woocommerce_bacs_accounts');
// Get sortcode label in the $locale array and use appropriate one
$sort_code_label = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' );
if( $type == 'list' ) :
?>
<div class="woocommerce-bacs-bank-details">
<h2 class="wc-bacs-bank-details-heading"><?php _e('Our bank details'); ?></h2>
<?php
$i = -1;
if ( $bacs_info ) : foreach ( $bacs_info as $account ) :
$i++;
$account_name = esc_attr( wp_unslash( $account['account_name'] ) );
$bank_name = esc_attr( wp_unslash( $account['bank_name'] ) );
$account_number = esc_attr( $account['account_number'] );
$sort_code = esc_attr( $account['sort_code'] );
$iban_code = esc_attr( $account['iban'] );
$bic_code = esc_attr( $account['bic'] );
?>
<h3 class="wc-bacs-bank-details-account-name"><?php echo $account_name; ?>:</h3>
<ul class="wc-bacs-bank-details order_details bacs_details">
<li class="bank_name"><?php _e('Bank'); ?>: <strong><?php echo $bank_name; ?></strong></li>
<li class="account_number"><?php _e('Account number'); ?>: <strong><?php echo $account_number; ?></strong></li>
<li class="sort_code"><?php echo $sort_code_label; ?>: <strong><?php echo $sort_code; ?></strong></li>
<li class="iban"><?php _e('IBAN'); ?>: <strong><?php echo $iban_code; ?></strong></li>
<li class="bic"><?php _e('BIC'); ?>: <strong><?php echo $bic_code; ?></strong></li>
</ul>
<?php endforeach; endif; ?>
</div>
<?php
else :
?>
<h2><?php _e( 'Account details', 'woocommerce' ); ?>:</h2>
<table class="widefat wc_input_table" cellspacing="0">
<thead>
<tr>
<th><?php _e( 'Account name', 'woocommerce' ); ?></th>
<th><?php _e( 'Account number', 'woocommerce' ); ?></th>
<th><?php _e( 'Bank name', 'woocommerce' ); ?></th>
<th><?php echo $sort_code_label; ?></th>
<th><?php _e( 'IBAN', 'woocommerce' ); ?></th>
<th><?php _e( 'BIC / Swift', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody class="accounts">
<?php
$i = -1;
if ( $bacs_info ) {
foreach ( $bacs_info as $account ) {
$i++;
echo '<tr class="account">
<td>' . esc_attr( wp_unslash( $account['account_name'] ) ) . '</td>
<td>' . esc_attr( $account['account_number'] ) . '</td>
<td>' . esc_attr( wp_unslash( $account['bank_name'] ) ) . '</td>
<td>' . esc_attr( $account['sort_code'] ) . '</td>
<td>' . esc_attr( $account['iban'] ) . '</td>
<td>' . esc_attr( $account['bic'] ) . '</td>
</tr>';
}
}
?>
</tbody>
</table>
<?php
endif;
$output = ob_get_clean();
if ( $echo )
echo $output;
else
return $output;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
POSSIBLE USAGES:
1) In any template or php code you will just use to display this account details:
get_bacs_account_details_html();
2) As a hooked function (where you will set your desired action hook).
Here is an example usage that will display this bank account details in My account order view, for orders that have BACS as payment gateway and an "on hold" status:
add_action( 'woocommerce_view_order', 'display_bacs_account_details_on_view_order', 5, 1 );
function display_bacs_account_details_on_view_order( $order_id ){
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
if( $order->get_payment_method() === 'bacs' && $order->get_status() === 'on-hold' ){
get_bacs_account_details_html();
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
As a shortcode [bacs_account_details]:
add_shortcode( 'bacs_account_details', 'shortcode_bacs_account_details' );
function shortcode_bacs_account_details() {
get_bacs_account_details_html( false );
}
Code goes in function.php file of your active child theme (or active theme). Tested and wok.
Then you can use it in any Wordpress editor of a page, a post or a custom post: [bacs_account_details]
Or in the PHP code: echo do_shortcode('[bacs_account_details]');
Use action on thank you page, In your child functions.php or using code snippets plugin
add_action('woocommerce_thankyou', 'customThankYouFunction');
and in your function write your logic
function customThankYouFunction ($order_id) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
$order_currency = $order_data['currency'];
$order_version = $order_data['version'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method_title = $order_data['payment_method_title'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method = $order_data['payment_method'];
}
You can deal with order now
Reference How to get WooCommerce order details

WooCommerce - Adding product category to order-details table

Here's what I'm attempting to do and where.
In the check out process for an order, in the plug-in WooCommerce; you are taken to a page when the check out is process is complete. It displays an overview of the order details. The template file used to output this page is order-details.php.
Here's what I'd like to add
I would like to display the product's product category like so:
This is area of code I'm attempting to add to, the first section of the order details table.
<tbody>
<?php
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_item_name', sprintf( '%s', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong>', $item );
This is my understanding so far;
That a custom query is being applied.
That for each product listed details are being echoed.
That a WooCommerce Class is being used to get specific product meta.
My understandings are observations at my current knowledge level.
What I was hoping was that I could use something like
echo apply_filters( 'woocommerce_order_item_name', ' <strong class="product-category">' . e_( $item['product_cat'] ) . '</strong>', $item );
I can't find a more relevant filter and I'm really not sure that ['product_cat'] is even relevant here. This is an example of the way I'm attempting to solve my requirement.
As you can tell, I don't work in PHP full time. I'm learning as much as I can when I can. Advise would be greatly appreciated
This is how you might add the product categories to the Product title. I don't particularly like how it is outputting for me, but then I am testing it on a variable subscription, so that is amending its own data. This would go in your theme's functions.php
function kia_woocommerce_order_item_name( $name, $item ){
$product_id = $item['product_id'];
$tax = 'product_cat';
$terms = wp_get_post_terms( $product_id, $tax, array( 'fields' => 'names' ) );
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
$name .= '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
}
return $name;
}
add_filter( 'woocommerce_order_item_name', 'kia_woocommerce_order_item_name', 10, 2 );
Otherwise you could copy the order-details.php template into your own theme (so yourtheme/woocommerce/order/order-details.php and add the code directly. This would give you more control over where you want the category to appear since there aren't a lot of hooks available in this section, but put you at risk if WooCommerce ever modifies this code. I have worked on a few sites that broke because the the theme was using outdated template.
Any way, an example would be:
<?php
/**
* Order details
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
$order = new WC_Order( $order_id );
?>
<h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2>
<table class="shop_table order_details">
<thead>
<tr>
<th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) :
?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php
endforeach;
?>
</tfoot>
<tbody>
<?php
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_item_name', sprintf( '%s', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong>', $item );
$item_meta->display();
// insert product category here
$tax = 'product_cat';
$terms = wp_get_post_terms( $_product->id, $tax, array( 'fields' => 'names' ) );
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
echo '<strong>' . $taxonomy->label . ': </strong>' . implode( ', ', $terms );
}
// end edit
if ( $_product && $_product->exists() && $_product->is_downloadable() && $order->is_download_permitted() ) {
$download_files = $order->get_item_downloads( $item );
$i = 0;
$links = array();
foreach ( $download_files as $download_id => $file ) {
$i++;
$links[] = '<small>' . sprintf( __( 'Download file%s', 'woocommerce' ), ( count( $download_files ) > 1 ? ' ' . $i . ': ' : ': ' ) ) . esc_html( $file['name'] ) . '</small>';
}
echo '<br/>' . implode( '<br/>', $links );
}
?>
</td>
<td class="product-total">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
</tr>
<?php
if ( $order->has_status( array( 'completed', 'processing' ) ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) {
?>
<tr class="product-purchase-note">
<td colspan="3"><?php echo wpautop( do_shortcode( $purchase_note ) ); ?></td>
</tr>
<?php
}
}
}
do_action( 'woocommerce_order_items_table', $order );
?>
</tbody>
</table>
<?php do_action( 'woocommerce_order_details_after_order_table', $order ); ?>
<header>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
</header>
<dl class="customer_details">
<?php
if ( $order->billing_email ) echo '<dt>' . __( 'Email:', 'woocommerce' ) . '</dt><dd>' . $order->billing_email . '</dd>';
if ( $order->billing_phone ) echo '<dt>' . __( 'Telephone:', 'woocommerce' ) . '</dt><dd>' . $order->billing_phone . '</dd>';
// Additional customer details hook
do_action( 'woocommerce_order_details_after_customer_details', $order );
?>
</dl>
<?php if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option( 'woocommerce_calc_shipping' ) !== 'no' ) : ?>
<div class="col2-set addresses">
<div class="col-1">
<?php endif; ?>
<header class="title">
<h3><?php _e( 'Billing Address', 'woocommerce' ); ?></h3>
</header>
<address><p>
<?php
if ( ! $order->get_formatted_billing_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address();
?>
</p></address>
<?php if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option( 'woocommerce_calc_shipping' ) !== 'no' ) : ?>
</div><!-- /.col-1 -->
<div class="col-2">
<header class="title">
<h3><?php _e( 'Shipping Address', 'woocommerce' ); ?></h3>
</header>
<address><p>
<?php
if ( ! $order->get_formatted_shipping_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_shipping_address();
?>
</p></address>
</div><!-- /.col-2 -->
</div><!-- /.col2-set -->
<?php endif; ?>
<div class="clear"></div>

get custom field value from taxonomy in wordpress

i make a custom field in categories, it save and update data successfully, but i want to show the value of custom field data in archive page, i search a lot about this but in vain
Please help me
here is my code
Create custom field:
add_action ( 'category_add_form_fields', 'extra_field');
add_action ( 'category_edit_form_fields', 'extra_field');
function extra_field($term) { //check for existing featured ID
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id");
?>
<table width="100%" border="0" cellspacing="3" cellpadding="0" style="margin-bottom:20px;">
<tr>
<td><strong>Image</strong></td>
</tr>
<tr>
<td><input type="text" size="40" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>" /></td>
</tr>
<tr>
<td><p>A quick brown fox jumps over the lazy dog.</p></td>
</tr>
</table>
<?php
}
Save / Update data:
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = $_POST['term_meta'];
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_category', 'save_taxonomy_custom_meta' );
add_action( 'create_category', 'save_taxonomy_custom_meta' );
One more thing, can i make an extra field in db in wp_terms, because it save in wp_options
Use this
first of all you get category id on taxonomy page.I suppose one category assign to each post.
$t_id = $term_id;
Then get value using this
get_option( "taxonomy_".$t_id );
I thanked to #yatendra to help me i got an idea from his answer so its work
here is answer
$queried_object = get_queried_object();
$t_id = $queried_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo "<img src=".$term_meta['custom_term_meta']." />";
I want to customize my category section so have use below code and it is working perfectly without any issue..
In
function.php
add_action ( 'edit_category_form_fields', function( $tag ){
$cat_title = get_term_meta( $tag->term_id, '_pagetitle', true );
?>
<tr class='form-field'>
<th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
<td>
<input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
<p class='description'><?php _e('Title for the Category '); ?></p>
</td>
</tr> <?php
});
add_action ( 'edited_category', function() {
if ( isset( $_POST['cat_title'] ) )
update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});
and call in index.php page for fronted
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $categories as $category ) {
if($category->name !="Uncategorized")
{
$cat_title = get_term_meta( $category->term_id, '_pagetitle', true );
echo '
<div class="col-md-4">' . $category->name . '</div>
<div class="col-md-4">' . $category->description . '</div>
';
}
}
?>

Categories