Woocommerce variation product image not showing in order emails - php

I'm using Woocommerce with Dynamic Gallery PRO. The images on the product pages work fine, and I also managed to display a product image in the emails sent to admin and customer. However, it's the main product image that's showing, not the variation image. As I understand the Gallery plugin uses the first image for the checkout page to display.
I already turned '$show_image' to true in email-order-details.php in Woocommerce.
The document of code I should edit (I think) is as following:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
**if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
}**
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
// Variation
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
}
// File URLs
if ( $show_download_links ) {
$order->display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ); ?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php
}
if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
The file of Gallery PRO where it defines images is as following:
class WC_Dynamic_Gallery_Variations{
public static function wc_dgallery_change_variation() {
$product_id = (int) $_REQUEST['product_id'];
$variations = wp_unslash( $_REQUEST['variations'] );
ob_start();
if ( $variations == '' ) {
WC_Gallery_Display_Class::wc_dynamic_gallery_display($product_id);
} else {
WC_Gallery_Display_Class::get_gallery_variations($product_id, $variations);
}
$result = ob_get_clean();
echo json_encode($result);
die();
}
public static function change_image_in_cart_page( $product_image, $values, $cart_item_key ) {
if ( is_array( $values ) && isset( $values['variation_id'] ) && $values['variation_id'] > 0 ) {
$variation_id = $values['variation_id'];
$dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $variation_id );
if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
// Use first image from variation gallery
$img_id = (int) array_shift( $dgallery_ids );
$product_image = wp_get_attachment_image( $img_id, 'shop_thumbnail' );
}
} elseif ( isset( $values['product_id'] ) && $values['product_id'] > 0 ) {
$product_id = $values['product_id'];
// Don't change the image if product has featured image
if ( has_post_thumbnail( $product_id ) ) return $product_image;
$dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $product_id );
if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
// Use first image from variation gallery
$img_id = (int) array_shift( $dgallery_ids );
$product_image = wp_get_attachment_image( $img_id, 'shop_thumbnail' );
}
}
return $product_image;
}
public static function wc_dgallery_variation_save_gallery_ids() {
if ( isset( $_POST['variation_id'] ) && $_POST['variation_id'] > 0 && isset( $_POST['dgallery_ids'] ) ) {
$variation_id = trim( $_POST['variation_id'] );
$dgallery_ids = array_filter( explode( ',', trim( $_POST['dgallery_ids'] ) ) );
update_post_meta( $variation_id, '_product_image_gallery', implode( ',', $dgallery_ids ) );
}
die();
}
public static function add_gallery_variation( $loop, $variation_data, $variation ) {
$global_wc_dgallery_activate = get_option( WOO_DYNAMIC_GALLERY_PREFIX.'activate' );
$actived_d_gallery = get_post_meta( $variation->post_parent, '_actived_d_gallery',true );
if ( $actived_d_gallery == '' && $global_wc_dgallery_activate != 'no' ) {
$actived_d_gallery = 1;
}
$default_show_variation = get_option( WOO_DYNAMIC_GALLERY_PREFIX.'show_variation' );
$show_variation = get_post_meta($variation->post_parent, '_wc_dgallery_show_variation',true);
if ( $show_variation == '' ) {
$show_variation = $default_show_variation;
}
if ( $show_variation == 1 || $show_variation == 'yes' ) {
$show_variation = 1 ;
}
?>
<div class="variations_dgallery_activated_panel_container a3-metabox-panel-wrap a3-dynamic-metabox-panel-wrap" style="<?php if ( 1 != $actived_d_gallery ) { echo 'display: none;'; } ?> padding-left: 0px;">
<div class="a3-metabox-panel a3-metabox-wrapper">
<div id="variations_dgallery_panel" class="a3-metabox-items" style="<?php if ( 1 != $show_variation ) { echo 'display: none;'; } ?>">
<div class="dgallery_images_container dgallery_variation_images_container a3-metabox-options-panel">
<h4 style="margin:0;">
<?php echo __( 'Variation Gallery', 'woo_dgallery' ); ?>
<span class="a3_dg_variation_ajax_loader" style="display: none;"><img class="" src="<?php echo WOO_DYNAMIC_GALLERY_IMAGES_URL; ?>/ajax-loader.gif" /></span>
</h4>
<ul class="dgallery_images">
<?php
$variation_id = $variation->ID;
$dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $variation_id );
if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
foreach ( $dgallery_ids as $img_id ) {
$img_data = wp_get_attachment_image_src( $img_id, 'thumbnail' );
?>
<li class="image" data-attachment_id="<?php echo $img_id ; ?>">
<img class="image_item" src="<?php echo $img_data['0']; ?>" />
<ul class="actions">
<li><?php echo __( 'Delete image', 'woo_dgallery' ); ?></li>
</ul>
</li>
<?php
}
}
?>
</ul>
<input type="hidden" class="dgallery_ids" data-variation-id="<?php echo $variation_id; ?>" value="<?php if ( $dgallery_ids ) echo esc_attr( implode( ',', $dgallery_ids ) ); ?>" />
<p class="add_dgallery_images hide-if-no-js" style="margin: 10px 0px;">
<?php _e( 'Add variation gallery images', 'woo_dgallery' ); ?>
</p>
</div>
</div>
</div>
<?php
// Add an nonce field so we can check for it later.
//wp_nonce_field( 'a3_dynamic_variation_metabox_action', 'a3_dynamic_variation_metabox_nonce_field' );
?>
<div style="clear: both;"></div>
</div>
<?php
} }
Someone familiar with this issue? I'm not really a programmer but I learn a lot along the way.

Try this one near #$show_image, Its working for me.
Option 1:
echo $order->email_order_items_table( true, false, true, true, array(80,48) );
Option 2:
echo $_product->get_image();

Related

Displaying WooCommerce product images in DK PDF Template

I am trying to adapt this code to echoing out a fore each for the image gallary of the products on woocommerce into a pdf. If you could point me into the correct direction that would be great
<?php
/**
* dkpdf-index.php
* This template is used to display the content in the PDF
*
* Do not edit this template directly,
* copy this template and paste in your theme inside a directory named dkpdf
*/
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<?php echo get_bloginfo( 'stylesheet_url' ); ?>" media="all" />
<?php
$wp_head = get_option( 'dkpdf_print_wp_head', '' );
if( $wp_head == 'on' ) {
wp_head();
}
?>
<style type="text/css">
body {
background:#fff;
font-size: 6pt;
}
/* fontawesome compatibility */
.fa {
font-family: fontawesome;
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<?php
// get pdf custom css option
$css = get_option( 'dkpdf_pdf_custom_css', '' );
echo $css;
?>
</style>
</head>
<body>
<?php
global $post;
$pdf = get_query_var( 'pdf' );
$post_type = get_post_type( $pdf );
if( $pdf && $post_type == 'product' ) {
$title = get_the_title();
if( has_post_thumbnail( $post->ID ) ) {
$thumbnail = get_the_post_thumbnail( $post->ID, 'FULL' );
}
$description = get_the_excerpt();
$description2 = get_post($item['product_id'])->post_content;
$product = new WC_Product( $post->ID );
$link = get_the_permalink();
?>
<?php function yikes_custom_product_tabs_shortcode( $args ) {
global $post;
// Define our default values
$defaults = array(
'product_id' => $post->ID,
'tab_number' => 'all',
);
// Let the user-defined values override our defaults
$values = is_array( $args ) ? array_merge( $defaults, $args ) : $defaults;
// Make sure we have a product ID and that the product ID is for a product
if ( empty( $values['product_id'] ) || ! empty( $values['product_id'] ) && get_post_type( $values['product_id'] ) !== 'product' ) {
return;
}
// Fetch our tabs
$tabs = get_post_meta( $values['product_id'], 'yikes_woo_products_tabs', true );
// Get just the specified tab. (minus tab number by one so it starts at 0)
$tabs = absint( $values['tab_number'] ) < 1 ? $tabs : ( isset( $tabs[ absint( $values['tab_number'] ) - 1 ] ) ? array( $tabs[ absint( $values['tab_number'] ) - 1 ] ) : array() );
if ( empty( $tabs ) ) {
return;
}
// Debug statement to show all tab data. Feel free to remove.
// echo '<pre>'; var_dump( $tabs ); echo '</pre>';
$html = '';
// Loop through the tabs and display each one
foreach( $tabs as $tab ) {
$html .= '<tr>
<td style="padding-top:15px; padding-bottom:15px; text-align:center; line-height:2em; background:#f8f8f8;">';
$html .= '<h5>' . $tab['title'] . '</h5>';
$html .= '</td></tr>';
$html .= '<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">';
$html .= '<p style="font-size:10px;">' . $tab['content'] . '</p>';
$html .= '</td></tr>';
}
// Make sure to return your content, do not echo it.
return $html;
}
add_shortcode( 'custom_product_tabs', 'yikes_custom_product_tabs_shortcode' ); ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding-top:20px; padding-bottom:20px;text-align:center; color:#fff;" class="bg-dark">
<h3 style="color:#fff;"><?php echo esc_attr( $title );?></h3>
</td>
</tr>
<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">
<p style="font-size:10px;"><?php echo $description;?></p>
</td>
</tr>
<tr>
<td style="padding-bottom:15px; text-align:center; line-height:2em; ">
<?php if( $thumbnail ) { echo $thumbnail; } ?>
</td>
</tr>
<tr>
<td style="padding-top:15px; padding-bottom:15px; text-align:center; line-height:2em; background:#f8f8f8;">
<h5>DESCRIPTION</p>
</td>
</tr>
<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">
<p style="font-size:10px;"><?php echo $description2;?> <?php echo wpautop( $post->post_content ); ?></p>
</td>
</tr>
<?php echo yikes_custom_product_tabs_shortcode('custom_product_tabs'); ?>
</table>
<?php }
?>
</body>
</html>

Can I sort Order Items by categories in Woocommerce emails

I want to sort items on the Woocommerce notification emails by categories, as by default they are sorted by the order the customer picks them and add them to the cart. Any help in this regard would be appreciated. Thanks
I actually had a solution added to the email-order-items.php - this adds the categories to the admin email and sorts by using the category (I have categories with numbers that relate to warehouse location - so to have all the same locations together really helps the client pick the order.)
the template is the older version but it still works
//**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.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/Emails
* #version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$text_align = is_rtl() ? 'right' : 'left';
global $woocommerce , $wpdb;
$c1 = $wpdb->get_results("select t1.order_item_name,t1.order_item_id,t5.name,t2.meta_value from wp_woocommerce_order_items as t1 LEFT JOIN wp_woocommerce_order_itemmeta as t2 ON t1.order_item_id = t2.order_item_id LEFT JOIN wp_term_relationships as t3 ON t2.meta_value = t3.object_id LEFT JOIN wp_term_taxonomy as t4 ON t3.term_taxonomy_id = t4.term_taxonomy_id LEFT JOIN wp_terms as t5 ON t4.term_id = t5.term_id where t1.order_id = '".$order->id."' and t1.order_item_type='line_item' and t2.meta_key='_product_id' and t4.taxonomy='product_cat' ORDER BY `t5`.`name` ASC");
$array = array();
foreach($c1 as $data)
{
if (!in_array($data->order_item_id , $array)) {
$array[] = $data->order_item_id;
}
}
//foreach ( $items as $item_id => $item ) :
foreach($array as $newdata) :
$item = $order->get_item($newdata);
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
$product = $item->get_product();
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $product->get_image_id() ? current( wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' ) ) : wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Product image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;" /></div>', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false );
// SKU
if ( $show_sku && is_object( $product ) && $product->get_sku() ) {
echo ' (#' . $product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta( $item );
if ( $show_download_links ) {
wc_display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
// get category for admin email only
if( $sent_to_admin ) {
echo get_the_term_list( $product->id, 'product_cat', __( 'Categories', 'woocommerce' ).': ', ', ' );
}
?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ); ?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php
}
if ( $show_purchase_note && is_object( $product ) && ( $purchase_note = $product->get_purchase_note() ) ) : ?>
<tr>
<td colspan="3" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
Go to WooCommerce > Settings in your WordPress admin. On the Products tab, under the Display settings,change the Default Product Sorting.
Change the "Order by" in your sql query.

WordPress: WooCommerce email_order_items_table remove price

I’m new to PHP and also new to WooCommerce.
I want to edit the woo-custom-emails plugin.
As a user I have the possibility to set {email_order_items_table} in my text which outputs the product name, followed by its quantity, followed by the price. I want to remove the price!
Here is what it does in code:
// file: my_plugin/admin/class-wcemails-instance.php
$this->find[] = '{email_order_items_table}';
$this->replace[] = $this->object->email_order_items_table();
here is what the that function does:
// file: woocommerce/includes/abstracts/abstract-wc-order.php
/**
* Output items for display in html emails.
*
* #param array $args Items args.
* #param null $deprecated1 Deprecated arg.
* #param null $deprecated2 Deprecated arg.
* #param null $deprecated3 Deprecated arg.
* #param null $deprecated4 Deprecated arg.
* #param null $deprecated5 Deprecated arg.
* #return string
*/
public function email_order_items_table( $args = array(), $deprecated1 = null, $deprecated2 = null, $deprecated3 = null, $deprecated4 = null, $deprecated5 = null ) {
ob_start();
if ( ! is_null( $deprecated1 ) || ! is_null( $deprecated2 ) || ! is_null( $deprecated3 ) || ! is_null( $deprecated4 ) || ! is_null( $deprecated5 ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' );
}
$defaults = array(
'show_sku' => false,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => false,
'sent_to_admin' => false
);
$args = wp_parse_args( $args, $defaults );
$template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, apply_filters( 'woocommerce_email_order_items_args', array(
'order' => $this,
'items' => $this->get_items(),
'show_download_links' => $this->is_download_permitted() && ! $args['sent_to_admin'],
'show_sku' => $args['show_sku'],
'show_purchase_note' => $this->is_paid() && ! $args['sent_to_admin'],
'show_image' => $args['show_image'],
'image_size' => $args['image_size'],
'plain_text' => $args['plain_text'],
'sent_to_admin' => $args['sent_to_admin']
) ) );
return apply_filters( 'woocommerce_email_order_items_table', ob_get_clean(), $this );
}
and that is the template:
// file: woocommerce/templates/emails/email-order-items.php
<?php
/**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.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/Emails
* #version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
// Variation
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
}
// File URLs
if ( $show_download_links ) {
$order->display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ); ?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php
}
if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
I could replace the template as described in the comments BUT that would overwrite all emails to hide the price and I actually want to keep the price in all other emails that are not send through the plugin. I have no idea on how to make this conditional.
So here is my question: How can I hook that email_order_items_table() function to not output the price?
PS: How can I see what is inside $this->object or how can I output in a human readable form everything inside the $thisobject?
You can use either of this to see the object printed as an array in human readable form.
echo '<pre>';print_r($this->object);exit;
echo '<pre>';print_r($this);exit;
Or you may need to log the object into error log file like below
error_log(print_r($this->object, 1), true);
In woocommerce all templates can be override if you put it on your theme,
so:
woocommerce/templates/emails/email-order-items.php
can be override if you put it on:
wp-content/themes/{your theme}/woocomerce/{the template}
in this case
content/themes/{your theme}/woocommerce/emails/email-order-items.php
Then you can change the template, so it dont draw the price.
take out this line on the file that is on your themes directory. Be carefull some time you change somthing on the plugin directory and you will lose it on the next update.
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
br

WooCommerce E-mail Order - Products Alignment

I want to edit the email template for the WooCommerce Order, so that instead of the usual product per line table, I get 3 products per line (screenshot).
I don't know how to set a limit of 3 products per line though.
Only first 3 products show correctly.
Here is my snippet.
<?php
/**
* Email Order Items
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<td class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>" style="text-align:center; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
echo nl2br ("\n");
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
echo nl2br ("\n");
//Product quantity
echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item );
echo nl2br (" piece(s)\n");
echo apply_filters( 'woocommerce_cart_item_weight', $_product->get_weight());
echo nl2br (" gr\n");
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo nl2br ("\n");
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
?></td>
<?php
}
if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
I found a solution to it in case anyone is interested in aligning his products like this.
I inserted a counter $x to count the products in the end of the foreach loop and once it reaches the 3rd product, I have an if statement to change the table row and restart the counter.
Here is how the email looks like now: http://imgur.com/6q14Pes
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
$x = 0;
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
$x += 1;
if( $x > 3 ){ ?>
<tr></tr>
<?php
$x = 0;
}
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
?>
<td class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>" style="text-align:center; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
echo nl2br ("\n");
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
echo nl2br ("\n");
//Product quantity
echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item );
echo nl2br (" piece(s)\n");
//Product weight
echo apply_filters( 'woocommerce_cart_item_weight', $_product->get_weight());
echo nl2br (" gr\n");
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo nl2br ("\n");
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
?></td><?php
}
if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>

Wordpress & Woocommerce add excerpt in order email

I have a little problem with Wordpress and Woocommerce I hope someone can help me.
When a person purchases receive an email with your data such as title, quantity, etc. ... I would also add the excerpt of the products near the quatity. The classic <? Php the_excerpt ();?> Does not work, does anyone know how to do?
The file is "email-order-items" in woocommerce\templates\emails
Here is the code:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'orderby' => 'desc' );
$loop = new WP_Query( $args ); ?>
<?php
/**
* Email Order Items
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.0.3
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
foreach ($items as $item) :
// Get/prep product data
$_product = $order->get_product_from_item( $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$image = ($show_image) ? '<img src="'. current(wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail')) .'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />' : '';
?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php
// Show title/image etc
echo apply_filters( 'woocommerce_order_product_image', $image, $_product, $show_image);
// Product name
echo apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );
// SKU
echo ($show_sku && $_product->get_sku()) ? ' - cod. articolo (#' . $_product->get_sku() . ')' : '';
// File URLs
if ( $show_download_links && $_product->exists() && $_product->is_downloadable() ) {
$download_file_urls = $order->get_downloadable_file_urls( $item['product_id'], $item['variation_id'], $item );
$i = 0;
foreach ( $download_file_urls as $file_url => $download_file_url ) {
echo '<br/><small>';
if ( count( $download_file_urls ) > 1 ) {
echo sprintf( __('Download %d:', 'woocommerce' ), $i + 1 );
} elseif ( $i == 0 )
echo __( 'Download:', 'woocommerce' );
echo ' ' . basename( $file_url ) . '</small>';
$i++;
}
}
// Variation
echo ($item_meta->meta) ? '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>' : '';
?>
</td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?> x <?php the_excerpt(); ?> </td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php if ($show_purchase_note && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo apply_filters('the_content', $purchase_note); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
thank you very much
To get excerpt in order email try this code.
<?php
/**
* Email Order Items
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
/* To get product excerpt */
$product_data = get_post( $item['product_id'] );
$excerpt = strip_tags($product_data->post_excerpt);
echo $excerpt;
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . __( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" />', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo ' (#' . $_product->get_sku() . ')';
}
// File URLs
if ( $show_download_links && is_object( $_product ) && $_product->exists() && $_product->is_downloadable() ) {
$download_files = $order->get_item_downloads( $item );
$i = 0;
foreach ( $download_files as $download_id => $file ) {
$i++;
if ( count( $download_files ) > 1 ) {
$prefix = sprintf( __( 'Download %d', 'woocommerce' ), $i );
} elseif ( $i == 1 ) {
$prefix = __( 'Download', 'woocommerce' );
}
echo '<br/><small>' . $prefix . ': ' . esc_html( $file['name'] ) . '</small>';
}
}
// Variation
if ( $item_meta->meta ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>';
}
?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php if ( $show_purchase_note && is_object( $_product ) && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo wpautop( do_shortcode( $purchase_note ) ); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
I've used the following code to get the excerpt from Woocommerce order posts (delivery notes).
$post_id = $post_id_of_the_product;
$page_data = get_page( $post_id );
$excerpt = strip_tags($page_data->post_excerpt);

Categories