get second to last comment php woo commerce followup email - php

I found this handy code to add order notes to the completed email. I really just want to add the last two notes if possible. Can someone tell me what I would need to tweak?
<?php
add_action( 'woocommerce_email_order_meta', 'woo_add_order_notes_to_email' );
function woo_add_order_notes_to_email() {
global $woocommerce, $post;
$args = array(
'post_id' => $post->ID,
'approve' => 'approve',
'type' => 'order_note'
);
$notes = get_comments( $args );
echo '<h2>' . __( 'Order Notes', 'woocommerce' ) . '</h2>';
echo '<ul class="order_notes">';
if ( $notes ) {
foreach( $notes as $note ) {
$note_classes = get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? array( 'customer-note', 'note' ) : array( 'note' );
?>
<li rel="<?php echo absint( $note->comment_ID ) ; ?>" class="<?php echo implode( ' ', $note_classes ); ?>">
<div class="note_content">
(<?php printf( __( 'added %s ago', 'woocommerce' ), human_time_diff( strtotime( $note->comment_date_gmt ), current_time( 'timestamp', 1 ) ) ); ?>) <?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
</div>
</li>
<?php
}
} else {
echo '<li>' . __( 'There are no notes for this order yet.', 'woocommerce' ) . '</li>';
}
echo '</ul>';
}
?>

You can try use array_slice
After the if and before the foreach
$notes = array_slice($notes, 0, 2);

Related

Get image from post in wordpress

I need to configure this script so that it fetches the first image that is in the article and shows it in a widjet.
At the moment the widjet only shows featured images, I would like it to show external images that were in the post.
For example, the first image in the article would be shown.
Code
<?php
/**
* Featured Posts widget
*/
class colormag_featured_posts_widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_featured_posts widget_featured_meta',
'description' => __( 'Display latest posts or posts of specific category.', 'colormag' ),
'customize_selective_refresh' => true,
);
$control_ops = array( 'width' => 200, 'height' => 250 );
parent::__construct( false, $name = __( 'TG: Featured Posts (Style 1)', 'colormag' ), $widget_ops );
}
function form( $instance ) {
$tg_defaults['title'] = '';
$tg_defaults['text'] = '';
$tg_defaults['number'] = 4;
$tg_defaults['type'] = 'latest';
$tg_defaults['category'] = '';
$instance = wp_parse_args( ( array ) $instance, $tg_defaults );
$title = esc_attr( $instance['title'] );
$text = esc_textarea( $instance['text'] );
$number = $instance['number'];
$type = $instance['type'];
$category = $instance['category'];
?>
<p><?php _e( 'Layout will be as below:', 'colormag' ) ?></p>
<div style="text-align: center;"><img src="<?php echo get_template_directory_uri() . '/img/style-1.jpg' ?>">
</div>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php _e( 'Description', 'colormag' ); ?>
<textarea class="widefat" rows="5" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to display:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="radio" <?php checked( $type, 'latest' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="latest" /><?php _e( 'Show latest Posts', 'colormag' ); ?>
<br />
<input type="radio" <?php checked( $type, 'category' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="category" /><?php _e( 'Show posts from a category', 'colormag' ); ?>
<br /></p>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>
:</label>
<?php wp_dropdown_categories( array(
'show_option_none' => ' ',
'name' => $this->get_field_name( 'category' ),
'selected' => $category,
) ); ?>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
}
$instance['number'] = absint( $new_instance['number'] );
$instance['type'] = $new_instance['type'];
$instance['category'] = $new_instance['category'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
extract( $instance );
global $post;
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$text = isset( $instance['text'] ) ? $instance['text'] : '';
$number = empty( $instance['number'] ) ? 4 : $instance['number'];
$type = isset( $instance['type'] ) ? $instance['type'] : 'latest';
$category = isset( $instance['category'] ) ? $instance['category'] : '';
$post_status = 'publish';
if ( get_option( 'fresh_site' ) == 1 ) {
$post_status = array( 'auto-draft', 'publish' );
}
$args = array(
'posts_per_page' => $number,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => $post_status,
);
// Display from category chosen.
if ( $type == 'category' ) {
$args['category__in'] = $category;
}
$get_featured_posts = new WP_Query( $args );
echo $before_widget;
?>
<?php
if ( $type != 'latest' ) {
$border_color = 'style="border-bottom-color:' . colormag_category_color( $category ) . ';"';
$title_color = 'style="background-color:' . colormag_category_color( $category ) . ';"';
} else {
$border_color = '';
$title_color = '';
}
if ( ! empty( $title ) ) {
echo '<h3 class="widget-title" ' . $border_color . '><span ' . $title_color . '>' . esc_html( $title ) . '</span></h3>';
}
if ( ! empty( $text ) ) {
?> <p> <?php echo esc_textarea( $text ); ?> </p> <?php } ?>
<?php
$i = 1;
while ( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
?>
<?php if ( $i == 1 ) {
$featured = 'colormag-featured-post-medium';
} else {
$featured = 'colormag-featured-post-small';
} ?>
<?php if ( $i == 1 ) {
echo '<div class="first-post">';
} elseif ( $i == 2 ) {
echo '<div class="following-post">';
} ?>
<div class="single-article clearfix">
<?php
if ( has_post_thumbnail() ) {
$image = '';
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$image_alt_text = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
$title_attribute = get_the_title( $post->ID );
if ( empty( $image_alt_text ) ) {
$image_alt_text = $title_attribute;
}
$image .= '<figure>';
$image .= '<a href="' . get_permalink() . '" title="' . the_title( '', '', false ) . '">';
$image .= get_the_post_thumbnail( $post->ID, $featured, array(
'title' => esc_attr( $title_attribute ),
'alt' => esc_attr( $image_alt_text ),
) ) . '</a>';
$image .= '</figure>';
echo $image;
}
?>
<div class="article-content">
<?php colormag_colored_category(); ?>
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<div class="below-entry-meta">
<?php
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on"><i class="fa fa-calendar-o"></i> %3$s</span>', 'colormag' ), esc_url( get_permalink() ), esc_attr( get_the_time() ), $time_string
);
?>
<span class="byline"><span class="author vcard"><i class="fa fa-user"></i><a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo get_the_author(); ?>"><?php echo esc_html( get_the_author() ); ?></a></span></span>
<?php if ( ! post_password_required() && comments_open() ) { ?>
<span class="comments"><i class="fa fa-comment"></i><?php comments_popup_link( '0', '1', '%' ); ?></span>
<?php } ?>
</div>
<?php if ( $i == 1 ) { ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php } ?>
</div>
</div>
<?php if ( $i == 1 ) {
echo '</div>';
} ?>
<?php
$i ++;
endwhile;
if ( $i > 2 ) {
echo '</div>';
}
// Reset Post Data
wp_reset_query();
?>
<!-- </div> -->
<?php
echo $after_widget;
}
}
This is the complete code of the widget
Try this code
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>

Display Woocommerce Sorting Dropdown as a List

I'm trying to replace the woocommerce sorting dropdown with a list and I have this so far which works ok.
The problem is the options are not converted to clickable working links. Any ideas how I can do it ?
<div class="wrapper-dropdown">
<span>I'm kinda the label!</span>
<ul class="dropdown">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( get_option( 'woocommerce_enable_review_rating' ) == 'no' )
unset( $catalog_orderby['rating'] );
foreach ( $catalog_orderby as $id => $name )
echo '<li>' . esc_attr( $name ) . '</li>';
?>
</ul>
Thanks.
your li tag in the foreach loop should be something like this:
echo '<li><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '?orderby=' . $id . '" >' . esc_attr( $name ) . '</a></li>';
woocommerce_get_page_id() is depracted since WC 3.0
Use this code instead:
<ul class="woocommerce-ordering">
<?php
$term = get_queried_object();
$term_url = get_term_link($term->term_id);
?>
<?php foreach ($catalog_orderby_options as $id => $name) : ?>
<?php echo '<li>' . esc_attr( $name ) . '</li>'; ?>
<?php endforeach; ?>

Getting rid of order status within the WooCommerce My Account - Orders Page

I'd like to hide the Status columns within My Accounts - Orders (where it shows processing, refunded, etc...).
Does anyone know how I can do that?
Here is a screenshot showing the account status column:
Thanks.
Here is the code that's currently in my file (my-orders.php)
<?php
/**
* My Orders
*
* Shows recent orders on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-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 http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$my_orders_columns = apply_filters( 'woocommerce_my_account_my_orders_columns', array(
'order-number' => __( 'Order', 'woocommerce' ),
'order-date' => __( 'Date', 'woocommerce' ),
'order-status' => __( 'Status', 'woocommerce' ),
'order-total' => __( '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() )
) ) );
/**
* This is the entire "Recent Orders" section below my subscriptions. It has been commented out
*
if ( $customer_orders ) : ?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_orders_title', __( 'Recent Orders', 'woocommerce' ) ); ?></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 );
$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(); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<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>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
)
);
if ( ! $order->needs_payment() ) {
unset( $actions['pay'] );
}
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
unset( $actions['cancel'] );
}
if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
**/
You need to override myaccount/orders.php WooCommerce via your active theme.
For that, if not done, you will need to copy from woocommerce plugin folder, a subfolder named templates to your active child theme (or theme) and to rename it woocommerce (see this related docs).
After that you find inside that new woocommerce folder in myaccount subfolder a template named orders.php.
Open/edit orders.php template and replace the code by this:
<?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/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
<?php if ( $has_orders ) : ?>
<table class="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 ) :
if( $column_name != 'Status' ){ // added this line
?>
<th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php
} // and this too
endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $customer_orders->orders as $customer_order ) :
$order = wc_get_order( $customer_order );
$item_count = $order->get_item_count();
?>
<tr class="order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) :
if( $column_name != 'Status' ){ // added this line
?>
<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(); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<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>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
)
);
if ( ! $order->needs_payment() ) {
unset( $actions['pay'] );
}
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
unset( $actions['cancel'] );
}
if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php
} // added this too
endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
<div class="woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php _e( 'Previous', 'woocommerce' ); ?></a>
<?php endif; ?>
<?php if ( $current_page !== intval( $customer_orders->max_num_pages ) ) : ?>
<a class="woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page + 1 ) ); ?>"><?php _e( 'Next', 'woocommerce' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<div class="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 _e( 'Go Shop', 'woocommerce' ) ?>
</a>
<?php _e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
Save, you are done. This code is tested and working.
Update: for prior versions of woocommerce 2.5.x, the template file is named my-orders.php and you have just to comment one line in template code:
// 'order-status' => __( 'Status', 'woocommerce' ),
Here is an extract of this template with the commented line in the code:
<?php
/**
* My Orders
*
* Shows recent orders on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-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 http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$my_orders_columns = apply_filters( 'woocommerce_my_account_my_orders_columns', array(
'order-number' => __( 'Order', 'woocommerce' ),
'order-date' => __( 'Date', 'woocommerce' ),
// 'order-status' => __( 'Status', 'woocommerce' ),
'order-total' => __( 'Total', 'woocommerce' ),
'order-actions' => ' ',
) );
Reference: Template Structure + Overriding Templates via a Theme
The easiest approach would probably be just to hide that column via CSS -- If that column has a unique ID or the TD's have a class you could target with display:none, that'd probably take care of what you'd need.
Alternatively, you could probably also hide it by hooking into the woocommerce_my_account_my_orders_columns filter.
Something like this (untested)
add_filter('woocommerce_my_account_my_orders_columns', 'my_custom_function_name', 10);
function my_custom_function_name($order){
unset($order['order-status']);
return $order;
}
I'm sure that exact code won't work, but I'll bet it gets you pretty close.

WooCommerce customer order detail in BS Modal

In a WordPress website running WooCommerce, the user can login in his (default) personal area and display information like:
Orders history
Download
Addresses
Edit info
Logout
In the orders tab, a table is presented by default, showing a list of all orders, with a View button which redirects to the full detail page of that order.
What I'm trying to do is showing that table view in a modal window.
I don't have any problem in showing the modal with the target url loaded in it.
The real problem is that the targeted url is that of the full page which is showing like in an <iframe>, and is not what I want.
I think there is some shortcode allowing to load just that table, or maybe some woocommerce function like load_order_content_by_id($id)?
Can anybody point me in the right direction?
Thanks
===SOLVED===
Thanks to Raunak Gupta for pointing me to the right function.
I override the orders.php template, added Modal window html and edited $actions:
'view' => array(
'url' => 'javascript:;',
'data' => [
'order-number' => $order->get_order_number()
],
'name' => __( 'View', 'woocommerce' )
),
and on same file:
foreach ( $actions as $key => $action ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '"';
if(isset($action['data']) && is_array($action['data'])){
foreach($action['data'] AS $data_attr=>$data_value){
echo 'data-' . sanitize_html_class($data_attr) .'="' .esc_html($data_value) . '" ';
}
}
echo '>' . esc_html( $action['name'] ) . '</a>';
}
A little JS
$('.woocommerce-MyAccount-orders .button.view').on('click', function(e){
e.preventDefault();
var data = {};
data.action = 'modal_order';
data.order_number = $(this).data('order-number');
$.get( ajax_script.ajax_url, data, function(response) {
$('#modalOrderDetail').modal('show').find('.modal-body').html(response);
});
});
and hooked into wordpress by function.php
function modal_order() {
if(is_user_logged_in()) {
$order_number = $_GET['order_number'];
woocommerce_order_details_table($order_number);
}
}
add_action('wp_ajax_modal_order', 'modal_order');
add_action('wp_ajax_nopriv_modal_order', 'modal_order');
Here is the complete code to display curent customer orders in a modal window. Its based on a classic query to get current user orders and on the template my-account/orders.php (lightly customized)…
<?php
if(is_user_logged_in()):
// The query
$args = array(
// WC orders post type
'post_type' => 'shop_order',
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
// get orders statuses
'post_status' => array_keys(wc_get_order_statuses()),
);
// Get all customer orders
$customer_orders = get_posts( $args );
$count_ord = 0;
if (!empty($customer_orders))
foreach ( $customer_orders as $custo_order )
$count_ord++;
if ( $count_ord > 0 )
$has_orders = true;
else
$has_orders = false;
// the template my-account/orders.php ?>
<?php do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
<?php if ( $has_orders ) : ?>
<table class="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="<?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 );
$item_count = $order->get_item_count();
?>
<tr class="order">
<?php foreach ( wc_get_account_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(); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<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>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
)
);
if ( ! $order->needs_payment() ) {
unset( $actions['pay'] );
}
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
unset( $actions['cancel'] );
}
if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
<div class="woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php _e( 'Previous', 'woocommerce' ); ?></a>
<?php endif; ?>
<?php if ( $current_page !== intval( $customer_orders->max_num_pages ) ) : ?>
<a class="woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page + 1 ) ); ?>"><?php _e( 'Next', 'woocommerce' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<div class="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 _e( 'Go Shop', 'woocommerce' ) ?>
</a>
<?php endif; ?>
<?php _e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
<?php endif; ?>
You will have to add existing css rules to your modal window and/or customized that css rules.
This code is tested and fully functional.
woocommerce_order_details_table( $order_id )
This WooCommerce function returns the full order details in HTML form by $order_id
There isn't a single function that I'm aware of that can get all of the order details that you need in one go however you can call WC_Order class to get what you need. Making some calls methods similar to these would get you the info you need. You will likely need to make calls to more than just the get_items() method depending on the exact info you need. Generally they return objects similar in structure to post objects.
$order = new WC_Order($post->ID);
$_order = $order->get_items();
Look here under the 'inherited methods' section to find the methods you might need to call to get all the info you need. https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

Original template code suddenly doesn't work

I was translating the titles and sentences right on the editor. I don't know what went wrong, but when I hit upload I keep getting this:
Parse error: syntax error, unexpected 'Comentários' (T_STRING) in /home2/tkleinow/public_html/wp-content/themes/fashionistas/inc/template-tags.php on line 94
Then I tried to copy the original code for that section (template tags) on the zip files, but I keep getting the same error.
I pasted it on Excel to find what the line was, and this is wat I got:
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
My website was doing just fine, I don't know how I screwed it like that.
This is the whole thing:
<?php
/**
* Custom template tags for this theme.
*
* Eventually, some of the functionality here could be replaced by core features
*
* #package aThemes
*/
if ( ! function_exists( 'athemes_content_nav' ) ) :
/**
* Display navigation to next/previous pages when applicable
*/
function athemes_content_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
?>
<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'athemes' ); ?></h1>
<?php if ( is_single() ) : // navigation links for single posts ?>
<?php previous_post_link( '<div class="nav-previous"><span>Artigo Anterior</span>%link</div>', '<span class="meta-nav">' . _x( '←', 'Link do Post Anterior', 'athemes' ) . '</span> %title' ); ?>
<?php next_post_link( '<div class="nav-next"><span>Próximo Artigo</span>%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Link para o próximo artigo', 'athemes' ) . '</span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Artigos Anteriores', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Artigos Recentes <span class="meta-nav">→</span>', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
<?php
}
endif; // athemes_content_nav
if ( ! function_exists( 'athemes_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*/
function athemes_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
<div class="comment-body">
<?php _e( 'Pingback:', 'athemes' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'athemes' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<?php else : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="clearfix comment-meta">
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<div class="clearfix comment-author vcard">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<time datetime="<?php comment_time( 'c' ); ?>">
<?php printf( _x( '%1$s', '1: date, 2: time', 'athemes' ), get_comment_date(), get_comment_time() ); ?>
</time>
</a>
</div><!-- .comment-metadata -->
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Seu comentário será aprovado assim que passar pela moderação.', 'athemes' ); ?></p>
<?php endif; ?>
</footer><!-- .comment-meta -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->
</article><!-- .comment-body -->
<?php
endif;
}
endif; // ends check for athemes_comment()
if ( ! function_exists( 'athemes_the_attached_image' ) ) :
/**
* Prints the attached image with a link to the next attached image.
*/
function athemes_the_attached_image() {
$post = get_post();
$attachment_size = apply_filters( 'athemes_attachment_size', array( 1200, 1200 ) );
$next_attachment_url = wp_get_attachment_url();
/**
* Grab the IDs of all the image attachments in a gallery so we can get the
* URL of the next adjacent image in a gallery, or the first image (if
* we're looking at the last image in a gallery), or, in a gallery of one,
* just the link to that image file.
*/
$attachment_ids = get_posts( array(
'post_parent' => $post->post_parent,
'fields' => 'ids',
'numberposts' => -1,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
) );
// If there is more than 1 attachment in a gallery...
if ( count( $attachment_ids ) > 1 ) {
foreach ( $attachment_ids as $attachment_id ) {
if ( $attachment_id == $post->ID ) {
$next_id = current( $attachment_ids );
break;
}
}
// get the URL of the next image attachment...
if ( $next_id )
$next_attachment_url = get_attachment_link( $next_id );
// or get the URL of the first image attachment.
else
$next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
}
printf( '%3$s',
esc_url( $next_attachment_url ),
the_title_attribute( array( 'echo' => false ) ),
wp_get_attachment_image( $post->ID, $attachment_size )
);
}
endif;
if ( ! function_exists( 'athemes_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function athemes_posted_on() {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
//if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
//$time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on">%1$s</span>', 'athemes' ),
sprintf( '%3$s',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
}
endif;
/**
* Returns true if a blog has more than 1 category
*/
function athemes_categorized_blog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
// This blog has more than 1 category so athemes_categorized_blog should return true
return true;
} else {
// This blog has only 1 category so athemes_categorized_blog should return false
return false;
}
}
/**
* Flush out the transients used in athemes_categorized_blog
*/
function athemes_category_transient_flusher() {
// Like, beat it. Dig?
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'athemes_category_transient_flusher' );
add_action( 'save_post', 'athemes_category_transient_flusher' );

Categories