with some help I manage to create a plugin to attach, on the finished order email, an invoice.
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'customer_completed_order' );
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
$pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
$attachments[] = $pdf_path;
}
return $attachments;
}
This code is to check on the order meta for "email_fatura" (translated "email_invoice"), and get the value of this field. This value takes the path root /Faturas/GestaoDespesas/ORDER123.pdf and attach the pdf to the email.
However, the problem is when there isn't a field "email_fatura", it stills attachs a file called "GestaoDespesas", that comes from /Faturas/**GestaoDespesas**/
For those who know PHP I assume that it is easy to fix this.
Thank you in advance for any kind of help.
I would first check whether the field is empty or not, if it is then just return:
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'customer_completed_order' );
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
if ( empty($pdf_name) ){
return;
}
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
$pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
$attachments[] = $pdf_path;
}
return $attachments;
}
Related
I have this function which adds custom meta field to the product detail in all WooCommerce emails. But I need to show only after the order is paid (this can be also just the "completed" email).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
I hoped that I can nest it inside if ( $email->id == 'customer_completed_order' ) {}, so the final code will look like this:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
if ( $email->id == 'customer_completed_order' ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
}
But it stops working after that change. Any advice?
As you can see in your code attempt, $email is not part of the woocommerce_order_item_meta_start hook. So to target certain WooCommerce email notifications, you will need a workaround.
Step 1) creating and adding a global variable, via another hook that only applies to WooCommerce email notifications.
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
Step 2) In the hook woocommerce_order_item_meta_start, use the global variable so we can target certain WooCommerce email notifications
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the email ID global variable
$ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
$email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
// NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma
if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) {
// Get meta
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
// OR use to get meta
// $ot_address = $item->get_meta( 'ot_address' );
if ( ! empty( $ot_address ) ) {
printf( '<div>' . __( 'Terms: %s', 'woocommerce' ) . '</div>', $ot_address );
}
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
I have a function which help me to make a redirect of a product if that product doesn't exist anymore in my affiliate xml.
What I want now is to put the product on draft but to redirect his link to a "This products doesnt exist anymore page" - for seo purposes
I didn't try something because I dont know what.
I saw something here and I think my answear is here but i don't know how to apply it:
Change product status if prices are updated in Woocommerce 3
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I expect that my code to remove the product from my website feed and redirect his link to a page of my website.
For seo purposes I cannot delete the product for all I just need to hide it from my shop but keep the link for google (also the pictures).
You can use WordPress function to update the status of the product where your logic of product doesn't exist anymore in my affiliate xml. exists.
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
/* Start Here*/
$my_product = array(
'ID' => $import->id,
'post_status' => 'draft',
);
wp_update_post( $my_product );
/* End Here*/
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I am trying to change/add in the title of the "Order Recieved" Woocommerce page.
The below snippet works - I am able to change the pre-existing TEXT with the following code:
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = $str . ' We have emailed the purchase receipt to you.';
return $new_str;
}
The below snippet does not work. - I am unable to change/add the TITLE and also pass in the username to personalise it. Here is the code and also an image of the output I am trying to achieve....The "You are awesome FIRSTNAME" added in.
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
This should be possible as there are examples on how to do it, such as here...I just can't figure out why the main title wont even appear?
As a workaround I inspected the CSS and changed the text below the header to be a larger size and the font family required.
Then through the below PHP I created the custom text with the customer name in the header.
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = sprintf( "You are awesome, %s :) - We've recieved your order.", esc_html( $order->get_billing_first_name() ) );
return $new_str;
}
2022 update:
function ps_title_order_received( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
unset( $order );
}
}
if ( isset ( $order ) ) {
$title = sprintf( "Thank you, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
add_filter( 'the_title', 'ps_title_order_received', 10, 2 );
I'm trying to add a custom field jckwds_date as an order note. I can't for the life of me figure out why this code isn't working in functions.php?
The code also only allows the note to be added in the are a certain role type.
function wdm_my_custom_notes_on_single_order_page($order){
$user = wp_get_current_user();
$allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');
if( array_intersect($allowed_roles, $user->roles ) ) {
$value = get_post_meta( $product->get_id(), 'jckwds_date', true );
echo $value;
$order->add_order_note( $value, $is_customer_note = 1 );
}
}
Basically I need THIS:
To be added HERE:
Update:
The following code will add from the order custom field 'jckwds_date' (or checkout posted field value 'jckwds_date') an order note that will appear in backend for defined user roles:
add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 );
function product_custom_field_to_custom_order_notes( $order_id, $data ){
// HERE define allowed user roles
$allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer');
$user_id = get_post_meta( '_customer_user', 'jckwds_date', true );
$user = new WP_User( $user_id );
// Exit if no matched user roles
if( ! array_intersect( $allowed_roles, $user->roles ) ) return;
// Get the date custom field (or checkout field)
if( get_post_meta( $order_id, 'jckwds_date', true ) ){
$note = get_post_meta( $order_id, 'jckwds_date', true );
} elseif( isset( $_POST['jckwds_date'] ) ){
$note = sanitize_text_field( $_POST['jckwds_date'] );
}
// The order note
if( isset($note) && ! empty($note) ){
$order = wc_get_order( $order_id ); // The WC_Order Object
$order->add_order_note( $note ); // Add the note
$order->save(); // Save the order
}
}
Code goes in function.php file of the active child theme (or active theme). It should work.
add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_add_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
$fields['order']['order_note']['priority'] = 5;
$fields['order']['order_note'] = array(
'label' => __('Order Notes', 'woocommerce'),
'placeholder' => _x('Order Notes', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
Try this
$order = wc_get_order( $order_id );
$note = __("This my custom note...");
$order->add_order_note( $note );
$order->save();
Try this
add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta');
function checkout_field_update_order_meta($order_id)
{
if (!empty($_POST['field_name'])) {
update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name']));
}
}
Try this code.
add_action( 'woocommerce_thankyou', 'my_note_custom' );
function my_note_custom( $order_id ) {
$order = new WC_Order( $order_id );
$note = __("This my custom note...");
$order->add_order_note( $note );
$order->save();
}
Found out it was a simple as changing the $product to $order as it is the order custom field value I'm trying to retrieve.
Full code below:
function wdm_my_custom_notes_on_single_order_page($order){
$user = wp_get_current_user();
$allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');
if( array_intersect($allowed_roles, $user->roles ) ) {
$value = get_post_meta( $order->get_id(), 'jckwds_date', true );
$note = '<b>Wholesale</b>';
echo $value;
$order->add_order_note( $value, $is_customer_note = 1 );
}
}
I noticed that the customer on hold order email is not available so i tried to replace the actions with a single action that would send the appropriate email.
This seems to work except for the on-hold status. I dont see what the difference is between the on-hold and processing case other than its not in the $available_emails in class-wc-meta-box-order-actions.php and I have removed all the others and they still work.
What I am doing wrong? Is it a way to make this possible?
My code is:
function ulmh_resend1( $actions ) {
$actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
return $actions;
}
function ulmh_resend2( $order ) {
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if ($order->has_status( 'on-hold' )) {
$eml = 'customer_on_hold_order';
}elseif ($order->has_status( 'processing' )) {
$eml = 'customer_processing_order';
}elseif ($order->has_status( 'completed' )) {
$eml = 'customer_completed_order';
} else {
$eml = "nothing";
}
if ( ! empty( $mails ) ) {
foreach ( $mails as $mail ) {
if ( $mail->id == eml ) {
$mail->trigger( $order->id );
}
}
}
}
function ulmh_resend3( $order_emails ) {
$remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
$order_emails = array_diff( $order_emails, $remove );
return $order_emails;
}
add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );
I have revisited and compacted your code because there where some errors like a typo error in if ( $mail->id == eml ){ for eml as a variable nameā¦ Also to get the Order ID from the WC_Order object you should use $order->get_id() method instead of $order->id.
Here is this new functional code:
add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
function ulmh_resend1( $actions ) {
$actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
return $actions;
}
add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
function ulmh_resend2( $order ) {
$wc_emails = WC()->mailer()->get_emails();
if( empty( $wc_emails ) ) return;
if ($order->has_status( 'on-hold' ))
$email_id = 'customer_on_hold_order';
elseif ($order->has_status( 'processing' ))
$email_id = 'customer_processing_order';
elseif ($order->has_status( 'completed' ))
$email_id = 'customer_completed_order';
else
$email_id = "nothing";
foreach ( $wc_emails as $wc_mail )
if ( $wc_mail->id == $email_id )
$wc_mail->trigger( $order->get_id() );
}
add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );
function ulmh_resend3( $order_emails ) {
$remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
$order_emails = array_diff( $order_emails, $remove );
return $order_emails;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested in WooCommerce 3+ and works fine now for on-hold order status email notifications, when resending