Customize email subject in WooCommerce with vendor store-name (Dokan) - php

We found this question https://wordpress.stackexchange.com/questions/320924/woocommerce-order-processing-email-subject-not-changing and it's working fine. We now want to extend this by display the vendor on a order in the order completed mail.
But we are not able to output the vendor store name.
Is there a obvious error in our code?
add_filter( 'woocommerce_email_subject_customer_completed_order',
'change_completed_email_subject', 1, 2 );
function change_completed_email_subject( $subject, $order ) {
global $woocommerce;
// Order ID
$order->get_items();
// Author id
$author_id = $product->post->post_author;
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();
// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// Output subject
$subject = sprintf( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', $order->billing_first_name, $blogname, $order->get_order_number(), $shop_name );
return $subject;
}
Update:
I already tried to get the name via $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); but no success.

The use of global $woocommerce is not necessary
You use $order->get_items();, but don't do anything with it
$product is not defined
Use $order->get_billing_first_name() VS $order->billing_first_name
So you get:
function filter_woocommerce_email_subject_customer_completed_order( $subject, $order ) {
// Empty array
$shop_names = array();
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
// Author id
$author_id = $product->post->post_author;
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();
// OR JUST USE THIS FOR SHOPNAME
// Shop name
// $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
// NOT in array
if ( ! in_array( $shop_name, $shop_names ) ) {
// Push to array
$shop_names[] = $shop_name;
}
}
// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// Set subject
$subject = sprintf( __( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', 'woocommerce' ), $order->get_billing_first_name(), $blogname, $order->get_order_number(), implode( ', ', $shop_names ) );
// Return
return $subject;
}
add_filter( 'woocommerce_email_subject_customer_completed_order', 'filter_woocommerce_email_subject_customer_completed_order', 10, 2 );

Related

Add shipping state to subject in WooCommerce new order email notification

I've already succeeded in finding a way to change the subject of the WooCommerce new order email notification. The problem I'm facing is that the object $order doesn't process the shipping_state of the user profile.
Here is the code:
add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
global $woocommerce;
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf( 'Commande pour %s', $order->shipping_state);
return $subject;
}
I tried calling it directly from the user metadata, but it doesn't work, where it should appear the data, it appears blank.
Here is the code I tried:
function change_admin_email_subject( $subject, $user) {
global $woocommerce;
$user = wp_get_current_user(); $current_user_id =
$user -> ID;
$user_billing_state = get_user_meta($current_user_id, 'shipping_state', true);
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf( 'Commande pour %s',
$user_billing_state);
return $subject;
}
Any advice would be appreciated.
Your code attempt(s) contain some mistakes:
$order->shipping_state is no longer applicable since WooCommerce 3 and has been replaced by $order->get_shipping_state()
The use of global $woocommerce; is not necessary
You use $blogname, but you don't do anything with this data, for example you don't display it in the subject
So you get:
function filter_woocommerce_email_subject_new_order( $subject, $order ) {
// Get shipping state
$shipping_state = $order->get_shipping_state();
// NOT empty
if ( ! empty( $shipping_state ) ) {
// Get blogname
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
// Change subject
$subject = sprintf( __( '%s: Commande pour %s', 'woocommerce' ), $blogname, $shipping_state );
}
return $subject;
}
add_filter( 'woocommerce_email_subject_new_order', 'filter_woocommerce_email_subject_new_order', 10, 2 );

Remove Component/Variant in Order Invoice PDF in Woocommerce

I Hope are you okay.
Today I have problems with I open order invoice pdf in admin after products list have components.
I Remove all components as well
I am using woocommerce-pdf-invoices-packing-slips plugin.
Below is my product URL :
https://www.pcguru.lt/produktas/amd-home-guru/
My Invoice PDF screenshot as well.
How can I remove it?
I Searching Google but couldn't Found Anything.
After Code Debugging Step by Step, That's are Found Successfully.
File Path:
/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/documents/abstract-wcpdf-order-document-methods.php
public function get_order_items() {
$items = $this->order->get_items();
$data_list = array();
if( sizeof( $items ) > 0 ) {
foreach ( $items as $item_id => $item ) {
//Find Child Product Of Composite Product
global $wpdb;
$post_id = $wpdb->get_row("SELECT meta_value FROM pcg_woocommerce_order_itemmeta WHERE (meta_key = 'wooco_ids' AND order_item_id = '". $item_id ."')");
if($post_id->meta_value !=''){
$main_id = str_replace('/1/', '', $post_id->meta_value);
$data_ids = explode(",",$main_id);
}
//END Custom Code
$data = array();
// Set the item_id
$data['item_id'] = $item_id;
// Set the id
$data['product_id'] = $item['product_id'];
$data['variation_id'] = $item['variation_id'];
// Compatibility: WooCommerce Composit Products uses a workaround for
// setting the order before the item name filter, so we run this first
if ( class_exists('WC_Composite_Products') ) {
$order_item_class = apply_filters( 'woocommerce_order_item_class', '', $item, $this->order );
}
// Set item name
$data['name'] = apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
// Set item quantity
$data['quantity'] = $item['qty'];
//$data['product_desc'] = $item->get_product(); // Get the WC_Product objec
//echo '<pre>'; print_r($product); echo '</pre>'; die;
// Set the line total (=after discount)
$data['line_total'] = $this->format_price( $item['line_total'] );
$data['single_line_total'] = $this->format_price( $item['line_total'] / max( 1, abs( $item['qty'] ) ) );
$data['line_tax'] = $this->format_price( $item['line_tax'] );
$data['single_line_tax'] = $this->format_price( $item['line_tax'] / max( 1, abs( $item['qty'] ) ) );
$data['tax_rates'] = $this->get_tax_rate( $item, $this->order, false );
$data['calculated_tax_rates'] = $this->get_tax_rate( $item, $this->order, true );
// Set the line subtotal (=before discount)
$data['line_subtotal'] = $this->format_price( $item['line_subtotal'] );
$data['line_subtotal_tax'] = $this->format_price( $item['line_subtotal_tax'] );
$data['ex_price'] = $this->get_formatted_item_price( $item, 'total', 'excl' );
$data['price'] = $this->get_formatted_item_price( $item, 'total' );
$data['order_price'] = $this->order->get_formatted_line_subtotal( $item ); // formatted according to WC settings
// Calculate the single price with the same rules as the formatted line subtotal (!)
// = before discount
$data['ex_single_price'] = $this->get_formatted_item_price( $item, 'single', 'excl' );
$data['single_price'] = $this->get_formatted_item_price( $item, 'single' );
// Pass complete item array
$data['item'] = $item;
// Get the product to add more info
if ( is_callable( array( $item, 'get_product' ) ) ) {
$product = $item->get_product();
} else {
$product = $this->order->get_product_from_item( $item );
}
// Checking fo existance, thanks to MDesigner0
if( !empty( $product ) ) {
// Thumbnail (full img tag)
$data['thumbnail'] = $this->get_thumbnail( $product );
$data['get_parent_id'] = is_callable( array( $product, 'get_parent_id' ) ) ? $product->get_parent_id() : '';
// Set item SKU
$data['sku'] = is_callable( array( $product, 'get_sku' ) ) ? $product->get_sku() : '';
// Set item weight
$data['weight'] = is_callable( array( $product, 'get_weight' ) ) ? $product->get_weight() : '';
// Set item dimensions
$data['dimensions'] = $product instanceof \WC_Product ? WCX_Product::get_dimensions( $product ) : '';
// Pass complete product object
$data['product'] = $product;
} else {
$data['product'] = null;
}
// Set item meta
if (function_exists('wc_display_item_meta')) { // WC3.0+
$data['meta'] = wc_display_item_meta( $item, array(
'echo' => false,
) );
} else {
if ( version_compare( WOOCOMMERCE_VERSION, '2.4', '<' ) ) {
$meta = new \WC_Order_Item_Meta( $item['item_meta'], $product );
} else { // pass complete item for WC2.4+
$meta = new \WC_Order_Item_Meta( $item, $product );
}
$data['meta'] = $meta->display( false, true );
}
if (!in_array($data['product_id'], $data_ids)) {
$data_list[$item_id] = apply_filters( 'wpo_wcpdf_order_item_data', $data, $this->order, $this->get_type());
}
}
}
//echo '<pre>'; print_r($data_list); echo '</pre>'; die;
return apply_filters( 'wpo_wcpdf_order_items_data', $data_list, $this->order, $this->get_type() );
}
Solution Screenshot:
I am very happy because solution are completed.
Also Thanks to all helping guys

Display a custom text based product category order item names in Woocommerce thankyou

I got the following code in my woocommerce thankyou.php, this works if only products from ONE category is bought. When products from both the 'ebook' and the 'ticket' category are bought, 'ticket' will be added to the $productname variable.
How to determine if the products in the list are of one or multiple categories?
<?php
$email = $order->billing_email;
$order_info = wc_get_order( $order );
$productname = "";
foreach( $order_info->get_items() as $item ) {
// check if a product is in specific category
if ( has_term( 'ebook', 'product_cat', $item['product_id'] ) ) {
$productname = ' ebook';
} elseif (has_term( 'ticket', 'product_cat', $item['product_id'] )) {
$productname = 'ticket';
} else {
$productname = 'order details';
}
}
echo '<p> Your ' . $productname . ' will be send to <strong>' . $email . '</strong> as soon as the payment is received.<br>;
?>
Try the following instead that will store your product names in an array, removing duplicated value and displaying the product names in your message:
// Get the instance of the WC_Order Object from the $order_id variable
$order = wc_get_order( $order_id );
$product_names = array(); // Initializing
// Loop through order items
foreach( $order->get_items() as $item ) {
// check if a product is in specific category
if ( has_term( 'ebook', 'product_cat', $item['product_id'] ) )
{
$product_names[] = '"Ebook"';
}
elseif ( has_term( 'ticket', 'product_cat', $item['product_id'] ) )
{
$product_names[] = '"Ticket"';
}
else
{
$product_names[] = '"Others"';
}
}
$product_names = array_unique( $product_names );
echo sprintf( '<p>%s %s %s <strong>%s</strong> %s</p><br>',
_n( "Your", "Yours", sizeof( $product_names ), "woocommerce-bookings" ),
implode( ', ', $product_names ),
__("will be sent to", "woocommerce-bookings"),
$order->get_billing_email(),
__("as soon as the payment is received.", "woocommerce-bookings")
);
Tested and works

Change Woocommerce email subject if order have this specific category

I just want to know if possible to change the email subject if the order have the specific category like (Preorder). I want to put PO at beginning (PO New customer order #0000) then all other order the customer receive the default email subject (New Customer Order #0000).
add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
global $woocommerce;
global $product;
if ( has_term( 'preorder', $product->ID ) ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf( '[%s]New customer order (# %s) from %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name );
}
return $subject;
}
Note: I just copy this code somewhere.
This can be done this way, making some small changes:
add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);
function custom_admin_email_subject( $subject, $order ) {
$backordered = false;
foreach($order->get_items() as $item_id => $item ){
if ( has_term( 'preorder', 'product_cat' , $item->get_product_id() ) ) {
$backordered = true;
break;
}
}
if ( $backordered ) {
$subject = sprintf( '[PO]New customer order (# %s) from %s %s', $order->get_id(), $order->get_billing_first_name(), $order->get_billing_last_name() );
}
return $subject;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
Or it can be done this way without a product category, checking that product is backordered:
add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);
function custom_admin_email_subject( $subject, $order ) {
$backordered = false;
foreach($order->get_items() as $item_id => $item ){
$product = $item->get_product();
if( $product->get_backorders() == 'yes' && $product->get_stock_quantity() < 0 ){
$backordered = true;
break;
}
}
if ( $backordered ) {
$subject = sprintf( '[PO]New customer order (# %s) from %s %s', $order->get_id(), $order->get_billing_first_name(), $order->get_billing_last_name() );
}
return $subject;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
Use this:
function change_admin_email_subject( $subject, $order ) {
// Get all order items
$items = $order->get_items();
$found = false;
// Loop through the items
foreach ( $items as $item ) {
$product_id = $item['product_id'];
// get the categories for current item
$terms = get_the_terms( $product_id, 'product_cat' );
// Loop through the categories to find if 'preorder' exist.
foreach ($terms as $term) {
if($term->slug == 'preorder'){
$subject = 'PO '. $subject;
$found = true;
break;
}
}
if($found == true){
break;
}
}
return $subject;
}

Send a custom email to seller when product is sold in Woocommerce

I have a site that allows users(sellers) to enter products on the frontend. When there product sells I would like to email seller to let them know product has been sold.
Heres what I have so far
add_filter ('woocommerce_payment_complete', 'send_seller_email');
function send_seller_email($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
//foreach loop to get sellers email from order start
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$sent_to = wp_get_object_terms( $product_id, 'soldby', false );
$seller_send_email = $sent_to[0]->name;
$seller_email = get_user_by('ID', $seller_send_email);
$email_to_sent_sold_conformation .= $seller_email->user_email;
}//foreach loop to get sellers email end
//Send seller email start
$to = $email_to_sent_sold_conformation;
$subject = 'Sold! ship now: ' . get_the_title($product_id);
$message = '';
$message .= '<p>' . get_the_excerpt($product_id) . '…</p>';
$message .= '<p></p>';
wp_mail($to, $subject, $message );
//Send seller email end
}
I have completely revisited your code:
The main code is in an utility function called by the 2 hooked functions
function hooked in woocommerce_new_order that will trigger paid orders.
function hooked in woocommerce_order_status_changed that will trigger validated orders on update status change.
The code:
// Utility function sending the email notification
function send_seller_email( $order ) {
$data = array();
// Loop through each order item
foreach ( $order->get_items() as $item_id => $item ) {
if( get_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true ) )
continue; // Go to next loop iteration
$product_id = $item->get_product_id();
// Untested part
$soldby = wp_get_object_terms( $product_id, 'soldby', false );
if( empty($soldby) ) continue; // Go to next loop iteration
$seller_id = $soldby[0]->name;
$seller = get_user_by( 'ID', $seller_id );
$seller_email = $seller->user_email;
// Set the data in an array (avoiding seller email repetitions)
$data[$seller_email][] = array(
'excerpt' => get_the_excerpt($product_id),
'title' => get_the_title($product_id),
'link' => get_permalink($product_id),
);
// Update order to avoid notification repetitions
update_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true );
}
if( count($data) == 0 ) return;
// Loop through custom data array to send mails to sellers
foreach ( $data as $email_key => $values ) {
$to = $email_key;
$subject_arr = array();
$message = '';
foreach ( $values as $value ) {
$subject_arr[] = $value['title'];
$message .= '<p>'.$value['title'].'</p>';
$message .= '<p>'.$value['excerpt'].'…</p>';
}
$subject = 'Sold! ship now: '.implode( ', ', $subject_arr );
// Send email to seller
wp_mail( $to, $subject, $message );
}
exit();
}
add_action('woocommerce_new_order', 'new_order_seller_notification', 10, 1 );
function new_order_seller_notification( $order_id ) {
$order = wc_get_order( $order_id );
if( ! ( $order->has_status('processing') || $order->has_status('completed') ) )
return; // Exit
send_seller_email( $order );
}
add_action( 'woocommerce_order_status_changed', 'order_status_seller_notification', 20, 4 );
function order_status_seller_notification( $order_id, $status_from, $status_to, $order ) {
if( ! ( $status_to == 'processing' || $status_to == 'completed' ) )
return; // Exit
send_seller_email( $order );
}
Code goes in function.php file of your active child theme (or theme).
The code is not really tested, but doesn't make errors. It should work.

Categories