Woocommerce Order Customer and Payment Details from Order - php

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.

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

Hook JS into WooCommerce Email function

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

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

Passing custom data from cart items to Order meta in Woocommerce 3

I have implemented a custom HTML Form and asking for some data which my customers will pass to place order successfully. Without these details my order has no importance.
For HTML form, I am referencing some custom PHP script which is below and which processes POST data from the Form and creates Cart with these data programmatically. Thanks #LoicTheAztec to help me achieve this.
The script.php file code:
<?php
require_once("../wp-load.php");
$customer_name = $_POST["customer_name"];
$customer_email = $_POST["customer_email"];
$customer_sex = $_POST["customer_sex"];
$customer_age = $_POST["customer_age"];
$product_id = $_POST["product_id"];
$custom_data = array(); // Initializing
if( isset($_POST['customer_name']) && ! empty($_POST['customer_name']) )
$custom_data['custom_data']['name'] = $_POST['customer_name'];
if( isset($_POST['customer_email']) && ! empty($_POST['customer_email']) )
$custom_data['custom_data']['email'] = $_POST['customer_email'];
if( isset($_POST['customer_sex']) && ! empty($_POST['customer_sex']) )
$custom_data['custom_data']['sex'] = $_POST['customer_sex'];
if( isset($_POST['customer_age']) && ! empty($_POST['customer_age']) )
$custom_data['custom_data']['age'] = $_POST['customer_age'];
global $woocommerce;
if (WC()->cart->add_to_cart( $product_id, '1', '0', array(), $custom_data )) {
var_dump($product_id);
} else {
var_dump($customer_name);
}
header("Location: ./checkout");
?>
As you see we have programmatically created a cart item using WC_Cart add_to_cart() method. So, all custom data is being saved to Cart as custom cart item data.
And now, we have also placed a code block into functions.php to print these data on Checkout page.
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
//var_dump($checkout);
global $woocommerce;
echo '<div id="my_custom_checkout_field"><h2>' . __('Child Info') . '</h2>';
foreach ( $woocommerce->cart->get_cart() as $cart_item ) {
if( isset($cart_item['custom_data']) ) {
$custom_data = $cart_item['custom_data'];
echo("<div>Name: <strong>" . $custom_data['name'] . "</strong></div>");
echo("<div>Email: <strong>" . $custom_data['email'] . "</strong></div>");
echo("<div>Gender: <strong>" . $custom_data['sex'] . "</strong></div>");
echo("<div>Age: <strong>" . $custom_data['age'] . "</strong></div>");
}
}
echo '</div>';
}
Now, I am trying to add these data printed on Checkout page to the Order page as well. As my order can't be completed without these data, user need to fill up these data and when he creates order, these data needs to be passed to the Order Summary page as well. And admin also needs to be able to see these data so he can process the order.
I hope this description clears everything and thanks again #LoicTheAztec to make me able to do this. Thank you very much.
Update 2 - Two steps
1) Saving data:
We will save this custom customer data as "Hidden" order "item" meta data and then as order meta data too as this is related to subscriptions with a unique order item:
// Utility function: array of custom customer key/label pairs
function custom_data_keys_labels(){
return array(
'name' => __('Customer name'), 'email' => __('Customer email'),
'sex' => __('Customer gender'), 'age' => __('Customer age'),
);
}
// Add/save custom field value as custom HIDDEN order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( ! isset( $values['custom_data'] ) )
return;
$custom_data = $values['custom_data'];
$meta_data = array();
$labels_keys = custom_data_keys_labels();
foreach( $labels_keys as $key => $label ){
if ( isset( $custom_data[$key] ) )
$meta_data[$key] = $custom_data[$key];
}
if ( sizeof( $meta_data ) > 0 )
$item->update_meta_data( __('_customer_data'), $meta_data );
return $cart_item_data;
}
// Add/save custom fields values as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 20, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
$order_items = $order->get_items(); // Order itesm
$item = reset($order_items); // Keep only the first order item
$item_data = $item->get_meta( '_customer_data' ); // Get custom customer data
if( is_array($item_data) && sizeof($item_data) > 0 ){
foreach( $item_data as $key => $value ) {
if ( isset( $item_data[$key] ) )
$order->update_meta_data( '_customer_' . $key, $value );
}
// Mark as data saved
$order->update_meta_data( '_customer_data_set', true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) Displaying saved custom data:
The code below also use our utility function custom_data_keys_labels()…
// Order pages (frontend and admin) display
add_filter( 'woocommerce_order_details_after_order_table' , 'display_admin_order_meta_cutom_data', 20, 1 ); // Front
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta_cutom_data', 20, 1 ); // Admin
function display_admin_order_meta_cutom_data( $order ){
$labels_keys = custom_data_keys_labels();
if( $order->get_meta( '_customer_data_set' ) ){
if( is_admin() ){ // Admin
echo '<p>';
foreach( $labels_keys as $key => $label ){
if ( $order->get_meta( '_customer_' . $key ) )
echo '<strong>' . $label . ':</strong> ' . $order->get_meta( '_customer_' . $key ) . '<br>';
}
echo '</p>';
}
else { // Front end: order view and Order received (thankyou)
echo '<table class="woocommerce-table"><tbody>';
foreach( $labels_keys as $key => $label ){
if ( $order->get_meta( '_customer_' . $key ) )
echo '<tr><th>' . $label . ':</th><td>' . $order->get_meta( '_customer_' . $key ) . '</td></tr>';
}
echo '</tbody></table>';
}
}
}
// Email notifications display
add_filter( 'woocommerce_email_order_meta_fields' , 'display_email_cutom_data', 20, 3 );
function display_email_cutom_data ( $fields, $sent_to_admin, $order ) {
$labels_keys = custom_data_keys_labels();
if( $order->get_meta( '_customer_data_set' ) ){
foreach( $labels_keys as $key => $label ){
$fields['customer_' . $key] = array(
'label' => $label,
'value' => $order->get_meta( '_customer_' . $key ),
);
}
}
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

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