skip to Main Content

Does anyone know how to get an order id for customizing email templates via hooks?

I’m working with a custom API that returns a bill based on order id, and I’m also trying to print it in an email template.

This is what I have tried so far:

add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
    echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip( $order->get_id() ) . '</pre>';
}, 10, 4 );

In that way, I’m not getting the bill. But if I hardcode the order id, then everything is working.

EDIT

When I dump the $order I got the complete object:

["items":protected]=>
  array(5) {
    ["line_items"]=>
    array(3) {
      [118]=>
      object(WC_Order_Item_Product)#2019 (11) {
        ["extra_data":protected]=>
        array(9) {
          ["product_id"]=>
          int(0)
          ["variation_id"]=>
          int(0)
          ["quantity"]=>
          int(1)
          ["tax_class"]=>
          string(0) ""
          ["subtotal"]=>
          int(0)
          ["subtotal_tax"]=>
          int(0)
          ["total"]=>
          int(0)
          ["total_tax"]=>
          int(0)
          ["taxes"]=>
          array(2) {
            ["subtotal"]=>
            array(0) {
            }
            ["total"]=>
            array(0) {
            }
          }
        }
        ["data":protected]=>
        array(11) {
          ["order_id"]=>
          int(55)
          ["name"]=>
          string(9) "Product 1"
          ["product_id"]=>
          int(11)
          ["variation_id"]=>
          int(0)
          ["quantity"]=>
          int(1)
          ["tax_class"]=>
          string(0) ""
          ["subtotal"]=>
          string(1) "0"
          ["subtotal_tax"]=>
          string(1) "0"
          ["total"]=>
          string(1) "0"
          ["total_tax"]=>
          string(1) "0"
          ["taxes"]=>
          array(2) {
            ["total"]=>
            array(0) {
            }
            ["subtotal"]=>
            array(0) {
            }
          }
        }
        ["cache_group":protected]=>
        string(11) "order-items"
        ["meta_type":protected]=>
        string(10) "order_item"
        ["object_type":protected]=>
        string(10) "order_item"
        ["id":protected]=>
        int(118)
        ["changes":protected]=>
        array(0) {
        }
        ["object_read":protected]=>
        bool(true)
        ["default_data":protected]=>
        array(11) {

But when I try to access it in the following way:

add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
    echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip( $order[order_id] ) . '</pre>';
}, 10, 4 );

I got an Internal Server Error.

Thanks

3

Answers


  1. As CBroe mentioned, verify what order contains.

    I remember that when I was working with WooCommerce $order->get_id() was the post’s id and that I had to use $order->get_order_number()

    Login or Signup to reply.
  2. Try this,

    add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data(); // The Order data
    $order_id = $order_data['id'];
        echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip($order_id ) . '</pre>';
    }, 10, 4 );
    
    Login or Signup to reply.
  3. The API in question probably requires the order number and not the post id. Change

     FS4E_VSDC::fs4e_get_order_slip( $order->get_id() ) 
    

    to

    FS4E_VSDC::fs4e_get_order_slip( $order->get_order_number() ) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search