In WooCommerce on this website, I have 2 Local Pickup shipping methods:
AM pickup: shipping_method_0_local_pickup31
PM pickup: shipping_method_0_local_pickup32
Unfortunately this shipping method does not show up on Admin Order edit pages
Is is possible to use:
add_action('woocommerce_admin_order_data_after_shipping_address','cwn_add_pickup_to_order_item_meta', 1, 2);
function cwn_add_pickup_to_order_item_meta($shipping_method) {
echo "<h4>Pickup Time</h4>";
echo '<p><strong>' . __('AM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup31', true) . '</p>';
echo '<p><strong>' . __('PM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup32', true) . '</p>';
}
There is 2 similar ways to get the Shipping methods data in the WC_Order object.
1) Using WC_Abstract_Order get_shipping_methods() method.
2) Using WC_Abstract_Order get_items( 'shipping' ) method.
This will output an array of WC_Order_Item_Shipping objects. So you will need a foreach loop to use WC_Order_Item_Shipping methods to get the data, this way (Where $order is an instance of the WC_Order object):
foreach($order->get_items( 'shipping' ) as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
//Or:
foreach($order->get_shipping_methods() as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
In your code, the $shipping_method hooked function argument is wrong as it should be $order (an instance of WC_Order object.
So now you can use it in your hooked function, this way for example:
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'cwn_add_pickup_to_order_item_meta', 10, 1 );
function cwn_add_pickup_to_order_item_meta( $order ){
echo '<div>';
foreach($order->get_shipping_methods() as $shipping_method ){
echo '<p><strong>Shipping Method ID:</strong> '. $shipping_method->get_method_id().'<br>
<strong>Shipping Method name:</strong> '. $shipping_method->get_name().'<br>
<strong>Shipping Method title:</strong> '. $shipping_method->get_method_title().'<br>
<strong>Shipping Method Total:</strong> '. $shipping_method->get_total().'<br>
<strong>Shipping Method Total tax:</strong> '. $shipping_method->get_total_tax().'</p><br>';
}
echo '</div>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file
Tested and works on WooCommerce 3+
I was looking for a way to execute a command when finalizing a purchase and the shipping method selected was different from "LOCAL PICKUP".
Your last code gave me the light I needed.
My code worked.
Follow my code to help whoever needs it:
function showpopup($order_id){
$order = wc_get_order($order_id);
$order->get_shipping_methods();
if( ! $order->has_shipping_method('local_pickup') ) {
echo '
<script>
function myFunction() {
alert("Retirada no local NÃO SELECIONADA!");
}
myFunction()
</script>
';
}
}
add_action( 'woocommerce_thankyou', 'showpopup', 10, 1 );
Thank you.
Related
We use this code to display the used coupons on a order. This is working fine! But now we want to show this information also in the order quick view.
I found this hook here: woocommerce_admin_order_preview_end
So i tried to change the hook from the code below with this hook. But then the quick view function does not work at all. When we click on the "eye" to open the quick view - nothing happend. Do we have to adjust the code more or what is here the problem?
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
/**
* Add used coupons to the order edit page
*
*/
function custom_checkout_field_display_admin_order_meta($order){
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
echo '<p><strong>' . __('Coupons used') . ':</strong> ';
$i = 1;
foreach( $order->get_used_coupons() as $coupon) {
echo $coupon;
if( $i < $coupons_count )
echo ', ';
$i++;
}
echo '</p>';
}
}
Try to use the following to display coupon used in admin order quick view (preview):
// Add custom order data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_data', 10, 2 );
function admin_order_preview_add_custom_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $coupons = $order->get_used_coupons() ) {
$data['coupons_count'] = count($coupons); // <= Store the count in the data array.
$data['coupons_codes'] = implode(', ', $coupons); // <= Store the count in the data array.
}
return $data;
}
// Display The data in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div><strong>' . __('Coupons used') . ' ({{data.coupons_count}})<strong>: {{data.coupons_codes}}</div><br>';
}
Code goes in functions.php file of the active child theme (or active theme). Untested it should works.
Based on: Display custom data on Woocommerce admin order preview
Here is what I have and it works great for 1 shipping class, but want to have separate messages for different shipping classes:
// Display a custom text under cart item name in cart page
add_filter( 'woocommerce_cart_item_name', 'custom_text_cart_item_name', 10, 3 );
function custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// Here below define your shipping class slug
$shipping_class = '5-gallon';
if( is_cart() && $cart_item['data']->get_shipping_class() === $shipping_class ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
}
return $item_name; }
For multiple shipping classes with different messages you can use the following:
// Display a custom text under cart item name in cart page
add_filter( 'woocommerce_cart_item_name', 'custom_text_cart_item_name', 10, 3 );
function custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// Only on cart page
if( ! is_cart() )
return $item_name;
// Here below define your shipping classes slugs (one by line)
$shipping_class_1 = '5-gallon';
$shipping_class_2 = '10-gallon';
$shipping_class = $cart_item['data']->get_shipping_class();
// First shipping class
if( $shipping_class === $shipping_class_1 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
}
// 2nd shipping class
elseif( $shipping_class === $shipping_class_2 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Some different message…", "woocommerce") . '</div>';
}
return $item_name;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
In Woocommerce, I have a checkout page (review order) with custom data.
I need to find a hook to register some custom data ($totaleiva_1 and $totalefinitocarrello) in the order and then I have to send them in email new order.
I'm not able to make it for instance. Any advice or help please?
Edit - That is my code:
$totaleiva_1 = 0;
$items = $woocommerce->cart->get_cart();
foreach($items as $item ) {
$totaleiva_1 += $totalForSebeneArray[$item ['data']->get_id()];
}
$totaleiva_1 = number_format($totaleiva_1, 2, '.', '');
$totalefinitocarrello = $totaleiva_1 + $total; echo "€";
echo $totalefinitocarrello;
You will need to add and display 2 hidden fields with the 2 desired values (to be able to get and save them as custom order meta data on submission).
Also your code is a bit outdated and can be simplified.
Here is your revisited code:
$totaleiva_1 = 0;
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ) {
$totale_iva_1 += $totalForSebeneArray[$cart_item['data']->get_id()];
}
$totale_finito_carrello = wc_price( $totale_iva_1 + $total );
echo $totale_finito_carrello;
// Display 2 hidden input fields
echo '<input type="hidden" id="totaleiva1" name="totaleiva1" value="'.$totale_iva_1.'">
<input type="hidden" id="tfcarrello" name="tfcarrello" value="'.$totale_finito_carrello.'">';
Then you will save that data as custom order meta data using:
add_action('woocommerce_checkout_create_order', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( $order, $data ) {
if( isset($_POST['totaleiva1']) && ! empty($_POST['totaleiva1']) ) {
$order->update_meta_data( '_totale_iva_1', esc_attr( $_POST['totaleiva1'] ) );
}
if( isset($_POST['tfcarrello']) && ! empty($_POST['tfcarrello']) ) {
$order->update_meta_data( '_totale_fin_carrello', esc_attr( $_POST['tfcarrello'] ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Now to get that data and use it anywhere:
1) from the order ID variable, use:
$totaleiva_1 = get_post_meta( $order_id, '_totale_iva_1', true );
$totalefinitocarrello = get_post_meta( $order_id, '_totale_fin_carrello', true );
2 from the WC_Order Object:
$totaleiva_1 = $order->get_meta('_totale_iva_1');
$totalefinitocarrello = $order->get_meta('_totale_fin_carrello');
To display those in Woocommerce new order notification, ou will use:
add_action( 'woocommerce_email_order_details', 'email_order_details_action_callback', 5, 4 );
function email_order_details_action_callback( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id === 'new_order' ) {
if ( $tiva1 = $order->get_meta('_totale_iva_1') ) {
echo '<p>'. __("Totale IVA") . ': ' . $tiva1 . '</p>';
}
if ( $tfcarr = $order->get_meta('_totale_fin_carrello') ) {
echo '<p>'. __("Totale finito carrello") . ': ' . $tfcarr . '</p>';
}
}
}
Display the fields in Admin order pages:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'admin_order_after_billing_address_callback', 10, 1 );
function admin_order_after_billing_address_callback( $order ){
if ( $tiva1 = $order->get_meta('_totale_iva_1') ) {
echo '<p>'. __("Totale IVA") . ': ' . $tiva1 . '</p>';
}
if ( $tfcarr = $order->get_meta('_totale_fin_carrello') ) {
echo '<p>'. __("Totale finito carrello") . ': ' . $tfcarr . '</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Now I think that all your IVA custom calculations can be handled by Woocommerce using available settings possibilities, as you are making things Much more complicated everywhere.
I am trying to add the shipping class under each product to the new order emails for both admin and customers in WooCommerce. This is my first time posting a question so please forgive me if there are formatting issues.
I used the code found here: https://wordpress.stackexchange.com/questions/291637/woocommerce-add-shipping-class-below-each-product-in-shopping-cart-page
That adds the shipping class below each product in the cart but I want to be able to show it on the order emails also.
I am just unsure of which objects to call to grab the data for each product in the new order email.
Here's the code I'm using:
add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text, $item_name ){
$product = $cart_item['data']; // Get the WC_Product object instance
$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
if( empty( $shipping_class_id ) )
return $item_name; // Return default product title (in case of)
$label = __( 'Shipping class', 'woocommerce' );
return $item_name . '<br>
<p class="item-shipping_class" style="margin:12px 0 0;">
<strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
}
I expected the shipping class to be listed below each product in the "New Order" email but currently adding this code returns an internal server error upon checkout.
The following will display The product shipping class name in Woocommerce "New Order" Email notification:
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
$GLOBALS['email_id_str'] = $email->id;
}
// Display Items shipping class name in New Order email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
// Targeting email notifications only
if( is_wc_endpoint_url() ) return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $shipping_class_id = $product->get_shipping_class_id() ){
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
// Only for New Order email notification
if( ! empty($email_id) && 'new_order' === $email_id ) {
$shipping_class_name = get_term( $shipping_class_id, 'product_shipping_class' )->name;
$item_name .= '<br><p class="item-shipping_class" style="margin:12px 0 0;">
<strong>' . __( 'Shipping class', 'woocommerce' ) . ': </strong>' . $shipping_class_name . '</p>';
}
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (Like udemy).
When the customer checks out, I would like to send an API request and enroll the user for that particular course. I have tried several WooCommerce hooks but none worked for me.
This is the code that I'm using:
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student($order_id)
{
echo $order_id;
echo "Hooked";
}
I am writing this code for a plugin and to make it easier, I am currently using Cash on Delivery method.
Can anyone point me out where I am going wrong because when I checkout I cant see the message "hooked" that I am printing nor the $order_id?
It takes me to the success page and doesn't show these two things that I am printing.
Update 2 Only For Woocommerce 3+ (added restriction to execute the code only once)
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Allow code execution only once
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get the order key
$order_key = $order->get_order_key();
// Get the order number
$order_key = $order->get_order_number();
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object
$product = $item->get_product();
// Get the product Id
$product_id = $product->get_id();
// Get the product name
$product_id = $item->get_name();
}
// Output some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
// Flag the action as done (to avoid repetitions on reload for example)
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related thread:
Get Order items and WC_Order_Item_Product in WooCommerce 3
How to get WooCommerce order details
The code is tested and works.
Updated (to get the product Id from Orders items as asked in your comment)
May be you could use woocommerce_thankyou hook instead, that will display on order-received page your echoed code, this way:
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works.
Then you can use all class WC_Abstract_Order methods on the $order object.
Related:
How to get WooCommerce order details
Get Order items and WC_Order_Item_Product in WooCommerce 3
How to get Customer details from Order in WooCommerce?
Rather than 'woocommerce_thankyou' hook, 'woocommerce_checkout_order_processed' hook is the relevant hook. 'woocommerce_checkout_order_processed' hook will be called only once and you will not need to add meta for each product and make additional calls to keep check for code to run only once. As, 'woocommerce_thankyou' can be called multiple times that is each time thankyou page loads.
Replace add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
with
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
and remove meta code and checks. Updated code is
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
you can get the order items of an order by
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}