vicatcu
vicatcu

Reputation: 5837

Woocommerce Order Customer and Payment Details from Order

I've got $order = new WCOrder( $orderid ); in a woocommerce_payment_complete_order_status Filter. I can get at a bunch of stuff about the order from there, but I'd like to get from $order to the Customer's phone and email address as displayed on the Edit Order page of the Admin interface. I'd also like to get the transaction fee (e.g. how much Stripe or PayPal charged me for the transaction) if it's available. How do you get from $order to those values though - I lose the trail in the quickly Woocommerce docs. ? Here's my code so far:

  public function my_hook_function( $order_status, $order_id ) {
    $order = new WC_Order( $order_id );
    $num_items = count( $order->get_items() );

    $ret = array();
    $ret["billing_address"] = $order->get_formatted_billing_address();
    $ret["shipping_address"] = $order->get_formatted_shipping_address();
    $ret["cart_discount"] = $order->get_cart_discount();
    $ret["cart_tax"] = $order->get_cart_tax();
    $ret["order_notes"] = $order->get_customer_order_notes();

    $ret["items"] = array();
    if ( $num_items > 0 ) {
      foreach( $order->get_items() as $item ) {
        if ( 'line_item' == $item['type'] ) {
          $item_data = array();
          $item_data['name'] = $item['name'];
          $item_data['quantity'] = $item['item_meta']['_qty'][0];
          $item_data['product_id'] = $item['item_meta']['_product_id'][0];
          $item_data['subtotal'] = $item['item_meta']['_line_subtotal'][0];
          $ret["items"][] = $item_data;
        }
      }
    }

    // do stuff with $ret

    return $order_status;
  }

Upvotes: 1

Views: 776

Answers (1)

Raivis Rengelis
Raivis Rengelis

Reputation: 121

Following code snippet appears in order details template:

if ( $order->billing_email ) echo '<dt>' . __( 'Email:', 'woocommerce' ) . '</dt><dd>' . $order->billing_email . '</dd>';
if ( $order->billing_phone ) echo '<dt>' . __( 'Telephone:', 'woocommerce' ) . '</dt><dd>' . $order->billing_phone . '</dd>';

These properties of WC_Abstract_Order class are not obvious because they are hidden behind magic methods __set and __get.

Upvotes: 1

Related Questions