Hook JS into WooCommerce Email function - php

I am trying to add some JS for Trustpilot in my WooCommerce processing order emails (customer-processing-order.php). I am currently trying to just echo it with a function in functions.php like so:
add_action ('woocommerce_email_header', 'add_trustpilot_script', 9999, 3);
function add_trustpilot_script ($headers, $email_id, $order ){
$theSku = '';
foreach ( $order->get_items() as $item_id => $item ) {
$sku = $item->get_sku();
$theSku .= $sku . ',';
}
$tSku = substr($theSku, 0,-1);
echo '<script type="application/json+trustpilot">
{
"recipientName": "'. $order->get_billing_first_name().'",
"recipientEmail": "'. $order->get_billing_email().'",
"referenceId": "'. $order->get_id().'",
"productSkus": ["'.$tSku.'"]
}
</script>';
}
But the above is not working :( any help appreciated.

Your code contains some minor errors
$order is not an argument in the woocommerce_email_header hook
$email->id is used in an if condition, to target the customer_processing_order email
$item->get_sku(); is replaced by $product->get_sku();
So you get:
function action_woocommerce_email_header( $email_heading, $email ) {
// Only for order processing email
if ( $email->id == 'customer_processing_order' ) {
// Get an instance of the WC_Order object
$order = $email->object;
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Empty string
$theSku = '';
foreach ( $order->get_items() as $item_id => $item ) {
// Get an instance of corresponding the WC_Product object
$product = $item->get_product();
// Get product SKU
$product_sku = $product->get_sku();
$theSku .= $product_sku . ',';
}
$tSku = substr( $theSku, 0, -1 );
echo '<script type="application/json+trustpilot">
{
"recipientName": "' . $order->get_billing_first_name() . '",
"recipientEmail": "' . $order->get_billing_email() . '",
"referenceId": "' . $order->get_id() . '",
"productSkus": ["' . $tSku . '"]
}
</script>';
}
}
}
add_action( 'woocommerce_email_header', 'action_woocommerce_email_header', 10, 2 );
Note: the results can be found in the email source, one email client displays the results visually, the other does not, so it depends on which email client you use

Related

Add order items in customer notes field

I am trying to add ordered products (quantity per product per line) in customer added notes field for new WooCommerce orders. I introduced the following code in my functions.php but it was not working.
/* Add ordered products to the customer notes field on order creation. */
add_action( ‘woocommerce_new_order’, ‘products_in_customer_notes’, 10, 1 );
function products_in_customer_notes( $order_id ) {
$order = wc_get_order( $order_id );
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$customer_note .= "<br>";
$customer_note .= $quantity . 'x ' . $product_name;
}
// Add the note
$order->set_customer_note($customer_note);
// $order->add_order_note($customer_note);
// Save the data
$order->save();
}
}
When that didn't work, I tried to simplify my code by hardcoding a comment instead, but that didn't work either.
/* Add ordered products to the customer notes field on order creation. */
add_action( ‘woocommerce_new_order’, ‘products_in_customer_notes’, 10, 1 );
function products_in_customer_notes( $order_id ) {
$order = wc_get_order( $order_id );
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
$customer_note .= '<br>';
$customer_note .= 'Hardcoded Test';
// foreach ( $order->get_items() as $item_id => $item ) {
// $product_name = $item->get_name();
// $quantity = $item->get_quantity();
// $customer_note .= "<br>";
// $customer_note .= $quantity . 'x ' . $product_name;
// }
// Add the note
$order->set_customer_note($customer_note);
// $order->add_order_note($customer_note);
// Save the data
$order->save();
}
}
What is it that I am missing that is preventing this from working?
The desired output would be something like the following:
Any customer notes if they were inserted at the time of order placement.
1x product A
3x product B
8x product C
etc.
add_action( 'woocommerce_new_order', 'products_in_customer_notes', 10, 2 );
function products_in_customer_notes( $order_id, $order ) {
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$customer_note .= "<br>";
$customer_note .= $quantity . 'x ' . $product_name;
}
// Add the note
$order->set_customer_note($customer_note);
// Save the data
$order->save();
}
}
You were missing the parameter count(2), $order_id, $order

Pass variable between woocommerce hooks

I'm trying to save the URL of a gravity forms entry to the metadata of the associated woocommerce order. I passed the entry ID to the checkout page via a query string and am able to access the data from the woocommerce_checkout_fields hook but not woocommerce_checkout_update_order_meta. So my solution was to store the query value in a global variable to pass to other functions. But from every function besides woocommerce_checkout_fields the global variable returns 'empty' even though it is returning a value inside woocommerce_checkout_fields. How would I pass the value of $entry to other functions?
/* REGISTER VARIABLE FROM URL QUERY STRING */
function entry($qvar) {
$qvar[] = "entry";
return $qvar;
}
add_filter('query_vars', 'entry');
$entry_ID = '';
function display_entry($fields) {
/* GET DATA FROM ENTRY ID PASSED VIA QUERY STRING*/
$entry = get_query_var('entry', 'empty');
$GLOBALS['entry_id'] = $entry;
if ( is_page('checkout') && $entry != "empty") {
$entries = GFAPI::get_entry($GLOBALS['entry']);
$first_name = $entries["4.3"];
$last_name = $entries["4.6"];
$email = $entries["5"];
$phone = $entries["6"];
/* POPULATE CHECKOUT FIELDS */
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ){
$product_id = $cart_item['product_id'];
if ($product_id == 646) {
$fields['billing']['billing_first_name']['default'] = $first_name;
$fields['billing']['billing_last_name']['default'] = $last_name;
$fields['billing']['billing_email']['default'] = $email;
$fields['billing']['billing_phone']['default'] = $phone;
}
}
}
return $fields;
}
add_filter('woocommerce_checkout_fields', display_entry);
/* SAVE ENTRY URL AS ORDER METADATA */
add_action( 'woocommerce_checkout_update_order_meta', 'save_order_meta' );
function save_order_meta( $order_id ) {
add_post_meta( $order_id, 'entry_url', 'https://jmgmediagroup.com/wp-admin/admin.php?page=gf_entries&view=entry&id=3&lid='.$GLOBALS['entry_id'] );
}
/* SHOW ENTRY URL IN WC ORDER ADMIN AREA */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_entry_url_in_admin_order_meta', 10, 1 );
function display_entry_url_in_admin_order_meta( $order ) {
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__('Entry URL', 'woocommerce').':</strong> ' . get_post_meta( $order_id, 'entry_url', true ) . '</p>';
}
/* SHOW ENTRY URL IN EMAIL NOTIFICATIONS*/
add_action('woocommerce_email_customer_details','add_verification_id_to_emails_notifications', 15, 4 );
function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$output = '';
$url = get_post_meta( $order_id, 'entry_url', true );
if ( !empty($billing_vid) )
$output .= '<div><strong>' . __( "Entry URL:", "woocommerce" ) . '</strong> <span class="text">' . $url . '</span></div>';
echo $output;
}

Show stock quantity per order item in a custom column on WooCommerce admin orders list

I'm trying to show stock remaining per product in a custom column on WooCommerce admin orders list, without success.
I would like to show it next to 'quantity'.
Ej. Product A x 1 (3)
My code attempt:
add_filter('manage_edit-shop_order_columns', 'new_order_items_column' );
function new_order_items_column( $order_columns ) {
$order_columns['order_products'] = "Productos";
return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'new_order_items_column_cnt' );
function new_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
echo '▪️ ' . $order_item['name'] .' × '. $order_item['quantity'] .'<br />';
}
}
}
}
Any advice?
You can use get_stock_quantity(), note that products with variations and products that do not contain stock are also taken into account
So you get:
// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
// Add new column
$columns['order_products'] = __( 'Products', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'order_products' ) {
// Get an instance of the WC_Order object from an Order ID
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
foreach( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Get product
$product = wc_get_product( $product_id );
// Get stock quantity
$get_stock_quantity = $product->get_stock_quantity();
// NOT empty
if ( ! empty ( $get_stock_quantity ) ) {
$stock_output = ' (' . $get_stock_quantity . ')';
} else {
$stock_output = '';
}
// Output
echo '▪ '. $item->get_name() . ' × ' . $item->get_quantity() . $stock_output . '<br />';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

Replace Product SKU Based on product Title in Order Details Page

I am trying to append text to the SKU when a sample is ordered.
I have tried the following code to modify the title,
function cart_title($title, $values, $cart_item_key){
if ($values['sample']){
$title .= ' [' . __('Sample','woosample') . '] ';
}
return $title;
}
How do I modify this code to change the SKU?
note: woocommerce_cart_item_name will not display in the backend order
view
function cart_title( $title, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$product_sku = $product->get_sku();
// optional
if ( empty( $product_sku ) ) {
$product_sku = 'not found!';
}
$title = $title . ' + ' . $product_sku;
return $title;
}
add_filter('woocommerce_cart_item_name', 'cart_title', 10, 3 );

Woocommerce Order Customer and Payment Details from Order

I've got $order = new WCOrder( $orderid ); in a woocommerce_payment_complete_order_status Filter. I can get at a bunch of stuff about the order from there, but I'd like to get from $order to the Customer's phone and email address as displayed on the Edit Order page of the Admin interface. I'd also like to get the transaction fee (e.g. how much Stripe or PayPal charged me for the transaction) if it's available. How do you get from $order to those values though - I lose the trail in the quickly Woocommerce docs. ? Here's my code so far:
public function my_hook_function( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
$num_items = count( $order->get_items() );
$ret = array();
$ret["billing_address"] = $order->get_formatted_billing_address();
$ret["shipping_address"] = $order->get_formatted_shipping_address();
$ret["cart_discount"] = $order->get_cart_discount();
$ret["cart_tax"] = $order->get_cart_tax();
$ret["order_notes"] = $order->get_customer_order_notes();
$ret["items"] = array();
if ( $num_items > 0 ) {
foreach( $order->get_items() as $item ) {
if ( 'line_item' == $item['type'] ) {
$item_data = array();
$item_data['name'] = $item['name'];
$item_data['quantity'] = $item['item_meta']['_qty'][0];
$item_data['product_id'] = $item['item_meta']['_product_id'][0];
$item_data['subtotal'] = $item['item_meta']['_line_subtotal'][0];
$ret["items"][] = $item_data;
}
}
}
// do stuff with $ret
return $order_status;
}
Following code snippet appears in order details template:
if ( $order->billing_email ) echo '<dt>' . __( 'Email:', 'woocommerce' ) . '</dt><dd>' . $order->billing_email . '</dd>';
if ( $order->billing_phone ) echo '<dt>' . __( 'Telephone:', 'woocommerce' ) . '</dt><dd>' . $order->billing_phone . '</dd>';
These properties of WC_Abstract_Order class are not obvious because they are hidden behind magic methods __set and __get.

Categories