Woocommerce customize email checkout - php

This is my custom e-mail template which gets sent when a order is completed.
<?php
/**
* Customer completed order email
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
<?php do_action( 'woocommerce_email_header', $email_heading ); ?>
<p><?php printf( __( "Hi there. Your recent order on %s has been completed. Your order details are shown below for your reference:", 'woocommerce' ), get_option( 'blogname' ) ); ?></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( true, false, true ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
</table>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
<?php if ($order->billing_email) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>
<?php wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
I want to insert PHP code there which connects to a database, does a select query which needs the article ID and how many of the article ID´s are purchased and then sends a new email with this data.
I tried it multiple times but at the end the mail always stopped working due to some PHP error which I couldn't address without debugging opportunity.
As an example I tried:
<?php
$db = mysqli_connect("sqwffwq", "qwfqwf", "qwfqwf", "qwfwqf");
// here I have no clue how to receive the article ID´s and there amounts...
// I wasn't able to find the woocommerce function
$result = mysqli_query('SELECT * FROM etc. etc. ');
mail(results) //Should be no problem after I got the information
?>

run this and see whats going on with each result. What you've failed to mention above is where you are adding this code, are you sure you have $order available?
global $wpdb;
$rere2 = substr( $order->get_order_number(), 4);
echo 'rere2='.$rere2.'<br>';
$myrows = $wpdb->get_results( 'SELECT * FROM wp_woocommerce_order_items WHERE order_id = "'.$rere2.'"' );
var_dump($myrows);
foreach ( $myrows as $myrow) {
//mail("etix#foxfiredev.net",$rere2,$myrow->order_item_id);
$x=wp_mail( "etix#foxfiredev.net", $rere2 ,$myrow->order_item_id);
echo $x.'<br>';
$wpdb->update( 'wp_woocommerce_order_items',
array('id'=>$variable),
array('order_item_id'=>$myrow->order_item_id),
); // change table name, column names and values to suit..
}
exit;

Related

Display product thumbnail in new column in WooCommerce quick order preview window

How can I display the product thumbnail in a new column with a hyperlink to open the thumbnail in the new tab whenever I click on it?
Column order:
"Image", "Product", "Quantity"
Based on Display product thumbnail in existing product column in WooCommerce quick order preview window answer code, this is my code attempt:
function filter_woocommerce_admin_order_preview_line_item_columns( $columns, $order ) {
$new_product = $columns['image'];
return array_merge( array( 'new_product' => $new_product ), $columns );
}
add_filter( 'woocommerce_admin_order_preview_line_item_columns', 'filter_woocommerce_admin_order_preview_line_item_columns', 10, 2 );
function filter_woocommerce_admin_order_preview_line_item_column_new_product( $html, $item, $item_id, $order ) {
$hidden_order_itemmeta = apply_filters(
'woocommerce_hidden_order_itemmeta',
array(
'_image',
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'method_id',
'cost',
'_reduced_stock',
'_restock_refunded_items',
)
);
$product_object = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null;
$thumbnail = $product_object->get_image( array( 60, 60 ) );
// Add thumbnail
if ( $product_object->get_image_id() > 0 ) {
$html .= '<div class="item-thumbnail">' . $thumbnail . '</div>';
'image_size' => array( 50, 50 ),
}
$html .= wp_kses_post( $item->get_name() );
if ( $product_object ) {
$html .= '<div class="wc-order-item-sku">' . esc_html( $product_object->get_sku() ) . '</div>';
}
$meta_data = $item->get_formatted_meta_data( '' );
if ( $meta_data ) {
<div style="margin-bottom: 40px;">
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
<thead>
<tr>
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'image.', 'woocommerce' ); ?></th>
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product quantity', 'woocommerce' ); ?></th>
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
);
?>
</tbody>
return $html;
}
add_filter( 'woocommerce_admin_order_preview_line_item_column_new_product', 'filter_woocommerce_admin_order_preview_line_item_column_new_product', 10, 4 );
But nothing changed in the quick order preview window, can anyone point me in the right direction?
Via woocommerce_admin_order_preview_line_item_columns filter hook, we have the option to add a new column, for order preview window.
The woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), ' filter hook, will become woocommerce_admin_order_preview_line_item_column_image
So to display the product thumbnail in new column, in WooCommerce quick order preview window you get:
// Add new column
function filter_woocommerce_admin_order_preview_line_item_columns( $columns, $order ) {
// Add a new column
$new_column['image'] = __( 'Image', 'woocommerce' );
// Return new column as first
return $new_column + $columns;
}
add_filter( 'woocommerce_admin_order_preview_line_item_columns', 'filter_woocommerce_admin_order_preview_line_item_columns', 10, 2 );
function filter_woocommerce_admin_order_preview_line_item_column_image( $html, $item, $item_id, $order ) {
// Get product object
$product_object = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null;
// Add thumbnail
if ( $product_object->get_image_id() > 0 ) {
// Get image ID
$image_id = $product_object->get_image_id();
// Get thumbnail
$thumbnail = $product_object->get_image( array( 120, 120 ) );
$html .= '<div class="item-thumbnail">' . $thumbnail . '</div>';
}
return $html;
}
add_filter( 'woocommerce_admin_order_preview_line_item_column_image', 'filter_woocommerce_admin_order_preview_line_item_column_image', 10, 4 );
// CSS style
function add_order_notes_column_style() {
$css = '.wc-order-preview .wc-order-preview-table td, .wc-order-preview .wc-order-preview-table th { text-align: left; }';
wp_add_inline_style( 'woocommerce_admin_styles', $css );
}
add_action( 'admin_print_styles', 'add_order_notes_column_style' );

How to remove payment method from WooCommerce order confirmation email?

We've removed the payment information on our customer emails but can't remove the title. How do we do it?We're using Woocommerce email templates for order confirmation emails.
We've searched in and tried to change
email-order-details.php
email-order-items.php
customer-on-hold-order. php (our customer use on-hold-order as default template for confirmation emails to customers)
We tried this one in
email-order-details, it did not work at all.
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
if ( $total['label'] != 'Payment Method:' ){
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
}
?>
</tfoot>
To remove payment method from WooCommerce email notifications using hooks:
add_filter( 'woocommerce_get_order_item_totals', 'remove_paymeny_method_row_from_emails', 10, 3 );
function remove_paymeny_method_row_from_emails( $total_rows, $order, $tax_display ){
// On Email notifications only
if ( ! is_wc_endpoint_url() ) {
unset($total_rows['payment_method']);
}
return $total_rows;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Or overriding email-order-details.php template file:
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $key => $total ) {
$i++;
if ( $key !== 'payment_method' ){
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
}
?>
</tfoot>
It should also works.

Customize WooCommerce attribute table to two columns

I'm looking for some help getting customize the attribute table on WooCommerce.
<table>
<?php foreach (array_chunk($product_attributes, 2) as $product_attribute_key => $product_attribute) :{ ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<?php foreach ($product_attribute as $value) :{ ?>
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $value['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $value['value'] ); ?></td>
<?php } endforeach; ?>
</tr>
<?php } endforeach; ?>
</table>
I tried modify the above snippet in order to break line between Attribute Name and Attribute Value to display the attributes like it is in the below picture.
attributes
Is there any easy hook to get this work?
Thanks in Advance.
Create child-theme in case of theme update to not lose your settings.
Create in child-theme folders /woocommerce/single-product/ and copy from woocommerce/templates/single-product the file product-attributes.php.
Change:
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
?>
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
to
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
$i = 0;
?>
<div class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<div class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<div class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></div>
<div class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></div>
</div>
<?php
if ($i % 2 != 0){
echo '<div class="clear"></div>';
}
$i++;
endforeach; ?>
</div>
add CSS
/*2 Columnt style on product attributes*/
.woocommerce-product-attributes.shop_attributes .woocommerce-product-attributes-item {
float: left;
width: 50%;
margin-bottom: 8px;
padding-right:10px;
}

How to change WooCommerce Shipping field in new order emails?

I am trying to change the Shipping field order in WooCommerce new order emails,
However, modifying the Shipping field in email-addresses.php will break the checkout page. How can I modify this?
This is how I am modifying:
0.Source code:
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
$shipping = $order->get_formatted_shipping_address();
?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
<tr>
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
<address class="address">
<?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
<?php if ( $order->get_billing_phone() ) : ?>
<br/><?php echo esc_html( $order->get_billing_phone() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_email() ) : ?>
<br/><?php echo esc_html( $order->get_billing_email() ); ?>
<?php endif; ?>
</address>
</td>
<?php if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && $shipping ) : ?>
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
<address class="address"><?php echo wp_kses_post( $shipping ); ?></address>
</td>
<?php endif; ?>
</tr>
</table>
1.Successfully modified the Billing field:
<address class="address">
<?php //echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
Name: <?php echo $order->billing_first_name; ?><?php echo $order->billing_last_name; ?><br>
Address:<?php echo $order->billing_country; ?><?php echo $order->billing_postcode; ?><?php echo $order->billing_city; ?><?php echo $order->billing_state; ?><?php echo $order->billing_address_1; ?>
<?php if ( $order->get_billing_phone() ) : ?>
<br/>Phone:<?php echo esc_html( $order->get_billing_phone() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_email() ) : ?>
<br/>Email:<?php echo esc_html( $order->get_billing_email() ); ?>
<?php endif; ?>
</address>
</td>
2.This is I want to change shipping field, but not work:
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
<address class="address">
<?php //echo wp_kses_post( $shipping ); ?>
Shipping Name:<?php echo esc_html($order->get_shipping_first_name() ); ?><?php echo esc_html($order->get_shipping_last_name() ); ?><br>
Shipping Address:<?php echo $order->esc_html(get_shipping_country() ); ?><?php echo esc_html($order->get_shipping_postcode() ); ?><?php echo esc_html($order->get_shipping_city() ); ?><?php echo esc_html($order->get_shipping_state() ); ?><?php echo esc_html($order->get_shipping_address_1() );?>
<?php if ( $order->get_shipping_phone() ) : ?>
<br/>Shipping Phone:<?php echo esc_html( $order->get_shipping_phone() ); ?>
<?php endif; ?>
<?php if ( $order->get_shipping_email() ) : ?>
<br/>Shipping Email:<?php echo esc_html( $order->get_shipping_email() ); ?>
<?php endif; ?>
</address>
Any help Where can i get and change it?
Thanks
Hey there your code is perfect except the following lines
1> if ( $order->get_shipping_phone() )
2> if ( $order->get_shipping_email() )
in above lines get_shipping_phone and get_shipping_email are the methods which are not available in the woocommerce itself so you can use get_billing_email and get_billing_phone instead of them if you want.

Grouped product parent vs Grouped product child in woocommerce

I am using PDF packing plugin (WooCommerce PDF Invoices) to create packing slips after order is placed. And Product Bundles plugin for bundled products.
In those PDFs I want to differentiate between the container of a bundled product and its child.
Currently I am using this from Product Bundles:
if(wc_pb_is_bundle_container_cart_item($item) )
it checks if an item is container of a bundled product then returns true. I need a similar function which would return true if the item is child or is inside a bundle.
here is the code of packing slip pdf body:
<?php
/**
* PDF packing slip template body.
*
* This template can be overridden by copying it to youruploadsfolder/woocommerce-pdf-invoices/templates/packing-slip/simple/yourtemplatename/body.php.
*
* HOWEVER, on occasion WooCommerce PDF Invoices 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.
*
* #author Bas Elbers
* #package WooCommerce_PDF_Invoices/Templates
* #version 0.0.1
*/
$templater = WPI()->templater();
$order = $templater->order;
$formatted_shipping_address = $order->get_formatted_shipping_address();
$formatted_billing_address = $order->get_formatted_billing_address();
$line_items = $order->get_items( 'line_item' );
$color = $templater->get_option( 'bewpi_color_theme' );
?>
<table>
<tr class="title">
<td colspan="3">
<h2><?php _e( 'Packing Slip', 'woocommerce-pdf-invoices' ); ?></h2>
</td>
</tr>
<tr class="information">
<td width="50%">
<?php echo nl2br( $templater->get_option( 'bewpi_company_address' ) ); ?>
</td>
<td>
<?php
if ( $templater->get_option( 'bewpi_show_ship_to' ) && ! empty( $formatted_shipping_address ) && $formatted_shipping_address !== $formatted_billing_address && ! $templater->has_only_virtual_products( $line_items ) ) {
printf( '<strong>%s</strong><br />', __( 'Ship to:', 'woocommerce-pdf-invoices' ) );
echo $formatted_shipping_address;
}
?>
</td>
<td>
<?php echo $formatted_billing_address; ?>
</td>
</tr>
</table>
<table>
<thead>
<tr class="heading" bgcolor="<?php echo $color; ?>;">
<th>
<?php _e( 'Qty', 'woocommerce-pdf-invoices' ); ?>
</th>
<th>
<?php _e( 'Product', 'woocommerce-pdf-invoices' ); ?>
</th>
<th>
<?php _e( 'SKU', 'woocommerce-pdf-invoices' ); ?>
</th>
</tr>
</thead>
<tbody>
<?php
//$parentItem = 0;
foreach ( $line_items as $item_id => $item ) {
$product = BEWPI_WC_Order_Compatibility::get_product( $order, $item );
if(wc_pb_is_bundle_container_cart_item($item) ){
?>
<tr class="item">
<td width="10%">
<?php echo $item['qty']; ?>
<?php // print_r($item); die(); ?>
</td>
<td width="65%">
<?php
echo $item['name'];
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
$templater->wc_display_item_meta( $item, true );
$templater->wc_display_item_downloads( $item, true );
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
?>
</td>
<td width="25%">
<?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
</td>
</tr>
<?php } else { ?>
<tr class="item">
<td width="10%" style="float:right;">
<?php echo $item['qty']; ?>
</td>
<td width="5%" style="float:right;">
<?php
echo '----'.$item['name'];
//echo 'i m child';
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
$templater->wc_display_item_meta( $item, true );
$templater->wc_display_item_downloads( $item, true );
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
?>
</td>
<td width="25%" style="float:right;">
<?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
</td>
</tr>
<?php }} ?>
</tbody>
</table>
<table class="notes">
<tr>
<td>
<?php
// Customer notes.
if ( $templater->get_option( 'bewpi_show_customer_notes' ) ) {
// Note added by customer.
$customer_note = BEWPI_WC_Order_Compatibility::get_customer_note( $order );
if ( $customer_note ) {
printf( '<strong>' . __( 'Note from customer: %s', 'woocommerce-pdf-invoices' ) . '</strong><br />', nl2br( $customer_note ) );
}
// Notes added by administrator on 'Edit Order' page.
foreach ( $order->get_customer_order_notes() as $custom_order_note ) {
printf( '<strong>' . __( 'Note to customer: %s', 'woocommerce-pdf-invoices' ) . '</strong><br />', nl2br( $custom_order_note->comment_content ) );
}
}
?>
</td>
</tr>
</table>
I want to append ---- infront of the name of product only if the product is part of any bundle. just want this check, I cant figure out how can I do that
Thanks.
Issue is that you are using Cart Functions instead of Order Functions.
First you need to use wc_pb_is_bundle_container_order_item instead of wc_pb_is_bundle_container_cart_item as we are checking against Order Items.
Further to that wc_pb_is_bundled_order_item is the check you should be using to determine if the product is a child item of an container item. This is important because it will check if item has a parent container so your code can scale when you also have non-bundled items in order as well.
Below is just partial relevant code from question.
<?php } else { ?>
<tr class="item">
<td width="10%" style="float:right;">
<?php echo $item['qty']; ?>
</td>
<td width="5%" style="float:right;">
<?php
// Adding a check to see if current item is a child of a container
$item_name_padding = wc_pb_is_bundled_order_item( $item ) ? '----': '';
echo $item_name_padding . $item['name'];
//echo 'i m child';
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
$templater->wc_display_item_meta( $item, true );
$templater->wc_display_item_downloads( $item, true );
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
?>
</td>
<td width="25%" style="float:right;">
<?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
</td>
</tr>
<?php }} ?>

Categories