array(
'pa_size' => array(
'label' => 'Size',
'value' => 'Medium',
)
)
public get_formatted ( string $hideprefix = '_' ) : array | ||
$hideprefix | string | exclude meta when key is prefixed with this, defaults to `_` |
Результат | array |
/** * Sort the data for CSV output first * * @param int $product_id * @param array $headers * @param array $body * @param array $items */ public static function output_csv($product_id, $headers, $body, $items) { $headers['quantity'] = __('Quantity', 'wcvendors'); $new_body = array(); foreach ($body as $i => $order) { // Remove comments unset($body[$i]['comments']); // Remove all numeric keys in each order (these are the meta values we are redoing into new lines) foreach ($order as $key => $col) { if (is_int($key)) { unset($order[$key]); } } // New order row $new_row = $body[$i]; // Remove order to redo unset($body[$i]); foreach ($items[$i]['items'] as $item) { $item_meta = new WC_Order_Item_Meta($item['item_meta']); $item_meta_options = $item_meta->get_formatted(); // $item_meta = $item_meta->display( true, true ); $new_row_with_meta = $new_row; if (sizeof($item_meta_options) > 0) { $new_row_with_meta[] = $item['qty']; foreach ($item_meta_options as $item_meta_option) { if (!array_key_exists($item_meta_option['label'], $headers)) { $headers[$item_meta_option['label']] = $item_meta_option['label']; } $new_row_with_meta[] = $item_meta_option['value']; } } else { $new_row_with_meta[] = $item['qty']; } $new_body[] = $new_row_with_meta; } } $headers = apply_filters('wcvendors_csv_headers', $headers, $product_id, $items); $body = apply_filters('wcvendors_csv_body', $new_body, $product_id, $items); WCV_Export_CSV::download($headers, $body, $product_id); }
/** * Prepare a single order output for response. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ public function prepare_item_for_response($post, $request) { global $wpdb; $order = wc_get_order($post); $dp = $request['dp']; $data = array('id' => $order->id, 'parent_id' => $post->post_parent, 'status' => $order->get_status(), 'order_key' => $order->order_key, 'currency' => $order->get_order_currency(), 'version' => $order->order_version, 'prices_include_tax' => $order->prices_include_tax, 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'customer_id' => $order->get_user_id(), 'discount_total' => wc_format_decimal($order->get_total_discount(), $dp), 'discount_tax' => wc_format_decimal($order->cart_discount_tax, $dp), 'shipping_total' => wc_format_decimal($order->get_total_shipping(), $dp), 'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp), 'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp), 'total' => wc_format_decimal($order->get_total(), $dp), 'total_tax' => wc_format_decimal($order->get_total_tax(), $dp), 'billing' => array(), 'shipping' => array(), 'payment_method' => $order->payment_method, 'payment_method_title' => $order->payment_method_title, 'transaction_id' => $order->get_transaction_id(), 'customer_ip_address' => $order->customer_ip_address, 'customer_user_agent' => $order->customer_user_agent, 'created_via' => $order->created_via, 'customer_note' => $order->customer_note, 'date_completed' => wc_rest_prepare_date_response($order->completed_date), 'date_paid' => $order->paid_date, 'cart_hash' => $order->cart_hash, 'line_items' => array(), 'tax_lines' => array(), 'shipping_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array(), 'refunds' => array()); // Add addresses. $data['billing'] = $order->get_address('billing'); $data['shipping'] = $order->get_address('shipping'); // Add line items. foreach ($order->get_items() as $item_id => $item) { $product = $order->get_product_from_item($item); $product_id = 0; $variation_id = 0; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = $product->id; $variation_id = $product->variation_id; $product_sku = $product->get_sku(); } $meta = new WC_Order_Item_Meta($item, $product); $item_meta = array(); $hideprefix = 'true' === $request['all_item_meta'] ? null : '_'; foreach ($meta->get_formatted($hideprefix) as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $formatted_meta['key'], 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } $line_item = array('id' => $item_id, 'name' => $item['name'], 'sku' => $product_sku, 'product_id' => (int) $product_id, 'variation_id' => (int) $variation_id, 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : '', 'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp), 'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'taxes' => array(), 'meta' => $item_meta); $item_line_taxes = maybe_unserialize($item['line_tax_data']); if (isset($item_line_taxes['total'])) { $line_tax = array(); foreach ($item_line_taxes['total'] as $tax_rate_id => $tax) { $line_tax[$tax_rate_id] = array('id' => $tax_rate_id, 'total' => $tax, 'subtotal' => ''); } foreach ($item_line_taxes['subtotal'] as $tax_rate_id => $tax) { $line_tax[$tax_rate_id]['subtotal'] = $tax; } $line_item['taxes'] = array_values($line_tax); } $data['line_items'][] = $line_item; } // Add taxes. foreach ($order->get_items('tax') as $key => $tax) { $tax_line = array('id' => $key, 'rate_code' => $tax['name'], 'rate_id' => $tax['rate_id'], 'label' => isset($tax['label']) ? $tax['label'] : $tax['name'], 'compound' => (bool) $tax['compound'], 'tax_total' => wc_format_decimal($tax['tax_amount'], $dp), 'shipping_tax_total' => wc_format_decimal($tax['shipping_tax_amount'], $dp)); $data['tax_lines'][] = $tax_line; } // Add shipping. foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) { $shipping_line = array('id' => $shipping_item_id, 'method_title' => $shipping_item['name'], 'method_id' => $shipping_item['method_id'], 'total' => wc_format_decimal($shipping_item['cost'], $dp), 'total_tax' => wc_format_decimal('', $dp), 'taxes' => array()); $shipping_taxes = maybe_unserialize($shipping_item['taxes']); if (!empty($shipping_taxes)) { $shipping_line['total_tax'] = wc_format_decimal(array_sum($shipping_taxes), $dp); foreach ($shipping_taxes as $tax_rate_id => $tax) { $shipping_line['taxes'][] = array('id' => $tax_rate_id, 'total' => $tax); } } $data['shipping_lines'][] = $shipping_line; } // Add fees. foreach ($order->get_fees() as $fee_item_id => $fee_item) { $fee_line = array('id' => $fee_item_id, 'name' => $fee_item['name'], 'tax_class' => !empty($fee_item['tax_class']) ? $fee_item['tax_class'] : '', 'tax_status' => 'taxable', 'total' => wc_format_decimal($order->get_line_total($fee_item), $dp), 'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), $dp), 'taxes' => array()); $fee_line_taxes = maybe_unserialize($fee_item['line_tax_data']); if (isset($fee_line_taxes['total'])) { $fee_tax = array(); foreach ($fee_line_taxes['total'] as $tax_rate_id => $tax) { $fee_tax[$tax_rate_id] = array('id' => $tax_rate_id, 'total' => $tax, 'subtotal' => ''); } foreach ($fee_line_taxes['subtotal'] as $tax_rate_id => $tax) { $fee_tax[$tax_rate_id]['subtotal'] = $tax; } $fee_line['taxes'] = array_values($fee_tax); } $data['fee_lines'][] = $fee_line; } // Add coupons. foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) { $coupon_line = array('id' => $coupon_item_id, 'code' => $coupon_item['name'], 'discount' => wc_format_decimal($coupon_item['discount_amount'], $dp), 'discount_tax' => wc_format_decimal($coupon_item['discount_amount_tax'], $dp)); $data['coupon_lines'][] = $coupon_line; } // Add refunds. foreach ($order->get_refunds() as $refund) { $data['refunds'][] = array('id' => $refund->id, 'refund' => $refund->get_refund_reason() ? $refund->get_refund_reason() : '', 'total' => '-' . wc_format_decimal($refund->get_refund_amount(), $dp)); } $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($order)); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); }
/** * Get the order data for the given ID. * * @since 2.5.0 * @param WC_Order $order The order instance * @return array */ protected function get_order_data($order) { $order_post = get_post($order->id); $dp = wc_get_price_decimals(); $order_data = array('id' => $order->id, 'order_number' => $order->get_order_number(), 'created_at' => $this->format_datetime($order_post->post_date_gmt), 'updated_at' => $this->format_datetime($order_post->post_modified_gmt), 'completed_at' => $this->format_datetime($order->completed_date, true), 'status' => $order->get_status(), 'currency' => $order->get_order_currency(), 'total' => wc_format_decimal($order->get_total(), $dp), 'subtotal' => wc_format_decimal($order->get_subtotal(), $dp), 'total_line_items_quantity' => $order->get_item_count(), 'total_tax' => wc_format_decimal($order->get_total_tax(), $dp), 'total_shipping' => wc_format_decimal($order->get_total_shipping(), $dp), 'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp), 'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp), 'total_discount' => wc_format_decimal($order->get_total_discount(), $dp), 'shipping_methods' => $order->get_shipping_method(), 'payment_details' => array('method_id' => $order->payment_method, 'method_title' => $order->payment_method_title, 'paid' => isset($order->paid_date)), 'billing_address' => array('first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address_1' => $order->billing_address_1, 'address_2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'postcode' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'phone' => $order->billing_phone), 'shipping_address' => array('first_name' => $order->shipping_first_name, 'last_name' => $order->shipping_last_name, 'company' => $order->shipping_company, 'address_1' => $order->shipping_address_1, 'address_2' => $order->shipping_address_2, 'city' => $order->shipping_city, 'state' => $order->shipping_state, 'postcode' => $order->shipping_postcode, 'country' => $order->shipping_country), 'note' => $order->customer_note, 'customer_ip' => $order->customer_ip_address, 'customer_user_agent' => $order->customer_user_agent, 'customer_id' => $order->get_user_id(), 'view_order_url' => $order->get_view_order_url(), 'line_items' => array(), 'shipping_lines' => array(), 'tax_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array()); // add line items foreach ($order->get_items() as $item_id => $item) { $product = $order->get_product_from_item($item); $product_id = null; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = isset($product->variation_id) ? $product->variation_id : $product->id; $product_sku = $product->get_sku(); } $meta = new WC_Order_Item_Meta($item, $product); $item_meta = array(); foreach ($meta->get_formatted(null) as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $meta_key, 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } $order_data['line_items'][] = array('id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp), 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : null, 'name' => $item['name'], 'product_id' => $product_id, 'sku' => $product_sku, 'meta' => $item_meta); } // Add shipping. foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) { $order_data['shipping_lines'][] = array('id' => $shipping_item_id, 'method_id' => $shipping_item['method_id'], 'method_title' => $shipping_item['name'], 'total' => wc_format_decimal($shipping_item['cost'], $dp)); } // Add taxes. foreach ($order->get_tax_totals() as $tax_code => $tax) { $order_data['tax_lines'][] = array('id' => $tax->id, 'rate_id' => $tax->rate_id, 'code' => $tax_code, 'title' => $tax->label, 'total' => wc_format_decimal($tax->amount, $dp), 'compound' => (bool) $tax->is_compound); } // Add fees. foreach ($order->get_fees() as $fee_item_id => $fee_item) { $order_data['fee_lines'][] = array('id' => $fee_item_id, 'title' => $fee_item['name'], 'tax_class' => !empty($fee_item['tax_class']) ? $fee_item['tax_class'] : null, 'total' => wc_format_decimal($order->get_line_total($fee_item), $dp), 'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), $dp)); } // Add coupons. foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) { $order_data['coupon_lines'][] = array('id' => $coupon_item_id, 'code' => $coupon_item['name'], 'amount' => wc_format_decimal($coupon_item['discount_amount'], $dp)); } $order_data = apply_filters('woocommerce_cli_order_data', $order_data); return $this->flatten_array($order_data); }
/** * Get an order refund for the given order ID and ID * * @since 2.2 * @param string $order_id order ID * @param string|null $fields fields to limit response to * @return array */ public function get_order_refund($order_id, $id, $fields = null) { try { // Validate order ID $order_id = $this->validate_request($order_id, $this->post_type, 'read'); if (is_wp_error($order_id)) { return $order_id; } $id = absint($id); if (empty($id)) { throw new WC_API_Exception('woocommerce_api_invalid_order_refund_id', __('Invalid order refund ID', 'woocommerce'), 400); } $order = wc_get_order($id); $refund = wc_get_order($id); if (!$refund) { throw new WC_API_Exception('woocommerce_api_invalid_order_refund_id', __('An order refund with the provided ID could not be found', 'woocommerce'), 404); } $line_items = array(); // Add line items foreach ($refund->get_items('line_item') as $item_id => $item) { $product = $order->get_product_from_item($item); $meta = new WC_Order_Item_Meta($item, $product); $item_meta = array(); foreach ($meta->get_formatted() as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $meta_key, 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } $line_items[] = array('id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item), 2), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], 2), 'total' => wc_format_decimal($order->get_line_total($item), 2), 'total_tax' => wc_format_decimal($order->get_line_tax($item), 2), 'price' => wc_format_decimal($order->get_item_total($item), 2), 'quantity' => (int) $item['qty'], 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : null, 'name' => $item['name'], 'product_id' => isset($product->variation_id) ? $product->variation_id : $product->id, 'sku' => is_object($product) ? $product->get_sku() : null, 'meta' => $item_meta, 'refunded_item_id' => (int) $item['refunded_item_id']); } $order_refund = array('id' => $refund->id, 'created_at' => $this->server->format_datetime($refund->date), 'amount' => wc_format_decimal($refund->get_refund_amount(), 2), 'reason' => $refund->get_refund_reason(), 'line_items' => $line_items); return array('order_refund' => apply_filters('woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this)); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Get the order data format for a single column per line item, compatible with the CSV Import Suite plugin * * @since 3.0 * @param array $order_data an array of order data for the given order * @param WC_Order $order the WC_Order object * @return array modified order data */ private function get_legacy_import_one_column_per_line_item($order_data, WC_Order $order) { $count = 1; // add line items foreach ($order->get_items() as $_ => $item) { // sku/qty/price $product = $order->get_product_from_item($item); if (!is_object($product)) { $product = new WC_Product(0); } $sku = $product->get_sku(); // note that product ID must be prefixed with `product_id:` so the importer can properly parse it vs. the SKU $product_id = SV_WC_Plugin_Compatibility::product_get_id($product); $line_item = array($sku ? $sku : "product_id:{$product_id}", $item['qty'], $order->get_line_total($item)); // Add item meta $item_meta = new WC_Order_Item_Meta($item); $formatted_meta = $item_meta->get_formatted(); if (!empty($formatted_meta)) { foreach ($formatted_meta as $meta_key => $meta) { // remove newlines $label = str_replace(array("\r", "\r\n", "\n"), '', $meta['label']); $value = str_replace(array("\r", "\r\n", "\n"), '', $meta['value']); // escape reserved chars (:;|) $label = str_replace(array(': ', ':', ';', '|'), array('\\: ', '\\:', '\\;', '\\|'), $meta['label']); $value = str_replace(array(': ', ':', ';', '|'), array('\\: ', '\\:', '\\;', '\\|'), $meta['value']); $line_item[] = wp_kses_post($label . ': ' . $value); } } $order_data["order_item_{$count}"] = implode('|', $line_item); $count++; } $count = 1; foreach ($order->get_items('shipping') as $_ => $shipping_item) { $order_data["shipping_method_{$count}"] = $shipping_item['method_id']; $order_data["shipping_cost_{$count}"] = wc_format_decimal($shipping_item['cost'], 2); $count++; } // fix order numbers $order_data['order_id'] = $order->id; $order_data['order_number_formatted'] = get_post_meta($order->id, '_order_number_formatted', true); $order_data['order_number'] = get_post_meta($order->id, '_order_number', true); // fix customer user $user = new WP_User($order_data['customer_id']); $order_data['customer_id'] = $user->user_email; return $order_data; }
/** * Return an array of formatted item meta in format: * * array( * $meta_key => array( * 'label' => $label, * 'value' => $value * ) * ) * * e.g. * * array( * 'pa_size' => array( * 'label' => 'Size', * 'value' => 'Medium', * ) * ) * * Backports the get_formatted() method to WC 2.1 * * @since 2.2.0 * @see WC_Order_Item_Meta::get_formatted() * @param \WC_Order_Item_Meta $item_meta order item meta class instance * @param string $hide_prefix exclude meta when key is prefixed with this, defaults to `_` * @return array */ public static function get_formatted_item_meta(WC_Order_Item_Meta $item_meta, $hide_prefix = '_') { if (self::is_wc_version_gte_2_2()) { return $item_meta->get_formatted($hide_prefix); } else { if (empty($item_meta->meta)) { return array(); } $formatted_meta = array(); foreach ((array) $item_meta->meta as $meta_key => $meta_values) { if (empty($meta_values) || !is_array($meta_values) || !empty($hide_prefix) && substr($meta_key, 0, 1) == $hide_prefix) { continue; } foreach ($meta_values as $meta_value) { // Skip serialised meta if (is_serialized($meta_value)) { continue; } $attribute_key = urldecode(str_replace('attribute_', '', $meta_key)); // If this is a term slug, get the term's nice name if (taxonomy_exists($attribute_key)) { $term = get_term_by('slug', $meta_value, $attribute_key); if (!is_wp_error($term) && is_object($term) && $term->name) { $meta_value = $term->name; } // If we have a product, and its not a term, try to find its non-sanitized name } elseif ($item_meta->product) { $product_attributes = $item_meta->product->get_attributes(); if (isset($product_attributes[$attribute_key])) { $meta_key = wc_attribute_label($product_attributes[$attribute_key]['name']); } } $formatted_meta[$meta_key] = array('label' => wc_attribute_label($attribute_key), 'value' => apply_filters('woocommerce_order_item_display_meta_value', $meta_value)); } } return $formatted_meta; } }
/** * Display Product Item Name * * Handles to display product item name * * @package WooCommerce - PDF Vouchers * @since 2.2.2 */ public function woo_vou_display_product_item_name($item = array(), $product = array(), $filter_recipient = false) { $prefix = WOO_VOU_META_PREFIX; $product_item_meta = isset($item['item_meta']) ? $item['item_meta'] : array(); $product_item_name = ''; $product_id = isset($product_item_meta['_product_id']) ? $product_item_meta['_product_id'] : ''; $product_recipient_lables = $this->woo_vou_get_product_recipient_meta($product_id); if (!empty($product_item_meta)) { // if not empty product meta // this is added due to skip depricted function get_formatted_legacy from woocommerce if (!defined('DOING_AJAX')) { define('DOING_AJAX', true); } //Item meta object //$item_meta_object = new WC_Order_Item_Meta( $item, $product ); $item_meta_object = new WC_Order_Item_Meta($product_item_meta); //Get product variations $product_variations = $item_meta_object->get_formatted(); //$product_variations = $this->woo_vou_get_formatted_legacy( '_', $item_meta_object ); if ($filter_recipient) { // If you want to hide some of variations using filter $product_variations = apply_filters('woo_vou_hide_recipient_variations', $product_variations, $product_item_meta); } else { // Displaying old order variations //Get recipient name from old orders if (!empty($product_item_meta[$prefix . 'recipient_name']) && !empty($product_item_meta[$prefix . 'recipient_name'][0]) && !is_serialized($product_item_meta[$prefix . 'recipient_name'][0])) { $recipient_name_lbl = $product_recipient_lables['recipient_name_lable']; $product_variations[$recipient_name_lbl] = array('label' => $recipient_name_lbl, 'value' => $product_item_meta[$prefix . 'recipient_name'][0]); } //Get recipient email from old orders if (!empty($product_item_meta[$prefix . 'recipient_email']) && !empty($product_item_meta[$prefix . 'recipient_email'][0]) && !is_serialized($product_item_meta[$prefix . 'recipient_email'][0])) { $recipient_email_lbl = $product_recipient_lables['recipient_email_label']; $product_variations[$recipient_email_lbl] = array('label' => $recipient_email_lbl, 'value' => $product_item_meta[$prefix . 'recipient_email'][0]); } //Get recipient message from old orders if (!empty($product_item_meta[$prefix . 'recipient_message']) && !empty($product_item_meta[$prefix . 'recipient_message'][0]) && !is_serialized($product_item_meta[$prefix . 'recipient_message'][0])) { $recipient_msg_lbl = $product_recipient_lables['recipient_message_label']; $product_variations[$recipient_msg_lbl] = array('label' => $recipient_msg_lbl, 'value' => $product_item_meta[$prefix . 'recipient_message'][0]); } } //Hide variation from item $product_variations = apply_filters('woo_vou_hide_item_variations', $product_variations); // Create variations Html if (!empty($product_variations)) { //variation display format $variation_param_string = apply_filters('woo_vou_variation_name_string_format', '<br /><strong>%1$s</strong>: %2$s', $product_item_meta); foreach ($product_variations as $product_variation) { $product_item_name .= sprintf($variation_param_string, $product_variation['label'], $product_variation['value']); } } } return apply_filters('woo_vou_display_product_item_name', $product_item_name, $product_item_meta, $filter_recipient); }
/** * Prepare a single order refund output for response. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ public function prepare_item_for_response($post, $request) { global $wpdb; $order = wc_get_order((int) $request['order_id']); if (!$order) { return new WP_Error('woocommerce_rest_invalid_order_id', __('Invalid order ID.', 'woocommerce'), 404); } $refund = wc_get_order($post); if (!$refund || intval($refund->post->post_parent) !== intval($order->id)) { return new WP_Error('woocommerce_rest_invalid_order_refund_id', __('Invalid order refund ID.', 'woocommerce'), 404); } $dp = $request['dp']; $data = array('id' => $refund->id, 'date_created' => wc_rest_prepare_date_response($refund->date), 'amount' => wc_format_decimal($refund->get_refund_amount(), $dp), 'reason' => $refund->get_refund_reason(), 'line_items' => array()); // Add line items. foreach ($refund->get_items() as $item_id => $item) { $product = $refund->get_product_from_item($item); $product_id = 0; $variation_id = 0; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = $product->id; $variation_id = $product->variation_id; $product_sku = $product->get_sku(); } $meta = new WC_Order_Item_Meta($item, $product); $item_meta = array(); $hideprefix = 'true' === $request['all_item_meta'] ? null : '_'; foreach ($meta->get_formatted($hideprefix) as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $formatted_meta['key'], 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } $line_item = array('id' => $item_id, 'name' => $item['name'], 'sku' => $product_sku, 'product_id' => (int) $product_id, 'variation_id' => (int) $variation_id, 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : '', 'price' => wc_format_decimal($refund->get_item_total($item, false, false), $dp), 'subtotal' => wc_format_decimal($refund->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($refund->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'taxes' => array(), 'meta' => $item_meta); $item_line_taxes = maybe_unserialize($item['line_tax_data']); if (isset($item_line_taxes['total'])) { $line_tax = array(); foreach ($item_line_taxes['total'] as $tax_rate_id => $tax) { $line_tax[$tax_rate_id] = array('id' => $tax_rate_id, 'total' => $tax, 'subtotal' => ''); } foreach ($item_line_taxes['subtotal'] as $tax_rate_id => $tax) { $line_tax[$tax_rate_id]['subtotal'] = $tax; } $line_item['taxes'] = array_values($line_tax); } $data['line_items'][] = $line_item; } $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($refund)); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); }
/** * Do the request */ public function request() { global $wpdb; $this->validate_input(array("start_date", "end_date")); header('Content-Type: text/xml'); $xml = new DOMDocument('1.0', 'utf-8'); $xml->formatOutput = true; $page = max(1, isset($_GET['page']) ? absint($_GET['page']) : 1); $exported = 0; $tz_offset = get_option('gmt_offset') * 3600; $raw_start_date = wc_clean(urldecode($_GET['start_date'])); $raw_end_date = wc_clean(urldecode($_GET['end_date'])); // Parse start and end date if ($raw_start_date && false === strtotime($raw_start_date)) { $month = substr($raw_start_date, 0, 2); $day = substr($raw_start_date, 2, 2); $year = substr($raw_start_date, 4, 4); $time = substr($raw_start_date, 9, 4); $start_date = gmdate("Y-m-d H:i:s", strtotime($year . '-' . $month . '-' . $day . ' ' . $time)); } else { $start_date = gmdate("Y-m-d H:i:s", strtotime($raw_start_date)); } if ($raw_end_date && false === strtotime($raw_end_date)) { $month = substr($raw_end_date, 0, 2); $day = substr($raw_end_date, 2, 2); $year = substr($raw_end_date, 4, 4); $time = substr($raw_end_date, 9, 4); $end_date = gmdate("Y-m-d H:i:s", strtotime($year . '-' . $month . '-' . $day . ' ' . $time)); } else { $end_date = gmdate("Y-m-d H:i:s", strtotime($raw_end_date)); } $order_ids = $wpdb->get_col($wpdb->prepare("\n\t\t\t\t\tSELECT ID FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'shop_order'\n\t\t\t\t\tAND post_status IN ( '" . implode("','", WC_ShipStation_Integration::$export_statuses) . "' )\n\t\t\t\t\tAND %s <= post_modified_gmt\n\t\t\t\t\tAND post_modified_gmt <= %s\n\t\t\t\t\tORDER BY post_modified_gmt DESC\n\t\t\t\t\tLIMIT %d, %d\n\t\t\t\t", $start_date, $end_date, WC_SHIPSTATION_EXPORT_LIMIT * ($page - 1), WC_SHIPSTATION_EXPORT_LIMIT)); $max_results = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\tSELECT COUNT(ID) FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'shop_order'\n\t\t\t\t\tAND post_status IN ( '" . implode("','", WC_ShipStation_Integration::$export_statuses) . "' )\n\t\t\t\t\tAND %s <= post_modified_gmt\n\t\t\t\t\tAND post_modified_gmt <= %s\n\t\t\t\t", $start_date, $end_date)); $orders_xml = $xml->createElement("Orders"); foreach ($order_ids as $order_id) { if (!apply_filters('woocommerce_shipstation_export_order', true, $order_id)) { continue; } $order = wc_get_order($order_id); $order_xml = $xml->createElement("Order"); $this->xml_append($order_xml, 'OrderNumber', ltrim($order->get_order_number(), '#')); $this->xml_append($order_xml, 'OrderDate', gmdate("m/d/Y H:i", strtotime($order->order_date) - $tz_offset), false); $this->xml_append($order_xml, 'OrderStatus', $order->get_status()); $this->xml_append($order_xml, 'LastModified', gmdate("m/d/Y H:i", strtotime($order->modified_date) - $tz_offset), false); $this->xml_append($order_xml, 'ShippingMethod', implode(' | ', $this->get_shipping_methods($order))); $this->xml_append($order_xml, 'OrderTotal', $order->get_total(), false); $this->xml_append($order_xml, 'TaxAmount', $order->get_total_tax(), false); if (class_exists('WC_COG')) { $this->xml_append($order_xml, 'CostOfGoods', wc_format_decimal($order->wc_cog_order_total_cost), false); } $this->xml_append($order_xml, 'ShippingAmount', $order->get_total_shipping(), false); $this->xml_append($order_xml, 'CustomerNotes', $order->customer_note); $this->xml_append($order_xml, 'InternalNotes', implode(" | ", $this->get_order_notes($order))); // Custom fields - 1 is used for coupon codes $this->xml_append($order_xml, 'CustomField1', implode(" | ", $order->get_used_coupons())); // Custom fields 2 and 3 can be mapped to a custom field via the following filters if ($meta_key = apply_filters('woocommerce_shipstation_export_custom_field_2', '')) { $this->xml_append($order_xml, 'CustomField2', apply_filters('woocommerce_shipstation_export_custom_field_2_value', get_post_meta($order_id, $meta_key, true), $order_id)); } if ($meta_key = apply_filters('woocommerce_shipstation_export_custom_field_3', '')) { $this->xml_append($order_xml, 'CustomField3', apply_filters('woocommerce_shipstation_export_custom_field_3_value', get_post_meta($order_id, $meta_key, true), $order_id)); } // Customer data $customer_xml = $xml->createElement("Customer"); $this->xml_append($customer_xml, 'CustomerCode', $order->billing_email); $billto_xml = $xml->createElement("BillTo"); $this->xml_append($billto_xml, 'Name', $order->billing_first_name . " " . $order->billing_last_name); $this->xml_append($billto_xml, 'Company', $order->billing_company); $this->xml_append($billto_xml, 'Phone', $order->billing_phone); $this->xml_append($billto_xml, 'Email', $order->billing_email); $customer_xml->appendChild($billto_xml); $shipto_xml = $xml->createElement("ShipTo"); if (empty($order->shipping_country)) { $this->xml_append($shipto_xml, 'Name', $order->billing_first_name . " " . $order->billing_last_name); $this->xml_append($shipto_xml, 'Company', $order->billing_company); $this->xml_append($shipto_xml, 'Address1', $order->billing_address_1); $this->xml_append($shipto_xml, 'Address2', $order->billing_address_2); $this->xml_append($shipto_xml, 'City', $order->billing_city); $this->xml_append($shipto_xml, 'State', $order->billing_state); $this->xml_append($shipto_xml, 'PostalCode', $order->billing_postcode); $this->xml_append($shipto_xml, 'Country', $order->billing_country); $this->xml_append($shipto_xml, 'Phone', $order->billing_phone); } else { $this->xml_append($shipto_xml, 'Name', $order->shipping_first_name . " " . $order->shipping_last_name); $this->xml_append($shipto_xml, 'Company', $order->shipping_company); $this->xml_append($shipto_xml, 'Address1', $order->shipping_address_1); $this->xml_append($shipto_xml, 'Address2', $order->shipping_address_2); $this->xml_append($shipto_xml, 'City', $order->shipping_city); $this->xml_append($shipto_xml, 'State', $order->shipping_state); $this->xml_append($shipto_xml, 'PostalCode', $order->shipping_postcode); $this->xml_append($shipto_xml, 'Country', $order->shipping_country); $this->xml_append($shipto_xml, 'Phone', $order->billing_phone); } $customer_xml->appendChild($shipto_xml); $order_xml->appendChild($customer_xml); // Item data $found_item = false; $items_xml = $xml->createElement("Items"); foreach ($order->get_items() as $item_id => $item) { $product = $order->get_product_from_item($item); if (!$product || !$product->needs_shipping()) { continue; } $found_item = true; $item_xml = $xml->createElement("Item"); $image_id = $product->get_image_id(); if ($image_id) { $image_url = current(wp_get_attachment_image_src($image_id, 'shop_thumbnail')); } else { $image_url = ''; } $this->xml_append($item_xml, 'LineItemID', $item_id); $this->xml_append($item_xml, 'SKU', $product->get_sku()); $this->xml_append($item_xml, 'Name', $product->get_title()); $this->xml_append($item_xml, 'ImageUrl', $image_url); $this->xml_append($item_xml, 'Weight', wc_get_weight($product->get_weight(), 'oz'), false); $this->xml_append($item_xml, 'WeightUnits', 'Ounces', false); $this->xml_append($item_xml, 'Quantity', $item['qty'], false); $this->xml_append($item_xml, 'UnitPrice', $order->get_item_subtotal($item, false, true), false); if ($item['item_meta']) { if (version_compare(WC_VERSION, '2.4.0', '<')) { $item_meta = new WC_Order_Item_Meta($item['item_meta']); } else { $item_meta = new WC_Order_Item_Meta($item); } $formatted_meta = $item_meta->get_formatted('_'); if (!empty($formatted_meta)) { $options_xml = $xml->createElement("Options"); foreach ($formatted_meta as $meta_key => $meta) { $option_xml = $xml->createElement("Option"); $this->xml_append($option_xml, 'Name', $meta['label']); $this->xml_append($option_xml, 'Value', $meta['value']); $options_xml->appendChild($option_xml); } $item_xml->appendChild($options_xml); } } $items_xml->appendChild($item_xml); } if (!$found_item) { continue; } // Append cart level discount line if ($order->get_total_discount()) { $item_xml = $xml->createElement("Item"); $this->xml_append($item_xml, 'SKU', "total-discount"); $this->xml_append($item_xml, 'Name', __('Total Discount', 'woocommerce-shipstation')); $this->xml_append($item_xml, 'Adjustment', "true", false); $this->xml_append($item_xml, 'Quantity', 1, false); $this->xml_append($item_xml, 'UnitPrice', $order->get_total_discount() * -1, false); $items_xml->appendChild($item_xml); } // Append items XML $order_xml->appendChild($items_xml); $orders_xml->appendChild($order_xml); $exported++; } $orders_xml->setAttribute("page", $page); $orders_xml->setAttribute("pages", ceil($max_results / WC_SHIPSTATION_EXPORT_LIMIT)); $xml->appendChild($orders_xml); echo $xml->saveXML(); $this->log(sprintf(__("Exported %s orders", 'woocommerce-shipstation'), $exported)); }
/** * Get the order data for a single CSV row * * Note items are keyed according to the column header keys above so these can be modified using * the provider filter without needing to worry about the array order * * @since 3.0 * @param int $order_id the WC_Order ID * @return array order data in the format key => content */ private function get_orders_csv_row($order_id) { $order = wc_get_order($order_id); $is_json = 'import' == $this->order_format; $line_items = $shipping_items = $fee_items = $tax_items = $coupon_items = array(); // get line items foreach ($order->get_items() as $item_id => $item) { $product = $order->get_product_from_item($item); if (!is_object($product)) { $product = new WC_Product(0); } $item_meta = new WC_Order_Item_Meta($item); if ($is_json) { $meta = array(); foreach ($item_meta->get_formatted('_') as $meta_key => $formatted_meta) { $meta[$formatted_meta['key']] = $formatted_meta['value']; } } else { add_filter('woocommerce_attribute_label', array($this, 'escape_reserved_meta_chars')); add_filter('woocommerce_order_item_display_meta_value', array($this, 'escape_reserved_meta_chars')); $meta = $item_meta->display(true, true); remove_filter('woocommerce_attribute_label', array($this, 'escape_reserved_meta_chars')); remove_filter('woocommerce_order_item_display_meta_value', array($this, 'escape_reserved_meta_chars')); if ($meta) { // replace key-value sperator (': ') with our own - equals sign (=) $meta = str_replace(': ', '=', $meta); // remove any newlines generated by WC_Order_Item_Meta::display() $meta = str_replace(array(", \r\n", ", \r", ", \n"), ',', $meta); // re-insert colons and newlines $meta = str_replace(array('[INSERT_COLON_HERE]', '[INSERT_NEWLINE_HERE]'), array(':', "\n"), $meta); } } $product = $order->get_product_from_item($item); $product_id = null; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = isset($product->variation_id) ? $product->variation_id : $product->id; $product_sku = $product->get_sku(); } if ($is_json) { $line_item = array('id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item), 2), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], 2), 'total' => wc_format_decimal($order->get_line_total($item), 2), 'total_tax' => wc_format_decimal($order->get_line_tax($item), 2), 'quantity' => (int) $item['qty'], 'name' => $item['name'], 'product_id' => $product_id, 'sku' => $product_sku, 'meta' => $meta, 'tax_data' => isset($item['line_tax_data']) ? maybe_unserialize($item['line_tax_data']) : ''); } else { $line_item = array('name' => html_entity_decode($item['name'], ENT_NOQUOTES, 'UTF-8'), 'sku' => $product_sku, 'quantity' => (int) $item['qty'], 'total' => wc_format_decimal($order->get_line_total($item), 2), 'refunded' => wc_format_decimal($order->get_total_refunded_for_item($item_id), 2), 'refunded_qty' => $order->get_qty_refunded_for_item($item_id), 'meta' => is_string($meta) ? html_entity_decode($meta, ENT_NOQUOTES, 'UTF-8') : $meta); // add line item tax $line_tax_data = isset($item['line_tax_data']) ? $item['line_tax_data'] : array(); $tax_data = maybe_unserialize($line_tax_data); $line_item['tax'] = isset($tax_data['total']) ? wc_format_decimal(wc_round_tax_total(array_sum((array) $tax_data['total'])), 2) : ''; } /** * CSV Order Export Line Item. * * Filter the individual line item entry for the default export * * @since 3.0.6 * @param array $line_item { * line item data in key => value format * the keys are for convenience and not used for exporting. Make * sure to prefix the values with the desired line item entry name * } * * @param array $item WC order item data * @param WC_Product $product the product * @param WC_Order $order the order * @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance */ $line_item = apply_filters('wc_customer_order_csv_export_order_line_item', $line_item, $item, $product, $order, $this); if (!in_array($this->order_format, array('default_one_row_per_item', 'import')) && is_array($line_item)) { foreach ($line_item as $name => $value) { $name = $this->escape_reserved_item_chars($name); $value = $this->escape_reserved_item_chars($value); $line_item[$name] = $name . ':' . $value; } $line_item = implode('|', $line_item); } if ($line_item) { $line_items[] = $line_item; } } foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) { $shipping_item = array('id' => $shipping_item_id, 'method_id' => $shipping_item['method_id'], 'method_title' => $shipping_item['name'], 'total' => wc_format_decimal($shipping_item['cost'], 2), 'taxes' => isset($shipping_item['taxes']) ? maybe_unserialize($shipping_item['taxes']) : ''); if ($is_json) { $shipping_items[] = $shipping_item; } else { $shipping_items[] = implode('|', array('method:' . $this->escape_reserved_item_chars($shipping_item['method_title']), 'total:' . $shipping_item['total'])); } } // get fee items & total $fee_total = 0; $fee_tax_total = 0; foreach ($order->get_fees() as $fee_item_id => $fee_item) { $fee = array('id' => $fee_item_id, 'title' => $fee_item['name'], 'tax_class' => !empty($fee_item['tax_class']) ? $fee_item['tax_class'] : null, 'total' => wc_format_decimal($order->get_line_total($fee_item), 2), 'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), 2), 'tax_data' => isset($fee_item['line_tax_data']) ? maybe_unserialize($fee_item['line_tax_data']) : ''); if ($is_json) { if (null !== $fee['tax_class']) { $fee['taxable'] = true; } $fee_items[] = $fee; } else { $fee_items[] = implode('|', array('name:' . $this->escape_reserved_item_chars($fee['title']), 'total:' . $fee['total'], 'tax:' . $fee['total_tax'])); } $fee_total += $fee_item['line_total']; $fee_tax_total += $fee_item['line_tax']; } // get tax items foreach ($order->get_tax_totals() as $tax_code => $tax) { $tax_item = array('id' => $tax->id, 'rate_id' => $tax->rate_id, 'code' => $tax_code, 'title' => $tax->label, 'total' => wc_format_decimal($tax->amount, 2), 'compound' => (bool) $tax->is_compound); if ($is_json) { $tax_items[] = $tax_item; } else { $tax_items[] = implode('|', array('code:' . $this->escape_reserved_item_chars($tax_code), 'total:' . $tax->amount)); } } // add coupons foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) { $coupon_item = array('id' => $coupon_item_id, 'code' => $coupon_item['name'], 'amount' => wc_format_decimal($coupon_item['discount_amount'], 2)); if ($is_json) { $coupon_items[] = $coupon_item; } else { $coupon = new WC_Coupon($coupon_item['code']); $coupon_post = get_post($coupon->id); $coupon_items[] = implode('|', array('code:' . $this->escape_reserved_item_chars($coupon_item['code']), 'description:' . $this->escape_reserved_item_chars(is_object($coupon_post) ? $coupon_post->post_excerpt : ''), 'amount:' . $coupon_item['amount'])); } } $order_data = array('order_id' => $order->id, 'order_number' => $order->get_order_number(), 'order_date' => $order->order_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), 'discount_total' => wc_format_decimal($order->get_total_discount(), 2), 'order_total' => wc_format_decimal($order->get_total(), 2), 'refunded_total' => wc_format_decimal($order->get_total_refunded(), 2), 'order_currency' => $order->get_order_currency(), 'payment_method' => $order->payment_method, 'shipping_method' => $order->get_shipping_method(), 'customer_id' => $order->get_user_id(), 'billing_first_name' => $order->billing_first_name, 'billing_last_name' => $order->billing_last_name, 'billing_company' => $order->billing_company, 'billing_email' => $order->billing_email, 'billing_phone' => $order->billing_phone, 'billing_address_1' => $order->billing_address_1, 'billing_address_2' => $order->billing_address_2, 'billing_postcode' => $order->billing_postcode, 'billing_city' => $order->billing_city, 'billing_state' => $order->billing_state, 'billing_country' => $order->billing_country, 'shipping_first_name' => $order->shipping_first_name, 'shipping_last_name' => $order->shipping_last_name, 'shipping_company' => $order->shipping_company, 'shipping_address_1' => $order->shipping_address_1, 'shipping_address_2' => $order->shipping_address_2, 'shipping_postcode' => $order->shipping_postcode, 'shipping_city' => $order->shipping_city, 'shipping_state' => $order->shipping_state, 'shipping_country' => $order->shipping_country, 'customer_note' => $order->customer_note, 'shipping_items' => $is_json && !empty($shipping_items) ? json_encode($shipping_items) : implode(';', $shipping_items), 'fee_items' => $is_json && !empty($fee_items) ? json_encode($fee_items) : implode(';', $fee_items), 'tax_items' => $is_json && !empty($tax_items) ? json_encode($tax_items) : implode(';', $tax_items), 'coupon_items' => $is_json && !empty($coupon_items) ? json_encode($coupon_items) : implode(';', $coupon_items), 'order_notes' => implode('|', $this->get_order_notes($order)), 'download_permissions' => $order->download_permissions_granted ? $order->download_permissions_granted : 0); if ('default_one_row_per_item' === $this->order_format) { $new_order_data = array(); foreach ($line_items as $item) { $order_data['item_name'] = $item['name']; $order_data['item_sku'] = $item['sku']; $order_data['item_quantity'] = $item['quantity']; $order_data['item_tax'] = $item['tax']; $order_data['item_total'] = $item['total']; $order_data['item_refunded'] = $item['refunded']; $order_data['item_refunded_qty'] = $item['refunded_qty']; $order_data['item_meta'] = $item['meta']; /** * CSV Order Export Row for One Row per Item. * * Filter the individual row data for the order export * * @since 3.3.0 * @param array $order_data { * order data in key => value format * to modify the row data, ensure the key matches any of the header keys and set your own value * } * @param array $item * @param \WC_Order $order WC Order object * @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance */ $new_order_data[] = apply_filters('wc_customer_order_csv_export_order_row_one_row_per_item', $order_data, $item, $order, $this); } $order_data = $new_order_data; } else { $order_data['line_items'] = $is_json ? json_encode($line_items) : implode(';', $line_items); } /** * CSV Order Export Row. * * Filter the individual row data for the order export * * @since 3.0 * @param array $order_data { * order data in key => value format * to modify the row data, ensure the key matches any of the header keys and set your own value * } * @param \WC_Order $order WC Order object * @param \WC_Customer_Order_CSV_Export_Generator $this, generator instance */ return apply_filters('wc_customer_order_csv_export_order_row', $order_data, $order, $this); }
/** * Add shipping line meta fields to the shipping line in the order API. */ public static function api_show_shipping_line_meta($order_data, $order, $fields, $server) { if (!empty($order_data['shipping_lines'])) { $shipping_methods = $order->get_shipping_methods(); //mail('*****@*****.**', 'shipping methods', print_r($shipping_methods, true)); foreach ($order_data['shipping_lines'] as $key => $shipping_line) { $shipping_line_id = $shipping_line['id']; $item_meta = array(); if (isset($shipping_methods[$shipping_line_id])) { $meta = new WC_Order_Item_Meta($shipping_methods[$shipping_line_id]); $hideprefix = null; foreach ($meta->get_formatted($hideprefix) as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $meta_key, 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } } $order_data['shipping_lines'][$key]['meta'] = $item_meta; } } return $order_data; }
function __construct($plugin, $order, $cart = null) { global $woocommerce; $secretKey = $plugin->maksuturva_secretkey; $sellerId = $plugin->maksuturva_sellerid; $dueDate = date("d.m.Y"); $id = $plugin->maksuturva_orderid_prefix . self::getMaksuturvaId($order->id); $pid = self::getMaksuturvaId($order->id); $products_rows = array(); $discount_total = 0; $product_total = 0; $shipping_total = 0; foreach ($order->get_items() as $order_item_id => $item) { $product = $order->get_product_from_item($item); $desc = ''; //$product->post->post_excerpt; // $product->get_title(); if (defined('WOOCOMMERCE_VERSION') && version_compare(WOOCOMMERCE_VERSION, '2.4', '>')) { $woi = new WC_Order_Item_Meta($item); } else { $woi = new WC_Order_Item_Meta($order->get_item_meta($order_item_id, '')); } $gf = $woi->get_formatted(); if ($gf) { foreach ($gf as $attr) { $desc .= implode('=', $attr) . '|'; } } $item_price_with_tax = $order->get_item_subtotal($item, true); $item_totalprice_with_tax = $order->get_item_total($item, true); $discount_pct = ($item_price_with_tax - $item_totalprice_with_tax) / $item_price_with_tax * 100.0; //( $line_price_with_tax - $line_totalprice_with_tax); //)) / $line_price_with_tax*100; if ($product->product_type == 'variable') { $desc .= implode(',', $product->get_variation_attributes()) . ' '; } $desc .= $product->post->post_excerpt; //apply_filters( 'woocommerce_short_description', $product->post->post_excerpt)); //$product_total = $product_total + $item_totalprice_with_tax; //$discount_total = $discount_total - $line_price_with_tax - $item_totalprice_with_tax; $encoding = get_bloginfo('charset'); //if($plugin->debug) { // $plugin->log->add($plugin->id, $encoding.' Item total price='.$item_totalprice_with_tax." subtotal=".$item_price_with_tax." discount-%=".$discount_pct.":".$desc); //} $sku = '-'; if ($product->get_sku()) { $sku = $product->get_sku(); } $row = array('pmt_row_name' => $item['name'], 'pmt_row_desc' => strip_tags(html_entity_decode($desc)), 'pmt_row_quantity' => $item['qty'], 'pmt_row_articlenr' => $sku, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $order->get_item_subtotal($item, true))), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $this->calc_tax_rate($product))), 'pmt_row_discountpercentage' => str_replace('.', ',', sprintf("%.2f", $discount_pct)), 'pmt_row_type' => 1); array_push($products_rows, $row); } //Coupons if ($cart) { foreach ($cart->get_coupons() as $code => $coupon) { $coupon_desc = $code; $coupon = new WC_Coupon($code); if ($coupon->apply_before_tax()) { continue; } $coupon_post = get_post($coupon->id); $excerpt = $coupon_post->post_excerpt; if ($excerpt) { $coupon_desc .= ' ' . $excerpt; } $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $cart->get_coupon_discount_amount($code))), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6); array_push($products_rows, $row); } } elseif ($order->get_total_discount()) { $discount = $order->get_total_discount(); $coupon_desc = implode(',', $order->get_used_coupons()); $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $discount)), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6); array_push($products_rows, $row); } //Shipping costs $shipping_cost = $order->get_total_shipping() + $order->get_shipping_tax(); //- $order->get_shipping_tax( ); if ($order->get_total_shipping() > 0) { $shipping_tax = 100 * $order->get_shipping_tax() / $order->get_total_shipping(); } else { $shipping_tax = 0; } $row = array('pmt_row_name' => __('Shipping cost', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($order->get_shipping_method())), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $shipping_tax)), 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 2); array_push($products_rows, $row); $returnURL = $plugin->notify_url; if ($woocommerce->session && $woocommerce->session->id) { $sessionid = $woocommerce->session->id; $returnURL = add_query_arg('sessionid', $sessionid, $plugin->notify_url); //get_return_url( $order )); $returnURL = add_query_arg('orderid', $id, $returnURL); } //$returnURL = add_query_arg('gateway', 'wc_maksurva_emaksut', $returnURL); $billing_email = $order->billing_email; $billing_phone = $order->billing_phone; if (!empty($order->customer_user)) { $user = get_user_by('id', $order->customer_user); if (empty($order->billing_email)) { $billing_email = $user->user_email; } if (empty($order->billing_email)) { $billing_email = $user->user_phone; } } $locale = get_bloginfo('language'); global $sitepress; if ($sitepress) { $locale = $sitepress->get_locale(ICL_LANGUAGE_CODE); } if ($locale != "fi_FI" || $locale != "sv_FI" || $locale != "en_FI") { if (substr($locale, 0, 2) == "fi") { $locale = "fi_FI"; } elseif (strpos($locale, "sv") != FALSE) { $locale = "sv_FI"; } else { $locale = "en_FI"; } } $options = array("pmt_keygeneration" => $plugin->maksuturva_keyversion, "pmt_id" => $id, "pmt_orderid" => $order->id, "pmt_reference" => $pid, "pmt_sellerid" => $sellerId, "pmt_duedate" => $dueDate, "pmt_userlocale" => $locale, "pmt_okreturn" => add_query_arg('pmt_act', 'ok', $returnURL), "pmt_errorreturn" => add_query_arg('pmt_act', 'error', $returnURL), "pmt_cancelreturn" => add_query_arg('pmt_act', 'cancel', $returnURL), "pmt_delayedpayreturn" => add_query_arg('pmt_act', 'delay', $returnURL), "pmt_amount" => str_replace('.', ',', sprintf("%.2f", $order->get_total() - $shipping_cost)), "pmt_deliveryname" => trim($order->shipping_first_name . ' ' . $order->shipping_last_name), "pmt_deliveryaddress" => trim($order->shipping_address_1 . " " . $order->shipping_address_2), "pmt_deliverypostalcode" => trim($this->clean($order->shipping_postcode, '000')), "pmt_deliverycity" => trim($this->clean($order->shipping_city)), "pmt_deliverycountry" => trim($this->clean($order->shipping_country)), "pmt_buyername" => trim($order->billing_first_name . ' ' . $order->billing_last_name), "pmt_buyeraddress" => trim($this->clean($order->billing_address_1 . " " . $order->billing_address_2)), "pmt_buyerpostalcode" => trim($this->clean($order->billing_postcode, '000')), "pmt_buyercity" => trim($this->clean($order->billing_city)), "pmt_buyercountry" => trim($order->billing_country), "pmt_buyeremail" => trim($billing_email), "pmt_buyerphone" => trim($billing_phone), "pmt_escrow" => $plugin->maksuturva_emaksut == "no" ? "Y" : "N", "pmt_sellercosts" => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), "pmt_rows" => count($products_rows), "pmt_rows_data" => $products_rows); parent::__construct($secretKey, $options, $plugin->maksuturva_encoding, $plugin->maksuturva_url); }