get_taxes(); foreach ( (array) ( $taxes['total'] ?? array() ) as $rate_id => $amount ) { if ( '' === $amount || null === $amount ) { continue; } $out[] = array( 'title' => WC_Tax::get_rate_label( $rate_id ), 'rate' => round( (float) WC_Tax::get_rate_percent_value( $rate_id ) / 100, 4 ), 'price' => self::money( $amount ), ); } return $out; } private static function address( $order, $type ) { $get = function ( $field ) use ( $order, $type ) { $fn = "get_{$type}_{$field}"; return method_exists( $order, $fn ) ? (string) $order->$fn() : ''; }; $country = strtoupper( $get( 'country' ) ); if ( '' === $country ) { return null; } return array( 'name' => trim( $get( 'first_name' ) . ' ' . $get( 'last_name' ) ), 'company' => $get( 'company' ), 'address1' => $get( 'address_1' ), 'address2' => $get( 'address_2' ), 'city' => $get( 'city' ), 'zip' => $get( 'postcode' ), 'country_code' => $country, ); } /** @param WC_Order $order */ public static function order( $order ) { $lines = array(); foreach ( $order->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { $is_fee = $item->is_type( 'fee' ); $qty = $is_fee ? 1 : max( 1, (int) $item->get_quantity() ); $total = (float) $item->get_total(); // Unit price before discounts (excl. tax); the discount makes the line add up to the exact total. $before = $is_fee ? $total : (float) $item->get_subtotal(); $unit = round( $before / $qty, 2 ); $disc = round( $unit * $qty - $total, 2 ); $product = $is_fee ? null : $item->get_product(); $lines[] = array( 'id' => (int) $item_id, 'title' => $item->get_name(), 'sku' => $product ? $product->get_sku() : null, 'quantity' => $qty, 'price' => self::money( $unit ), 'requires_shipping' => $product ? $product->needs_shipping() : false, 'taxable' => 'none' !== ( $is_fee ? $item->get_tax_status() : ( $product ? $product->get_tax_status() : 'taxable' ) ), 'tax_lines' => self::tax_lines( $item ), 'discount_allocations' => abs( $disc ) >= 0.01 ? array( array( 'amount' => self::money( $disc ) ) ) : array(), ); } $shipping = array(); foreach ( $order->get_items( 'shipping' ) as $item ) { $shipping[] = array( 'title' => $item->get_name(), 'price' => self::money( $item->get_total() ), 'tax_lines' => self::tax_lines( $item ), ); } $vat = (string) $order->get_meta( self::VAT_META ); $created = $order->get_date_created(); $paid = $order->get_date_paid(); return array( 'id' => (int) $order->get_id(), 'name' => '#' . $order->get_order_number(), 'currency' => $order->get_currency(), 'taxes_included' => false, 'created_at' => $created ? $created->format( DATE_ATOM ) : gmdate( DATE_ATOM ), 'processed_at' => $paid ? $paid->format( DATE_ATOM ) : null, 'email' => $order->get_billing_email(), 'financial_status' => 'paid', 'billing_address' => self::address( $order, 'billing' ), 'shipping_address' => self::address( $order, 'shipping' ), 'line_items' => $lines, 'shipping_lines' => $shipping, 'note_attributes' => '' !== $vat ? array( array( 'name' => 'VAT number', 'value' => $vat ) ) : array(), 'total_price' => self::money( $order->get_total() ), 'total_tax' => self::money( $order->get_total_tax() ), ); } /** @param WC_Order_Refund $refund */ public static function refund( $refund ) { $items = array(); foreach ( $refund->get_items() as $item ) { $original = (int) $item->get_meta( '_refunded_item_id' ); $qty = abs( (int) $item->get_quantity() ); if ( $original && $qty > 0 ) { $items[] = array( 'line_item_id' => $original, 'quantity' => $qty ); } } $created = $refund->get_date_created(); return array( 'id' => (int) $refund->get_id(), 'order_id' => (int) $refund->get_parent_id(), 'created_at' => $created ? $created->format( DATE_ATOM ) : gmdate( DATE_ATOM ), 'refund_line_items' => $items, 'transactions' => array( array( 'kind' => 'refund', 'status' => 'success', 'amount' => self::money( abs( (float) $refund->get_amount() ) ), ), ), ); } }