In the Woocommerce Shipment Tracking plugin they use:
<?php echo esc_html( $tracking_item['tracking_number'] ); ?>
to get the shipping tracking number. How can use something similar directly in a Woocommerce email template?
Here is what I came up with and it works!
<?php
$order_id = $order->get_order_number();
$tracking_items = get_post_meta( $order_id, '_wc_shipment_tracking_items', true );
foreach ( $tracking_items as $tracking_item ){
echo esc_html( $tracking_item['tracking_number'] );
}?>
Updated version for WooCommerce 3.0+:
$order = new WC_Order( $queue_item->order_id );
$tracking_items = $order->get_meta( '_wc_shipment_tracking_items', true );
if ( count( $tracking_items ) > 0 ) {
foreach ( $tracking_items as $tracking_item ) {
$tracker = esc_html( $tracking_item['tracking_number'] );
}
}
Related
Can someone help me how can i fix this code
instead of showing the country it shows the author
can you guys tell me what i did wrong on this code?
add_action( 'woocommerce_after_shop_loop_item_title','sold_by' );
function sold_by(){
?>
<?php
global $product;
$seller = get_post_field( 'post_author', $product->get_id());
$author = get_user_by( 'id', $seller );
$store_info = dokan_get_store_info( $author->ID );
$address = dokan_get_store_info( $author->address );
?>
<span class="details">
<?php printf( 'Origin: %s', $author->display_name, $address ); ?>
</span>
<?php
}
See this screenshot showing origin as author instead of country:
Try the following :
add_action( 'woocommerce_after_shop_loop_item_title','dokan_store_country' );
function dokan_store_country(){
global $product;
$seller_id = get_post_field( 'post_author', $product->get_id());
$seller = new WP_User( $seller_id );
$countries = WC()->countries->get_countries();
$store_data = dokan_get_store_info( seller_id );
if ( isset( $store_data['country'] ) ) {
$store_country = $store_data['country']; // Try to get seller store country
} elseif( isset( $countries[$seller->billing_country] ) ) {
$store_country = $countries[$seller->billing_country]; // Try to get seller billing country
} else {
return; // Exit
}
echo '<span class="details">' . sprintf( __('Origin: %s'), $store_country ) . '</span>';
}
Untested, it could work.
I tried searching online and contacting the plugin author, and he said that it can be retrieved using the WordPress get post meta.
I'm using a plugin called woo-gst to add a product attribute called 'prod_hsn_id' which add a filed called HSN code at product edit page, I'm also using a pdf invoice plugin called woocommerce pdf invoice to generate pdf invoice. Now I want to display the HSN code on the invoice.
<?php foreach ( $this->order->get_items( 'line_item' ) as $item_id => $item ) {
$product = $this->order->get_product_from_item( $item ); ?>
<tr class="product-row">
<td>
<?php echo esc_html( $item['name'] );
global $wpdb;
$hidden_order_itemmeta = apply_filters( 'woocommerce_hidden_order_itemmeta', array(
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'_wc_cog_item_cost',
'_wc_cog_item_total_cost',
'_reduced_stock',
) );
$hidden_order_itemmeta = apply_filters( 'bewpi_hidden_order_itemmeta', $hidden_order_itemmeta );
foreach ( $this->order->has_meta( $item_id ) as $meta ) {
// Skip hidden core fields.
if ( in_array( $meta['meta_key'], $hidden_order_itemmeta, true ) ) {
continue;
}
// Skip serialised meta.
if ( is_serialized( $meta['meta_value'] ) ) {
continue;
}
// Get attribute data.
if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) {
$term = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_key'] = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value'];
} else {
$meta['meta_key'] = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta['meta_key'], $product ), $meta['meta_key'] );
}
echo '<div class="item-attribute"><span style="font-weight: bold;">' . wp_kses_post( rawurldecode( $meta['meta_key'] ) ) . ': </span>' . wp_kses_post( rawurldecode( $meta['meta_value'] ) ) . '</div>';
}
$field_name = 'hsn_prod_id';
// then loop through items in order and print each custom field
foreach ( $this->order->get_items() as $item_id => $item ) {
if ( $product = $this->order->get_product_from_item( $item ) ) {
$location = $product->get_meta( $field_name );
if ( !empty($hsn_prod_id) ) {
echo '<div class="product-location">HSN Code: '.$hsn_prod_id.'</div>';
}
}
}
?>
</td>```
above is the code In the Invoice Template file I'm trying to display the HSN Code.
I was working on the same today, did not find a solution, and finally here is what I did and it worked well.
We need to pull the data from postmeta table, and to get that you need to have the post id for the product.
If you do $product_id = $product->get_id() this will return the id (they call it Variation ID). The post id and product id are not same.
To get the post id for the product you need to subtract -1 form the $product_id.
This is the line you can use to get the HSN code :
$hsn_prod_id = get_post_meta( $product->get_id()-1, 'hsn_prod_id', true );
UPDATE:
The logic for subtract -1 depends on the Wordpress,WooCommerce version may be. I have tried the same in a fresh setup where it didn't work. In there, I just used this $product_id = $product->get_id() value and it worked.
You can figure it by looking at the id for the product. The id it shows in the UI and the ID you see in the URL when you edit an order. If both are not the same you need to see the sequence difference and take a call.
I am trying to change/add in the title of the "Order Recieved" Woocommerce page.
The below snippet works - I am able to change the pre-existing TEXT with the following code:
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = $str . ' We have emailed the purchase receipt to you.';
return $new_str;
}
The below snippet does not work. - I am unable to change/add the TITLE and also pass in the username to personalise it. Here is the code and also an image of the output I am trying to achieve....The "You are awesome FIRSTNAME" added in.
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
This should be possible as there are examples on how to do it, such as here...I just can't figure out why the main title wont even appear?
As a workaround I inspected the CSS and changed the text below the header to be a larger size and the font family required.
Then through the below PHP I created the custom text with the customer name in the header.
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = sprintf( "You are awesome, %s :) - We've recieved your order.", esc_html( $order->get_billing_first_name() ) );
return $new_str;
}
2022 update:
function ps_title_order_received( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
unset( $order );
}
}
if ( isset ( $order ) ) {
$title = sprintf( "Thank you, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
add_filter( 'the_title', 'ps_title_order_received', 10, 2 );
I'm trying to add a custom field jckwds_date as an order note. I can't for the life of me figure out why this code isn't working in functions.php?
The code also only allows the note to be added in the are a certain role type.
function wdm_my_custom_notes_on_single_order_page($order){
$user = wp_get_current_user();
$allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');
if( array_intersect($allowed_roles, $user->roles ) ) {
$value = get_post_meta( $product->get_id(), 'jckwds_date', true );
echo $value;
$order->add_order_note( $value, $is_customer_note = 1 );
}
}
Basically I need THIS:
To be added HERE:
Update:
The following code will add from the order custom field 'jckwds_date' (or checkout posted field value 'jckwds_date') an order note that will appear in backend for defined user roles:
add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 );
function product_custom_field_to_custom_order_notes( $order_id, $data ){
// HERE define allowed user roles
$allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer');
$user_id = get_post_meta( '_customer_user', 'jckwds_date', true );
$user = new WP_User( $user_id );
// Exit if no matched user roles
if( ! array_intersect( $allowed_roles, $user->roles ) ) return;
// Get the date custom field (or checkout field)
if( get_post_meta( $order_id, 'jckwds_date', true ) ){
$note = get_post_meta( $order_id, 'jckwds_date', true );
} elseif( isset( $_POST['jckwds_date'] ) ){
$note = sanitize_text_field( $_POST['jckwds_date'] );
}
// The order note
if( isset($note) && ! empty($note) ){
$order = wc_get_order( $order_id ); // The WC_Order Object
$order->add_order_note( $note ); // Add the note
$order->save(); // Save the order
}
}
Code goes in function.php file of the active child theme (or active theme). It should work.
add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_add_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
$fields['order']['order_note']['priority'] = 5;
$fields['order']['order_note'] = array(
'label' => __('Order Notes', 'woocommerce'),
'placeholder' => _x('Order Notes', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
Try this
$order = wc_get_order( $order_id );
$note = __("This my custom note...");
$order->add_order_note( $note );
$order->save();
Try this
add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta');
function checkout_field_update_order_meta($order_id)
{
if (!empty($_POST['field_name'])) {
update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name']));
}
}
Try this code.
add_action( 'woocommerce_thankyou', 'my_note_custom' );
function my_note_custom( $order_id ) {
$order = new WC_Order( $order_id );
$note = __("This my custom note...");
$order->add_order_note( $note );
$order->save();
}
Found out it was a simple as changing the $product to $order as it is the order custom field value I'm trying to retrieve.
Full code below:
function wdm_my_custom_notes_on_single_order_page($order){
$user = wp_get_current_user();
$allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');
if( array_intersect($allowed_roles, $user->roles ) ) {
$value = get_post_meta( $order->get_id(), 'jckwds_date', true );
$note = '<b>Wholesale</b>';
echo $value;
$order->add_order_note( $value, $is_customer_note = 1 );
}
}
How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
$chosen_titles = array();
$available_methods = $wp->shipping->get_packages();
$chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'Delivery price on request', $chosen_titles ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
}
return $title;
}
is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )…
Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".
Try this instead:
add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
if ( empty($order_id) || $order_id == 0 )
return $title; // Exit
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key )
return $title; // Exit
$method_title_names = array();
// Loop through Order shipping items data and get the method title
foreach ($order->get_items('shipping') as $shipping_method )
$method_title_names[] = $shipping_method->get_name();
if( in_array( 'Delivery price on request', $method_title_names ) ) {
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Similar: Adding custom message on Thank You page by shipping method in Woocommerce