Forum Replies Created
- AuthorPosts
-
February 6, 2022 at 4:31 am in reply to: Woocommerce which hook to use for order status changes #9452
ruvee
Participant"I need to catch every time an order status changes from
wc_on_hold
towc_completed
"You would need to change your hook to
woocommerce_order_status_changed
."I couldn’t get on Chrome’s Console that echo giving me the order price"
You can’t use
javascript
andconsole.log
in this hook. You would need to either use thedie
function or theerror_log
function to log it to yourdebug.log
file.
1- Using
die
functionadd_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3); function so_status_completed($order_id, $old_status, $new_status) { $order = wc_get_order($order_id); //$order_total = $order->get_formatted_order_total(); $order_total = $order->get_total(); die($order_total); }
2- Using
error_log
functionadd_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3); function so_status_completed($order_id, $old_status, $new_status) { $order = wc_get_order($order_id); //$order_total = $order->get_formatted_order_total(); $order_total = $order->get_total(); error_log(print_r('order total: ' . $order_total, true)); }
Note:
- In order for this to work, you would need to activate "debugging" mode in your
wp-config.php
file. So, navigate to yourwp-config.php
file and add the following lines to it!
(NOT RECOMMENDED IN THE PRODUCTION)
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
3- Creating a simple logger using
file_put_contents
function!add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3); function so_status_completed($order_id, $old_status, $new_status) { $order = wc_get_order($order_id); //$order_total = $order->get_formatted_order_total(); $order_total = $order->get_total(); // Simple logger file_put_contents(dirname(__FILE__) . '/my_logger.txt', $order_total); }
- If "my_logger.txt" does not exist, it’ll be created. If it already exists, then it’ll be overwritten!
You could use
"append"
mode with your logger, if you need to! So your logger file won’t get overwritten.add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3); function so_status_completed($order_id, $old_status, $new_status) { $order = wc_get_order($order_id); //$order_total = $order->get_formatted_order_total(); $order_total = $order->get_total(); // Logger with "append" mode file_put_contents(dirname(__FILE__) . '/my_logger.txt', $order_total, FILE_APPEND); }
Second question
"since I am just here, I want to ask you if
$order-get_total()
is the right one to retrieve order amount without fee applied?"You might want to take a look at the
$order->get_subtotal()
for the totals before shipping, coupons and taxes:Please see these related answers:
January 4, 2022 at 4:10 am in reply to: How to display ACF field after the product name on the cart and order reviews in WooCommerce? #9477ruvee
Participant1- On the cart page AND on the order review table on the checkout page
If you would need to run it both on the cart page AND order review table on the checkout page, you could use
woocommerce_cart_item_name
filter hook, like this:add_filter('woocommerce_cart_item_name', 'order_review_custom_field', 999, 3); function order_review_custom_field($product_name, $cart_item, $cart_item_key) { $address = get_field('location', $cart_item['product_id']); return ($address) ? $product_name . '<div>Address: ' . $address . '</div>' : $product_name . '<div>Address: No address found!</div>'; }
Here’s the result on the cart page:
And on the order review table on the check out page:
2- On the email AND in the order details table on the thank you page:
We could use
woocommerce_order_item_meta_end
action hook to append the custom field value to the end of product name on the email template:add_action("woocommerce_order_item_meta_end", "email_order_custom_field", 999, 4); function email_order_custom_field($item_id, $item, $order, $plain_text) { $address = get_field('location', $item->get_product_id()); echo ($address) ? '<div>Address: ' . $address . '</div>' : '<div>Address: No address found!</div>'; };
And here’s the result on the email:
And in the order details table on the thank you page:
This answer has been fully tested on woocommerce
5.7.1
and works.September 6, 2021 at 12:29 pm in reply to: Woocommerce how to set default_checkout_billing_country only if user is not logged in #9464ruvee
ParticipantYou could use
is_user_logged_in
:add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' ); function bbloomer_change_default_checkout_country($default) { if(is_user_logged_in()){ return $default; }else{ return 'US'; }; };
However, sometimes, because of caching issues,
is_user_logged_in
doesn’t work. In that case then you could useglobal $current_user
.add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' ); function bbloomer_change_default_checkout_country($default) { global $current_user; if($current_user->ID){ return $default; }else{ return 'US'; }; };
Let me know if you could get it to work!
ruvee
ParticipantI would first check whether the field is empty or not, if it is then just return:
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3); function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) { $allowed_statuses = array( 'customer_completed_order' ); $pdf_name = get_post_meta( get_the_id(), 'email_fatura', true ); if ( empty($pdf_name) ){ return; } if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) { $pdf_name = get_post_meta( get_the_id(), 'email_fatura', true ); $pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name; $attachments[] = $pdf_path; } return $attachments; }
- In order for this to work, you would need to activate "debugging" mode in your
- AuthorPosts