modify the woocommerce customer email - wordpress

i have a website that uses woocommerce as a shopping car, usually they customer can choose different types of delivery, one of them is Local Pickup, we want the address where you can pick up your items and the time when you can pick it up to be sent in the email that the customer receive after he check out, but only if they choose local pick up these information will be sent to avoid confusion to the customer.
any idea where i can modify the text that goes out with the email, ill include a screen shot to know what i am talking about
email screenshot

Hello you can not do this with any e-mail edits because of woocommerce emails are set for the order statuses, not to the shipping methods. You could do it with code implementation into your theme's functions.php file.
Explanation: You need to add action before woocommerce email order table and set function for this action. In your function you will track your order's shipping method and if it match with local pickup then it adds extra text before the order table.
Do not forget to change this to your custom text echo '<h2 class="email-upsell-title">Local pickup instructions</h2><p class="email-upsell-p">Your text goes here for the local pickup info</p>';
UPDATE
add_action( 'woocommerce_email_before_order_table', 'localpickup_extra_info', 10, 4 );
function localpickup_extra_info( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_processing_order' ) {
foreach( $order->get_items('shipping') as $shipping_item ){
$shipping_rate_id = $shipping_item->get_method_id();
$method_array = explode(':', $shipping_rate_id );
$shipping_method_id = reset($method_array);
if( 'local_pickup' == $shipping_method_id ){
echo '<h2 class="heading-title">Local pickup instructions</h2><p class="local-text">Your text goes here for the local pickup info</p>'; //change this
break;
}
}
}
}

Related

Send customer notification emails for merchandise orders, but not for FooEvents tickets

I am hosting a webshop selling both merchandise and event tickets, using WooCommerce in combination with FooEvents.
I am having issues with the 'customer order completed' emails:
I want to send them for Merchandise items (order ready to pick up)
but do not want to send them for tickets (since these are issued automatically by email)
I tried to conditionally send the email notification based on the product categories, based on the following code:
(taken from this post: Avoid customer email notification for a specific product category in Woocommerce)
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
function product_cat_avoid_processing_email_notification( $recipient, $order ) {
if( is_admin() ) return $recipient;
// HERE set your product categories (coma separated term Ids, slugs or names)
$cat_to_filter = 'tickets';
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get the correct product ID (for variations we take the parent product ID)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Check for product categories for this item
if( has_term( $cat_to_filter, 'product_cat', $product_id ) )
return ''; // If it's found, we return an empty recipient
}
return $recipient;
}
This code works fine for excluding orders with regular products, e.g. I can exclude order completed notifications for the product category 'merchandise'. However, for tickets it does not work and results in an error: a generic error message on checkout page, the order is created but tickets are not generated nor sent.
My hunch is, that the code does not work with FooEvents, either because:
the code deletes the customer email address FooEvents needs to send the tickets, or
the code interferes with FooEvents in the area of changing order statusses to 'completed'
Can you help me out and provide me a fix for this issue?
Thanks,
Jeroen

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

Woocommerce Set Billing Address Optional

I redesigned my Woo checkout page to only show the billing address fields when the Stripe gateway is selected, otherwise if the cheque gateway is selected I do not want to collect their billing address. I have the template finished and the jQuery calls work like a charm. However, I'm having problems making the billing address fields optional and the shipping address fields required.
Here's what I have so far...I added the following filter to make both address fields not-required:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields');
function custom_override_default_address_fields( $fields ) {
// add or remove billing fields you do not want
$keys = array(
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'postcode',
'state',
'country'
);
foreach( $keys as $key ) {
$fields[$key]['required'] = false;
}
return $fields;}
I remove this filter when the Stripe gateway is selected using jQuery so that the billing address fields show as required (and re-apply if cheque is selected), this behavior works for the billing address fields.
I now need to make the shipping address fields to show as required, so I tried adding the following filter:
add_filter( 'woocommerce_checkout_fields', 'set_shipping_required_fields',9999 );
function set_shipping_required_fields( $fields ) {
// add or remove billing fields you do not want
$shipping_keys = array(
'first_name',
'last_name',
'address_1',
'city',
'postcode',
'state',
);
foreach( $shipping_keys as $key ) {
$fields['shipping']['shipping_'.$key]['required'] = true;
}
return $fields;
}
This successfully will display the shipping_first_name and shipping_last_name as required, however, the rest of the shipping fields initially render as required, but a split second later will revert back to being optional. Does anyone have any tips what I'm doing wrong to make the billing address fields optional and shipping address fields required? Is there a better way?
ou should need to use woocommerce_default_address_fields filter hook instead as explained here.
/ Billing and shipping addresses fields
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$key_fields = array('country','first_name','last_name','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $key_fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
As billing email and phone are already required by default, if you want them to be not required, you should need this additional code:
// For billing email and phone - Make them not required
add_filter( 'woocommerce_billing_fields', 'filter_billing_fields', 20, 1 );
function filter_billing_fields( $billing_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $billing_fields;
$billing_fields['billing_phone']['required'] = false;
$billing_fields['billing_email']['required'] = false;
return $billing_fields;
}
All code goes in function.php file of your active child theme (or active theme). Tested and works.

WooCommerce Product Vendors: Send notification to vendor for new order received

I am using WooCommerce Product Vendors plugin for vendor management.
Currently, when a new order is received the notification is sent to customer and admin. Once admin change order status to Processing or Completed then the email is sent to Vendor.
But I need to send that notification email when an order is received.
Can this be achieved by creating a filter in functions.php or maybe by triggering product status change notification on order received?
Updated: Added "New booking" email notification Id…
There is many ways to achieve that. Here I have 2 of them:
1). The first one, based on email ID "new Order". It is tested and works:
add_action ('woocommerce_email_customer_details', 'new_order_email_to_vendor', 20, 4 );
function new_order_email_to_vendor( $order, $sent_to_admin, $plain_text, $email ){
if( in_array( $email->id, ['new_order', 'new_booking'] ) ){
$mailer['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
2). This is untested as it's based on order status change conditions. You can set in the if statement the "from" order statuses or some "to" order statuses or both…
Here I use the "from" 'pending' order status only, as all orders are always set in pending status during the payment process in Woocommerce:
add_action( 'woocommerce_order_status_changed', 'new_order_email_to_vendor', 10, 4 );
function new_order_email_to_vendor( $order_id, $old_status, $new_status, $order ){
if ( in_array( $new_status, array( 'processing','completed') ) { // <== Updated
$emails = WC()->mailer()->get_emails();
$emails['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

WooCommerce - Making a checkout field required

on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}

Resources