I'm trying to add some labels after Woocommerce order email items.
Ex- we can do that on cart by using this filer
add_filter( 'woocommerce_cart_item_name', array( $this, 'wc_esd_show_date_cart_page' ), 10, 2 );
above call wc_esd_show_date_cart_page() function and show its data after item name
I want to do the same for order email items, anyone know there have some filter for it, or another way to do this.
The very similar hook for orders and email notifications is woocommerce_order_item_name filter hook.
But the function arguments are quiet different and you might need to make some changes to your existing function code.
The following will target only email notifications
add_filter( 'woocommerce_order_item_name', 'wc_esd_show_date_on_order', 10, 3 );
function wc_esd_show_date_on_order( $item_name, $item, $is_visible ) {
// Only for email notifications
if( is_wc_endpoint_url() )
return $item_name;
// ---------------- Your code start below ---------------- //
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related
We have created a site using WooCommerce and WordPress with predefined products.
However, Product A must have variations, to resolve this, we added custom fields such as Colors, Dimensions, etc...
Now once the end-users are navigating to the product, we are able to fetch the custom field values. We also modified the UI so that users can pick and choose from these custom fields. (We have a preview of the modifications done via JS/CSS), so for instance, if they choose a green color we use JS to add a layer of green so that the preview is real time.
Now, we have one challenge.
-> What is the best way to go about adding this product PLUS all modifications done to the cart?
So for instance Product A was modified (on the front-end) using data pulled from the custom fields to include Color: Green and Size: 100x100 instead of defaults values. How do we store this and pass the customized product to the cart?
Appreciate the help!. Glad to add more details if something is not clear.
(There are plugins out there that can provide this functionality; Things similar to WooCommerce Product Add-On, etc... However, we have to custom develop the feature.)
It requires some steps.
1). For your product page:
First you might need to add hidden fields to the add to cart form for each custom field like (example):
add_action( 'woocommerce_before_add_to_cart_button', 'add_hidden_empty_input_fields' );
function add_hidden_empty_input_fields() {
?>
<input type="hidden" name="hidden_color" id="hidden_color" value="">
<input type="hidden" name="hidden_dimensions" id="hidden_dimensions" value="">
<?php
}
Then you will have to make some changes to your Javascript code to set the custom fields chosen values in those hidden fields.
2). Pass the custom fields selected values as custom cart item data (example):
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 10, 3 );
function add_custom_field_data( $cart_item_data, $product_id, $variation_id ) {
if ( isset($_POST['hidden_color']) && ! empty($_POST['hidden_color']) ) {
$cart_item_data['color'] = esc_attr($_POST['hidden_color']);
}
if ( isset($_POST['hidden_dimensions']) && ! empty($_POST['hidden_dimensions']) ) {
$cart_item_data['dimensions'] = esc_attr($_POST['hidden_dimensions']);
}
return $cart_item_data;
}
And optionally display values on cart items (example):
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_data, $cart_item ) {
if ( isset($cart_item['color']) ) {
$cart_data[] = array( "name" => __("Color"), "value" => $cart_item['color'] );
}
if ( isset($cart_item['dimensions']) ) {
$cart_data[] = array( "name" => __("Dimensions"), "value" => $cart_item['dimensions'] );
}
return $cart_data;
}
3). Save the custom cart item data as custom order item meta data (example):
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta_data' , 10, 4 );
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if ( isset($values['color']) ) {
$item->add_meta_data( __("Color"), $values['color'] );
}
if ( isset($values['dimensions']) ) {
$item->add_meta_data( __("Dimensions"), $values['dimensions'] );
}
}
Note: The data is going to be displayed on everywhere on orders and email notifications.
Code goes in functions.php file of the active child theme (or active theme). It should work.
The variations have a post_id, you would just use the variation post_id instead of the main product one.
In WooCommerce, I would like to add a new custom field to order details. I now that I can use use the code below to create a new custom field 'referenceNumber' and adds in it "ordercreated" value:
update_post_meta($order_id, 'referenceNumber', 'ordercreated']);
What I would like is to make that through checkout once an order is placed.
But it doesn't work it doesn't add a new custom field to order details page and don't add the value 'ordercreated', as you can see in this screenshot:
So the question is how to add a custom field when an order is placed in WooCommerce?
To add a custom field to an order you can use:
WordPress update_post_meta() function (from an order id):
$order_id = $order->get_id(); // If needed
update_post_meta($order_id, 'referenceNumber', 'ordercreated'); // add and save the custom field
WooCommerce WC_Data update_meta_data() method (from the order object or the order id):
$order = wc_get_order( $order_id ); // If needed: Get the WC_Order object from the order Id
update_meta_data('referenceNumber', 'ordercreated'); // Add the custom field
$order->save(); // Save the data
Where referenceNumber is the meta key and ordercreated is the meta value. Both works.
Now to add a custom field to an order when customer place an order, you can use:
woocommerce_checkout_create_order action hook (before order data is saved - used to adjust order data before it's saved):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_update_order_meta action hook (order already exist - used to add custom meta data):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_order_created action hook (order already exist - to trigger an action or also to add custom meta data):
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' ); // Add the custom field
$order->save(); // Save data (as order exist yet)
}
Or:
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order' );
function add_custom_field_on_placed_order( $order ){
update_post_meta($order->get_id(), 'referenceNumber', 'ordercreated');
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Hi i had a similar requirement few days ago, as i needed to add new field at checkout. Following article helped me. You can check it too. Here is the link. Basically you will have a write a function which will use woo commerce hook "woocommerce_default_address_fields"
https://wisdmlabs.com/blog/add-custom-fields-woocommerce-checkout-page/
Let me clear my question:
I have downloaded & activated WooCommerce Plugin for E-Commerce Functionality.
I want to add "Applied coupon code" in Admin New Order Email Template using my custom plugin.
Now:
Can you tell me that exact Hook or Function which is actually setting up that New Order Email Template so that i will override it?
Can you tell me how to call applied coupon code, so that i will display this in email template?
It would be great, if you help me please.
This can be done using a custom function hooked in woocommerce_email_order_details action hook (for example) that will display in admin emails notifications the used coupons in the order:
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works...
This is because $coupon_codes is not an array() define it with $coupon_codes=array();
In WooCommerce, How and where I can apply some code for New Order Email notification, where I should get the Applied Coupon Code display.
In the template for New Order Email notification I have #hooked WC_Emails::order_details() that shows the order details table…
Also is WC_Email::order_details() the exact hook which I am searching for to update coupon code in New order mail to admin?
I am loosed, any help will be really appreciated…
The hook that you are searching is woocommerce_email_order_details action hook that can be used this way:
add_action( 'woocommerce_email_order_details', 'action_email_order_details', 10, 4 );
function action_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
if( $sent_to_admin ): // For admin emails notification
// Your code goes HERE
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
In your function you can use any of the 4 arguments from the hook like $order the WC_Order object.
You can use the WC_Abstract_Order method get_used_coupons() that will give you an array of coupons codes used in the order, like:
// Get the array of coupons codes
$coupon_codes = $order->get_used_coupons();
// Convert and display the array in a coma separated string:
echo 'Coupon codes: ' . implode( ', ', $coupon_codes );
I am very new to WordPress, and have created an e-commerce store with WooCommerce.
After the customer places an order, I get an email and the customer get an email- one for me to say what they have ordered, and one to them as a thank you email.
Within this thank you email, in my functions.php file, I have learned to change the subject of the header to include their name such as this:
//add the first name of the person to the person getting the reciept in the subject of the email.
add_filter('woocommerce_email_subject_customer_processing_order','UEBC_change_processing_email_subject', 10, 2);
function UEBC_change_processing_email_subject( $subject, $order ) {
global $woocommerce;
$subject = 'Thanks for your ' . get_bloginfo( 'name', 'display' ) . ' Order, '.$order->billing_first_name .'!';
return $subject;
}
The code snippet above works correctly, and is only displayed to the customer, not to me. e.g. "thanks for your order ABCD Clothes order John!".
Within the body of the email, I am trying to make this personal as a small thank you message, however, when I make the message, I am using the hook:
add_action( 'woocommerce_email_before_order_table', 'custom_add_content', 20,1 );
I know that since im using the woocommerce_email_before_order_table hook, the custom function will be send in the body of the email to both the customer and myself.
I was wondering, is there a hook that Woocommerce provides so that the custom function will only be sent to the customer within the body of the email?
For example: woocommerce_email_header_customer_processing_order or words to that effect?
Thanks
To add some custom content using woocommerce_email_before_order_table hook and targeting just the customer "processing order" email notification, you should try this:
add_action( 'woocommerce_email_before_order_table', 'custom_content_to_processing_customer_email', 10, 4 );
function custom_content_to_processing_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_processing_order' == $email->id ){
// Set here as you want your custom content (for customers and email notification related to processing orders only)
echo '<p class="some-class">Here goes your custom content… </p>';
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
This code is tested and works