Getting the data from order details is quite easy in WooCommerce, you just have to add a few lines of code which can fetch you the data from the WooCommerce tables.
7 Best Website Builder In 2020 (Compared)
Below is the sample code which you can check it.
// Get an instance of the WC_Order object $order = wc_get_order($order_id); $order_data = array( 'order_id' => $order->get_id(), 'order_number' => $order->get_order_number(), 'order_date' => date('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)), 'status' => $order->get_status(), 'shipping_total' => $order->get_total_shipping(), 'shipping_tax_total' => wc_format_decimal($order->get_shipping_tax(), 2), 'fee_total' => wc_format_decimal($fee_total, 2), 'fee_tax_total' => wc_format_decimal($fee_tax_total, 2), 'tax_total' => wc_format_decimal($order->get_total_tax(), 2), 'cart_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_cart_discount(), 2), 'order_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_order_discount(), 2), 'discount_total' => wc_format_decimal($order->get_total_discount(), 2), 'order_total' => wc_format_decimal($order->get_total(), 2), 'order_currency' => $order->get_currency(), 'payment_method' => $order->get_payment_method(), 'shipping_method' => $order->get_shipping_method(), 'customer_id' => $order->get_user_id(), 'customer_user' => $order->get_user_id(), 'customer_email' => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : '', 'billing_first_name' => $order->get_billing_first_name(), 'billing_last_name' => $order->get_billing_last_name(), 'billing_company' => $order->get_billing_company(), 'billing_email' => $order->get_billing_email(), 'billing_phone' => $order->get_billing_phone(), 'billing_address_1' => $order->get_billing_address_1(), 'billing_address_2' => $order->get_billing_address_2(), 'billing_postcode' => $order->get_billing_postcode(), 'billing_city' => $order->get_billing_city(), 'billing_state' => $order->get_billing_state(), 'billing_country' => $order->get_billing_country(), 'shipping_first_name' => $order->get_shipping_first_name(), 'shipping_last_name' => $order->get_shipping_last_name(), 'shipping_company' => $order->get_shipping_company(), 'shipping_address_1' => $order->get_shipping_address_1(), 'shipping_address_2' => $order->get_shipping_address_2(), 'shipping_postcode' => $order->get_shipping_postcode(), 'shipping_city' => $order->get_shipping_city(), 'shipping_state' => $order->get_shipping_state(), 'shipping_country' => $order->get_shipping_country(), 'customer_note' => $order->get_customer_note(), 'download_permissions' => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
$line_items_shipping = $order->get_items('shipping'); foreach ($line_items_shipping as $item_id => $item) { if (is_object($item)) { if ($meta_data = $item->get_formatted_meta_data('')) : foreach ($meta_data as $meta_id => $meta) : if (in_array($meta->key, $line_items_shipping)) { continue; } // html entity decode is not working preoperly $shipping_items[] = implode('|', array('item:' . wp_kses_post($meta->display_key), 'value:' . str_replace('×', 'X', strip_tags($meta->display_value)))); endforeach; endif; } } //get fee and total $fee_total = 0; $fee_tax_total = 0; foreach ($order->get_fees() as $fee_id => $fee) { $fee_items[] = implode('|', array( 'name:' . html_entity_decode($fee['name'], ENT_NOQUOTES, 'UTF-8'), 'total:' . wc_format_decimal($fee['line_total'], 2), 'tax:' . wc_format_decimal($fee['line_tax'], 2), )); $fee_total += $fee['line_total']; $fee_tax_total += $fee['line_tax']; } // get tax items foreach ($order->get_tax_totals() as $tax_code => $tax) { $tax_items[] = implode('|', array( 'rate_id:'.$tax->id, 'code:' . $tax_code, 'total:' . wc_format_decimal($tax->amount, 2), 'label:'.$tax->label, 'tax_rate_compound:'.$tax->is_compound, )); } // add coupons foreach ($order->get_items('coupon') as $_ => $coupon_item) { $coupon = new WC_Coupon($coupon_item['name']); $coupon_post = get_post((WC()->version < '2.7.0') ? $coupon->id : $coupon->get_id()); $discount_amount = !empty($coupon_item['discount_amount']) ? $coupon_item['discount_amount'] : 0; $coupon_items[] = implode('|', array( 'code:' . $coupon_item['name'], 'description:' . ( is_object($coupon_post) ? $coupon_post->post_excerpt : '' ), 'amount:' . wc_format_decimal($discount_amount, 2), )); } foreach ($order->get_refunds() as $refunded_items){ $refund_items[] = implode('|', array( 'amount:' . $refunded_items->get_amount(), 'reason:' . $refunded_items->get_reason(), 'date:'. date('Y-m-d H-i-s',strtotime((WC()->version < '2.7.0') ? $refunded_items->date_created : $refunded_items->get_date_created())), )); }
Comments