How to add Unique nos in order email for each unit of woocommerce product sold
Date : March 29 2020, 07:55 AM
it helps some times I found a solution if this helpful for you. I think you can store unique nos in database order item meta when product sold. Put this hook in your active theme functions.php file. add_action( 'woocommerce_add_order_item_meta', 'add_random_number_item_meta', 10, 2 );
function add_random_number_item_meta( $item_id, $item_data ){
// store each product item quantity in array
$random = array();
for( $i = 0; $i < $item_data['quantity']; $i++ )
$random[$i] = rand(10000, 90000);
// save unique numbers array each order item
wc_add_order_item_meta( $item_id, '_unique_nos', $random );
}
//woocommerce\emails\email-order-items.php
$unique_numbers = wc_get_order_item_meta( $item_id, '_unique_nos', true );
echo implode( "," , $unique_numbers );
// This will be same both for admin or customer
|
Get the product name or id in Woocommerce order email notifications
Date : March 29 2020, 07:55 AM
it helps some times As an order can have many items you can have many products names in an order. You need to get order items first and to loop through each one to get the product names or IDs. This can be done using the following (that will replace your actual code): add_action( 'woocommerce_email_after_order_table', 'custom_email_after_order_table', 10, 4 );
function custom_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// Displaying the shipping method used
echo '<p><h4>'.__('Shipping', 'woocommerce').':</h4> '.$order->get_shipping_method().'</p>';
$product_names = array();
// Loop thougth order items
foreach( $order->get_items() as $item_id => $item ){
$product = $item->get_product(); // Get an instance of the WC_Product object
$product_id = $item->get_product_id(); // Get the product ID
// Set each product name in an array
$product_names[] = $item->get_name(); // Get the product NAME
}
// Displaying the product names
echo '<p><strong>'.__('Product names', 'woocommerce').':</strong> <br>'.implode( ', ', $product_names ).'</p>';
}
|
Display product attribute value from order item in Woocommerce email subject
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Orders can have many items, and there is some errors in your code since Woocommerce 3. The code below will search through order items for a specific product attribute (taxonomy) and if it's found, it will display a new custom subject with this product attribute term name value: add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
// HERE define the product attribute taxonomy (start always with "pa_")
$taxonomy = 'pa_color'; //
// Loop through order items searching for the product attribute defined taxonomy
foreach( $order->get_items() as $item ){
// If product attribute is found
if( $item->get_meta($taxonomy) ){
// Custom new subject including the product attribute term name
$subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
get_term_by('slug', $item->get_meta($taxonomy), $taxonomy )->name, // Term name
wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
$order->get_id(),
$order->get_billing_first_name(),
$order->get_billing_last_name()
);
break; // Stop the loop
}
}
return $subject;
}
|
Product title below the thumbnail in Woocommerce email notifications
Tag : php , By : BinaryBoy
Date : March 29 2020, 07:55 AM
Does that help Updated: If the product image are displayed in your email notifications, you can try the following to display the product title under this image: add_filter( 'woocommerce_order_item_name', 'product_title_under_thumbnail_emails', 10, 3 );
function product_title_under_thumbnail_emails( $item_name, $item, $is_visible ) {
// Targeting view order pages only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $product->get_image_id() > 0 && $is_visible )
$item_name = '<br>' . $item_name;
return $item_name;
}
|
WooCommerce Add Product Link in Processing Order Email
Date : March 29 2020, 07:55 AM
|