Edit Product Link in No Stock Admin Notification Email - php

I would like to have an 'Edit product' link in the 'No stock' emails that the admin receives after a sale/ product is changed to out of stock.
This type of code works fine for getting all post meta/custom fields, but for post edit link it only works intermittently, really struggling to figure out what i'm doing wrong, tried various ways i know of getting edit_post and googled 100s of posts here on stack but only this one works now and then, need it to work all the time. Many thanks in advance if anyone can help..
function filter_woocommerce_email_content_edit_me_link( $message, $product ) {
$id = $product->get_id();
$edit_link = get_edit_post_link( $id, $product );
$string .= '<p class="edit-this">Edit Product</p>';
return $string;
}
// add the filter
add_filter( 'woocommerce_email_content_no_stock', 'filter_woocommerce_email_content_edit_me_link', 10, 12 );
// by various ways i mean with message or string as per all the returns as below, with a backslash ed/edi/edit it also worked but only upon manually placing an order, not when other users place an order.
//return get_edit_post_link($this->ID);
//echo get_edit_post_link($post->ID);
//return $message."\nEdit ".$edit_link;
//return $message. " \n " .get_edit_post_link(intval($product->product_id)) .$edit_link;
//return $edit_link. " edit\ " .get_edit_post_link(intval($product->product_id)) .$edit_link;
//return $message." \nedit " .$edit_link;
//return $message. " \ " .$edit_link;
//return $string .= ' ' .get_edit_post_link($product->product_id) .'';

Code below works fine for admin user who places the order but if others place the order, there is no edit link in the admin no stock email..
Using the global $product doesn't seem to help.
function filter_woocommerce_email_content_edit_me_link( $message, $product ) {
$id = $product->get_id();
$edit_link = get_edit_post_link( $id, $product );
return $message." edit product " .$edit_link;
}
// add the filter
add_filter( 'woocommerce_email_content_no_stock','filter_woocommerce_email_content_edit_me_link', 10, 12 );

Related

How to edit layout of orders table (orders.php)

I want to change the layout of table present in orders.php. Specifically, instead of having rows containing the data (name, date, status, etc.), I want separate horizontal cards for each product.
Example: https://i.stack.imgur.com/itdGU.png. I'm trying to do this with a shortcode, but I'm not sure if that's the right way. Can anyone give me directions ?
For now I'm trying this way: The code below works and returns all order numbers correctly. However I am getting all the order numbers side by side as a simple line of text: https://i.stack.imgur.com/RqdVC.png. I don't know how to insert div, or css classes to change the layout.
Even if I add css classes, the question that remains is how to add other elements such as name, price, status etc. Not sure it can all be done in one shortcode. Advice ?
// GET ALL ORDERS NUMBER OF CURRENT USER
// Give the callback function a clear and descriptive name.
add_shortcode( 'prcsed_order_numbers' , 'prcsed_order_1' );
function prcsed_order_1() {
// Get all orders for the current user.
$customer_orders = wc_get_orders([
'customer_id' => get_current_user_id(),
]);
// Transform the array of order objects into an array of order names.
$order_numbers = array_map( function( $order ) {
return $order->get_order_number();
}, $customer_orders );
// Return as a string, ready for output,
return implode( ', ', $order_numbers );
}
Expanding on what I was typing before. You can access more order variables using what you were already doing in $order_numbers. Here is what I have tested on standalone WooCommerce test site. You will need to input your customer ID function and modify the return HTML structure for styling and editing simplicity. What I have is not clean but does work.
$results = wc_get_orders([ 'cutomer_id'=>1 ]);
foreach ( $results as $order ) {
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$status = $order->get_status();
echo '<h1>' . $first_name . ' ' . $last_name . '</h1><p>' . $status . '</p>';
}
The following link gives you the functions/hooks for the order object that you will need accessing to. https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Woocommerce custom shortcode returns zero value

I have created a custom Thank you page in wordpress which has this paragraph:
Thank you so much for your order!
After paying off [cart_total] via XYZ website, please fill out
this form and inform us about your payment.
And tried this custom shortcode:
// Total Price Shortcode
function cart_wctotal(){
global $woocommerce;
$wctotal = $woocommerce->cart->get_cart_total();
return "<span class='cart-total'> ".$wctotal."</span>";
}
add_shortcode( 'cart_total', 'cart_wctotal' );
But when I check out orders the output returns zero value for Total Price :
Thank you so much for your order!
After paying €0.00 via XYZ website, please fill out this form and
inform us about your payment.
See comment from #xhynk why your code is not going to work.
Get last order (and order total) by user id
function cart_wctotal(){
// Get user id
$user_id = get_current_user_id();
// Get last order by user id
$last_order = wc_get_customer_last_order( $user_id );
// Order total
$wctotal = $last_order->get_total();
return "<span class='cart-total'> " . $wctotal . "</span>";
}
add_shortcode( 'cart_total', 'cart_wctotal' );

How do I add order note on (advanced) custom fields update?

I'd like to add an order note to log the time I added tracking number to existing order. I'm using Advanced Custom Fields to store trackings. Tried function below, and it breaks my site:
function tt_add_tracking_note( $order ) {
$tt_order = $order->id;
if (get_field('tt-track', $tt_order)) {
$tt_tracknum_note = 'Added tracking number ' . the_field('tt-track', $tt_order);
}
$tt_order->add_order_note( $tt_tracknum_note );
}
add_action( 'woocommerce_process_shop_order_meta', 'tt_add_tracking_note', 10, 2 );
What is wrong and how do I add a note the right way?

Change New Order email notification from name in Woocommerce

Email notification of New order process has 'My Blog' title.
I look into Woocommerce setting but could not find it.
Any Idea how to change 'My Blog' to 'X Company'
red underline text in attached images.
Plateform: Wordpress + Woocommerce
Update:
What you want to change is the "From name" and it can be changed using:
add_filter('woocommerce_email_from_name', 'change_new_order_email_from_name', 10, 2 );
function change_new_order_email_from_name( $from_name, $email ){
if( $email->id === 'new_order' )
$from_name = __("ACME corp");
return $from_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Addition: To add custom placeholders for the email subject (for woocommerce 3.2+):
// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'custom_email_format_string', 20, 2 );
function custom_email_format_string( $string, $email ) {
// Get the instance of the WC_Order object
$order = $email->object;
// Additional wanted placeholders in the array of find / relace pairs
$additional_placeholders = array(
'{shop_company}' => __("ACME corp"),
);
// return the clean string with new replacements
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Then in your email settings, on the Subject field of an email notification you will be able to replace for example:
Your {site_title} order receipt from {order_date}
by
Your {shop_company} order receipt from {order_date}
Please use this WooCommerce hook woocommerce_email_subject_new_order to change new order email title.
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ){
// Get an instance of the WC_Email_New_Order object
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
// Get unformatted subject from settings
$subject = $email->get_option( 'subject', $email->get_default_subject() );
// Loop through order line items
$product_names = array();
foreach( $order->get_items() as $item )
$product_names[] = $item->get_name(); // Set product names in an array
// Set product names in a string with separators (when more than one item)
$product_names = implode( ' - ', $product_names );
// Replace "{product_name}" by the product name
$subject = str_replace( '{product_name}', $product_names, $subject );
// format and return the custom formatted subject
return $email->format_string( $subject );
}
For more details see this link
Go to
WooCommerce > Settings > Emails > Processing Orders.
Here you will find a field called "Email Subject". Here, change {site_title} to whatever you want it to appear.
Alternatively, if you want to change the value of {site_title} itself, then head over to Settings > General.
Here you will find the field called Site Title. Change it to whatever you want it to appear.
Let me know if it works!
I found the solution after digging, its very easy.
WooCommerce>Setting>Emails
At the bottom, there is section where you can header and footer text.
Simple.
Really appreciate your help #LoicTheAztec

AffiliateWP + WooCommerce - Need Method of Pulling Nickname from ID

So, this one has my head in for a loop. I've contacted the Support of the plugin and they basically told me I'm SOL, but I refuse to believe there isn't a way to do this.
I'm using AffiliateWP to attribute sales to a single location. Each Affiliate feeds off of a Wordpress User, and for their nickname I've used their address/location.
Unfortunately, the plugin doesn't include this in the Order Details screen or Order Emails, which is a BIG problem. I've frankensteined some code together but keep getting all kinds of undefined errors because they do everything inside classes, which I guess makes sure that people like me can't fiddle in there?
Function for assigning an affiliate to an order: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/integrations/class-woocommerce.php#L78
Function for retrieving the Affiliates User ID: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L204
Function for retrieving the Affiliates Name: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L132
[Edit] And finally, my Frankenstein:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
This doesn't throw me an error, but it does throw me a blank area...
Try this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
$order_id = $order->get_id();
$referrals = affiliate_wp()->referrals->get_by( 'reference', $order_id);
$affiliate_id = $referrals->affiliate_id;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
the affwp_get_affiliate_name method returns blank on three conditions:
if ( ! $affiliate = affwp_get_affiliate( $affiliate ) ) {
return '';
}
if ( ! $user_info = get_userdata( $affiliate->user_id ) ) {
return '';
}
...
// If neither are set, return an empty string.
if ( empty( $first_name ) && empty( $last_name ) ) {
return '';
}
Start by manually passing an affiliate id you know to be associated with a record that contains a $first_name and/or $last_name, for example:
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name(5) . '</p>';
This will rule out if the problem is with the $affiliate_id that you're currently passing it. If you get values returned, it should be a simple matter to fix.
After trying that I would experiment with changing the priority of the add_action(), raising it to 999 or something because it could possibly be executing before the relevant hooks in the plugin.

Categories