How to remove price from Woocommerce Email - php

I am trying to develop a plugin that send the customer a "gift receipt" upon completing an order. I have everything working perfectly except I am unable to strip the price from the emails. When I try to edit the email-order-items.php template and remove the price column then the emails come in blank.
Specifically this is for downloadable products so the download links no longer show when I make any edits to the email-order-items file. And I would only want it to strip the price from the gift receipt emails not the other emails.
What I have done:
in my plugin I call on a email template "customer-gift-receipt.php" which is pretty much the same as "customer-processing-order.php" that comes package with Woocommerce.
In the file there is this line that brings in the email-order-items template and shows the links and price
<?php echo $order->email_order_items_table( $order->is_download_permitted(),
true, ($order->status=='processing') ? true : false ); ?>
This is the email-order-items template.
No matter what I do I cannot seem to get those prices stripped from just the customer-gift-receipt.php emails. Specifically it is this line in the above template:
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
I tried creating a duplicate of the email-order-items template and removing that line then calling it in my plugin and that did not work. I also tried copying the email-order-items template inside of the customer-gift-receipt.php file in the appropriate section and that failed as well. I had to define $items = $order->get_items(); when I tried copying email-order-items directly into the customer-gift-receipt template for it to somewhat work.
So can anyone suggest a way for me to strip the prices out of my customer-gift-receipt templates?
I have checked through these links:
Class WC_Order
Class WC_Email_Customer_Processing_Order
UPDATE:
I just found this link which should help me bring in email_order_items_table outside of the class: https://gist.github.com/mikejolley/1965842
When I try to add the above code in my customer-email-receipt template and place an order I get this error:
Fatal error: Class 'order_item_meta' not found in
.../.../.../.../woocommerce/emails/customer-gift-receipt.php on line 41"

Disable e-mail order items table from e-mail template and copy the function as a custom function into your theme.
<tbody>
<?php //echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false );
echo custom_order_table($order);
?>
</tbody>
I had to remove download and variations which causes *Fatal Error with Class 'order_item_meta'* error. So your custom function will look like:
function custom_order_table($order,$price = false) {
foreach($order->get_items() as $item) :
$_product = $order->get_product_from_item( $item );
$file = $sku = $variation = $image = '';
if ($show_image) :
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail');
$image = apply_filters('woocommerce_order_product_image', '<img src="'.$src[0].'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />', $_product);
endif;
if ($show_sku && $_product->get_sku()) :
$sku = ' (#' . $_product->get_sku() . ')';
endif;
$return .= '<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'. $image . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'.$item['qty'].'</td>';
if ($price):
$return .= '<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">';
if ( $order->display_cart_ex_tax || !$order->prices_include_tax ) :
$ex_tax_label = ( $order->prices_include_tax ) ? 1 : 0;
$return .= woocommerce_price( $order->get_line_subtotal( $item ), array('ex_tax_label' => $ex_tax_label ));
else :
$return .= woocommerce_price( $order->get_line_subtotal( $item, true ) );
endif;
$return .= '</td>';
endif;
$return .= '</tr>';
// Show any purchase notes
if ($show_purchase_note) :
if ($purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) :
$return .= '<tr><td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . apply_filters('the_content', $purchase_note) . '</td></tr>';
endif;
endif;
endforeach;
echo $return;
}

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>

How to apply PHP inside HTML that is running in a if statement

I need to get this code running but i need your help. It is a PHP script with a if statement combined to a categorie. The problem is that the output isn't compleet. The php action that give back a value that needs to be apply'd on the product page in the backend off Wordpress should be displays in the tabel after the day prijs but isn't visible.
<?php
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_category_slug' );
function bbloomer_single_category_slug() {
if ( has_term( 'verhuur-mini-gravers', 'product_cat' ) ) { ?>
<div class="view-right">
<h2>Verhuur prijzen</h2>
<table style="width:100%; border: 1px #000;">
<tr>
</tr>
<tr>
<td style="font-weight:500;">Dag prijs</td>
<td style="font-weight:500;">4 dagen prijs</td>
<td style="font-weight:500;">Week prijs</td><td>
</tr>
<tr>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_17', true ) ?></td>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_18', true ) ?></td>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_19', true ) ?></td>
</tr>
<tr>
</tr>
</table>
</div>
<?php; } elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else';
}
}?>
The value of the _text_field_17 should be visible with the value. Now the get_post_meta is not visible or not running.
Welcome!
Good thing that you wrote a function to do so. You might consider indenting your codes, so that you can see the bugs before testing.
One way is to just define a string $html and store everything in it and at the end just return that, maybe similar to:
add_action('woocommerce_after_single_product_summary', 'bbloomer_single_category_slug');
function bbloomerSingleCategorySlug($post)
{
$html = '';
if (isset($post) && has_term('verhuur-mini-gravers', 'product_cat')) {
$html .= '<div class="view-right">';
$html .= '<h2>Verhuur prijzen</h2>';
$html .= '<table style="width:100%; border: 1px #000;">';
$html .= '<tr></tr>';
$html .= '<tr><td style="font-weight:500;">Dag prijs</td><td style="font-weight:500;">4 dagen prijs</td><td style="font-weight:500;">Week prijs</td><td></tr>';
$html .= '<tr><td>€ ' . get_post_meta($post->ID, "_text_field_17", true) . '</td>';
$html .= '<td>€ ' . get_post_meta($post->ID, "_text_field_18", true) . '</td>';
$html .= '<td>€ ' . get_post_meta($post->ID, "_text_field_19", true) . '</td>';
$html .= '</tr><tr>';
$html .= '</table>';
$html .= '</div>';
} elseif (has_term('tables', 'product_cat')) {
echo 'Something else';
} else {
echo 'Something is not right! Maybe, check the $post variable!';
}
echo $html;
}
I'm not so sure about your variables in Woocommerce, you may want to check on that. If you wish, you can pass $post as a variable in the function.

Display order customer note in Woocommerce email notifications

In Woocommerce, I am trying to get the customers additional order message:
I am trying to display that customer message on the email notifications.
But i don't know how to get this information in the php code.
Here is my code in the functions.php file:
add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $item_id, $item, $order, $plain_text){
$notes=$order->customer_message; //did not work
echo '<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 1px solid #e5e5e5;" border="0"><tbody><tr><td><p>Test: ' . $notes . '</p></td></tr></tbody></table>';
}
I really don't know how to access that information.
Any help is appreciated.
There are some errors in your code. Using the WC_Order method get_customer_note(), try the following instead, that will display for some successful email notifications, the customer order note:
add_action( 'woocommerce_email_after_order_table', 'customer_note_email_after_order_table', 10, 4 );
function customer_note_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ){
// Only on some email notifications
if ( in_array( $email->id, array('new_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order') ) ) :
// Get customer Order note
$customer_note = $order->get_customer_note();
// Display the Customer order notes section
echo '<h2>' . __("Order notes", "woocommerce") . '</h2>
<div style="margin-bottom: 40px;">
<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
<tr><td><p>' . $customer_note . '</p></td></tr>
</table></div>';
endif;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Add custom meta data into emails as a html styled table with a title in Woocommerce

In WooCommerce, "Get a custom field array values within WooCommerce email order meta" answer code to one of my previous questions, has given me the way to pull fields out of an array from the order post meta data.
But how would I take this code and adjust it, to add a heading above the new data and a table around the data, within the email?
I would like to add a <h2>Attendee Info</h2> above the output of the fields. In addition I'd like to wrap the field output in a <table>.
I would like to add a few of the fields as new columns within the admin Order Table screen, via some sort of function.
In addition, I would also like to be able to display some of these fields within the Admin Order Table, as new columns.
This can be done using instead woocommerce_email_order_details action hook this way:
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email )
{
$event = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );
if( ! is_array($event) ) return;
$event = isset($event[1][1]) ? $event[1][1] : '';
if( sizeof($event) == 0 ) return;
$custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';
// Set our array of needed data
$fields_array = [
__('First name') => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
__('Last name') => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
__('Title') => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
__('Organization') => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
__('Address') => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
__('City') => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
__('Postcode') => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
__('State') => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
];
if( ! $event ) return;
// The HTML Structure
$html_output = '<h2>' . __('Attendee Info') . '</h2>
<div class="discount-info">
<table cellspacing="0" cellpadding="6"><tbody>';
// Loop though the data array to set the fields
foreach( $fields_array as $label => $value ):
if( ! empty($value) ):
$html_output .= '<tr>
<th>' . $label . '</th>
<td>' . $value . '</td>
</tr>';
endif;
endforeach;
$html_output .= '</tbody></table>
</div><br>'; // HTML (end)
// The CSS styling
$styles = '<style>
.discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.discount-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
.discount-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>';
// The Output CSS + HTML
echo $styles . $html_output;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
or also using woocommerce_email_order_meta action hook, replacing the first line:
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
by this:
add_action('woocommerce_email_order_meta', 'woocommerce_email_order_meta', 25, 4 );
You will get something more clean and formatted with your title like:
To get this data displayed in admin order table is something much more complicate and too broad to be answer in this answer or on stackOverFlow.
You should better display that data in a custom meta box in order edit pages, which is much more easier and practical.

How to hide VAT label and value from WooCommerce order emails?

I want to hide or remove VAT (tax) line from WooCommerce order emails. Result comes out from this function "get_order_item_totals()". Already I can hide tax label from emails by using following code.
function sv_change_email_tax_label( $label ) {
$label = '';
return $label;
}
add_filter( 'woocommerce_countries_tax_or_vat', 'sv_change_email_tax_label' );
I want to hide the entire row of VAT from order emails.
Finally figured out a way do this. "get_order_item_totals()" function returns an Array of arrays. so i unset() the unwanted array. in this case $totals[tax]
following is my code in email template.
<?php
if ( $totals = $order->get_order_item_totals() ) {
unset($totals[tax]);
$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
}
}
?>
Thank you very much everyone who tried to help!
Regards!
Depending on the status of the order, you'll need to edit the relevant email template.
For eg: If you have COD payment method enabled and the customer selected that option, the order status will be "Processing", in that case you need to edit customer-processing-order.php template.
//find this line and after it add the following line of code
foreach ( $totals as $total ) {
//ensure to change "VAT" with the label that appears on your email
if( strstr( $total['label'], "VAT" ) ) continue;

Categories