Access our premium support and let us know your problems, we will help you solve them.

0
No products in the cart.

Forum Replies Created

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • in reply to: Woocommerce which hook to use for order status changes #9452
    blankruvee
    Participant

    "I need to catch every time an order status changes from wc_on_hold to wc_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 and console.log in this hook. You would need to either use the die function or the error_log function to log it to your debug.log file.


    1- Using die 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();
    
        die($order_total);
    }
    

    2- Using error_log 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();
    
        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 your wp-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:

    blankruvee
    Participant

    1- 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:

    enter image description here

    And on the order review table on the check out page:

    enter image description here


    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:

    enter image description here

    And in the order details table on the thank you page:

    enter image description here


    This answer has been fully tested on woocommerce 5.7.1 and works.

    blankruvee
    Participant

    You 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 use global $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!

    in reply to: WooCommerce how to attach custom invoice to Email #9468
    blankruvee
    Participant

    I 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;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)