/**
  * Prepare a single product category output for response.
  *
  * @param WP_Term $item Term object.
  * @param WP_REST_Request $request
  * @return WP_REST_Response $response
  */
 public function prepare_item_for_response($item, $request)
 {
     // Get category display type.
     $display_type = get_woocommerce_term_meta($item->term_id, 'display_type');
     // Get category order.
     $menu_order = get_woocommerce_term_meta($item->term_id, 'order');
     $data = array('id' => (int) $item->term_id, 'name' => $item->name, 'slug' => $item->slug, 'parent' => (int) $item->parent, 'description' => $item->description, 'display' => $display_type ? $display_type : 'default', 'image' => array(), 'menu_order' => (int) $menu_order, 'count' => (int) $item->count);
     // Get category image.
     if ($image_id = get_woocommerce_term_meta($item->term_id, 'thumbnail_id')) {
         $attachment = get_post($image_id);
         $data['image'] = array('id' => (int) $image_id, 'date_created' => wc_rest_prepare_date_response($attachment->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($attachment->post_modified_gmt), 'src' => wp_get_attachment_url($image_id), 'title' => get_the_title($attachment), 'alt' => get_post_meta($image_id, '_wp_attachment_image_alt', true));
     }
     $context = !empty($request['context']) ? $request['context'] : 'view';
     $data = $this->add_additional_fields_to_object($data, $request);
     $data = $this->filter_response_by_context($data, $context);
     $response = rest_ensure_response($data);
     $response->add_links($this->prepare_links($item, $request));
     /**
      * Filter a term item returned from the API.
      *
      * Allows modification of the term data right before it is returned.
      *
      * @param WP_REST_Response  $response  The response object.
      * @param object            $item      The original term object.
      * @param WP_REST_Request   $request   Request used to generate the response.
      */
     return apply_filters("woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request);
 }
コード例 #2
0
 /**
  * Prepare a single coupon 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;
     // Get the coupon code.
     $code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $post->ID));
     $coupon = new WC_Coupon($code);
     $data = array('id' => $coupon->id, 'code' => $coupon->code, 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->type, 'description' => $post->post_excerpt, 'amount' => wc_format_decimal($coupon->coupon_amount, 2), 'expiry_date' => !empty($coupon->expiry_date) ? wc_rest_prepare_date_response($coupon->expiry_date) : null, 'usage_count' => (int) $coupon->usage_count, 'individual_use' => 'yes' === $coupon->individual_use, 'product_ids' => array_map('absint', (array) $coupon->product_ids), 'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids), 'usage_limit' => !empty($coupon->usage_limit) ? $coupon->usage_limit : null, 'usage_limit_per_user' => !empty($coupon->usage_limit_per_user) ? $coupon->usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, 'free_shipping' => $coupon->enable_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->product_categories), 'excluded_product_categories' => array_map('absint', (array) $coupon->exclude_product_categories), 'exclude_sale_items' => $coupon->exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2), 'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2), 'email_restrictions' => $coupon->customer_email, 'used_by' => $coupon->get_used_by());
     $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($post));
     /**
      * 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);
 }
コード例 #3
0
ファイル: coupons.php プロジェクト: pelmered/woocommerce
 /**
  * Test getting a single coupon.
  * @since 2.7.0
  */
 public function test_get_coupon()
 {
     wp_set_current_user($this->user);
     $coupon = WC_Helper_Coupon::create_coupon('dummycoupon-1');
     $post = get_post($coupon->get_id());
     $response = $this->server->dispatch(new WP_REST_Request('GET', '/wc/v1/coupons/' . $coupon->get_id()));
     $data = $response->get_data();
     $this->assertEquals(200, $response->get_status());
     $this->assertEquals(array('id' => $coupon->get_id(), 'code' => 'dummycoupon-1', 'amount' => '1.00', 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => 'fixed_cart', 'description' => 'This is a dummy coupon', 'expiry_date' => null, 'usage_count' => 0, 'individual_use' => false, 'product_ids' => array(), 'exclude_product_ids' => array(), 'usage_limit' => null, 'usage_limit_per_user' => null, 'limit_usage_to_x_items' => 0, 'free_shipping' => false, 'product_categories' => array(), 'excluded_product_categories' => array(), 'exclude_sale_items' => false, 'minimum_amount' => '0.00', 'maximum_amount' => '0.00', 'email_restrictions' => array(), 'used_by' => array()), $data);
 }
 /**
  * Prepare a single variation 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)
 {
     $variation = wc_get_product($post);
     $data = array('id' => $variation->get_id(), 'date_created' => wc_rest_prepare_date_response($variation->get_date_created()), 'date_modified' => wc_rest_prepare_date_response($variation->get_date_modified()), 'description' => $variation->get_description(), 'permalink' => $variation->get_permalink(), 'sku' => $variation->get_sku(), 'price' => $variation->get_price(), 'regular_price' => $variation->get_regular_price(), 'sale_price' => $variation->get_sale_price(), 'date_on_sale_from' => $variation->get_date_on_sale_from() ? date('Y-m-d', $variation->get_date_on_sale_from()) : '', 'date_on_sale_to' => $variation->get_date_on_sale_to() ? date('Y-m-d', $variation->get_date_on_sale_to()) : '', 'on_sale' => $variation->is_on_sale(), 'visible' => $variation->is_visible(), 'purchasable' => $variation->is_purchasable(), 'virtual' => $variation->is_virtual(), 'downloadable' => $variation->is_downloadable(), 'downloads' => $this->get_downloads($variation), 'download_limit' => '' !== $variation->get_download_limit() ? (int) $variation->get_download_limit() : -1, 'download_expiry' => '' !== $variation->get_download_expiry() ? (int) $variation->get_download_expiry() : -1, 'tax_status' => $variation->get_tax_status(), 'tax_class' => $variation->get_tax_class(), 'manage_stock' => $variation->managing_stock(), 'stock_quantity' => $variation->get_stock_quantity(), 'in_stock' => $variation->is_in_stock(), 'backorders' => $variation->get_backorders(), 'backorders_allowed' => $variation->backorders_allowed(), 'backordered' => $variation->is_on_backorder(), 'weight' => $variation->get_weight(), 'dimensions' => array('length' => $variation->get_length(), 'width' => $variation->get_width(), 'height' => $variation->get_height()), 'shipping_class' => $variation->get_shipping_class(), 'shipping_class_id' => $variation->get_shipping_class_id(), 'image' => $this->get_images($variation), 'attributes' => $this->get_attributes($variation));
     $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($variation, $request));
     /**
      * 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);
 }
コード例 #5
0
 /**
  * Prepare a single coupon 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)
 {
     // Get the coupon code.
     $code = wc_get_coupon_code_by_id($post->ID);
     $coupon = new WC_Coupon($code);
     $data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->get_discount_type(), 'description' => $coupon->get_description(), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'expiry_date' => $coupon->get_expiry_date() ? wc_rest_prepare_date_response($coupon->get_expiry_date()) : null, 'usage_count' => (int) $coupon->get_usage_count(), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'free_shipping' => $coupon->get_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->get_product_categories()), 'excluded_product_categories' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'email_restrictions' => $coupon->get_email_restrictions(), 'used_by' => $coupon->get_used_by());
     $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($post));
     /**
      * 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);
 }
コード例 #6
0
 /**
  * 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);
 }
コード例 #7
0
 /**
  * Prepare a single webhook output for response.
  *
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($post, $request)
 {
     $id = (int) $post->ID;
     $webhook = new WC_Webhook($id);
     $data = array('id' => $webhook->id, 'name' => $webhook->get_name(), 'status' => $webhook->get_status(), 'topic' => $webhook->get_topic(), 'resource' => $webhook->get_resource(), 'event' => $webhook->get_event(), 'hooks' => $webhook->get_hooks(), 'delivery_url' => $webhook->get_delivery_url(), 'date_created' => wc_rest_prepare_date_response($webhook->get_post_data()->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($webhook->get_post_data()->post_modified_gmt));
     $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($post));
     /**
      * Filter webhook object returned from the REST API.
      *
      * @param WP_REST_Response $response The response object.
      * @param WC_Webhook       $webhook  Webhook object used to create response.
      * @param WP_REST_Request  $request  Request object.
      */
     return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request);
 }
コード例 #8
0
 /**
  * Prepare a single order note output for response.
  *
  * @param WP_Comment $note Order note object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($note, $request)
 {
     $data = array('id' => (int) $note->comment_ID, 'date_created' => wc_rest_prepare_date_response($note->comment_date_gmt), 'note' => $note->comment_content, 'customer_note' => (bool) get_comment_meta($note->comment_ID, 'is_customer_note', true));
     $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($note));
     /**
      * Filter order note object returned from the REST API.
      *
      * @param WP_REST_Response $response The response object.
      * @param WP_Comment       $note     Order note object used to create response.
      * @param WP_REST_Request  $request  Request object.
      */
     return apply_filters('woocommerce_rest_prepare_order_note', $response, $note, $request);
 }
コード例 #9
0
 /**
  * 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)
 {
     $this->request = $request;
     $statuses = wc_get_order_statuses();
     $order = wc_get_order($post);
     $data = $order->get_data();
     $format_decimal = array('discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax');
     $format_date = array('date_created', 'date_modified', 'date_completed');
     $format_line_items = array('line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines');
     // Format decimal values.
     foreach ($format_decimal as $key) {
         $data[$key] = wc_format_decimal($data[$key], $this->request['dp']);
     }
     // Format date values.
     foreach ($format_date as $key) {
         $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : false;
     }
     // Format the order status.
     $data['status'] = 'wc-' === substr($data['status'], 0, 3) ? substr($data['status'], 3) : $data['status'];
     // Format line items.
     foreach ($format_line_items as $key) {
         $data[$key] = array_values(array_map(array($this, 'get_order_item_data'), $data[$key]));
     }
     // Refunds.
     foreach ($order->get_refunds() as $refund) {
         $data['refunds'][] = array('id' => $refund->get_id(), 'refund' => $refund->get_reason() ? $refund->get_reason() : '', 'total' => '-' . wc_format_decimal($refund->get_amount(), $this->request['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);
     $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);
 }
コード例 #10
0
ファイル: functions.php プロジェクト: pelmered/woocommerce
 /**
  * Test wc_rest_prepare_date_response().
  *
  * @since 2.6.0
  */
 public function test_wc_rest_prepare_date_response()
 {
     $this->assertEquals('2016-06-06T06:06:06', wc_rest_prepare_date_response('2016-06-06 06:06:06'));
 }
コード例 #11
0
 /**
  * Get an individual variation's data.
  *
  * @todo Remove in future version of the API. We have variations endpoints now.
  *
  * @param WC_Product $product Product instance.
  * @return array
  */
 protected function get_variation_data($product)
 {
     $variations = array();
     foreach ($product->get_children() as $child_id) {
         $variation = wc_get_product($child_id);
         if (!$variation->exists()) {
             continue;
         }
         $variations[] = array('id' => $variation->get_id(), 'date_created' => wc_rest_prepare_date_response($variation->get_date_created()), 'date_modified' => wc_rest_prepare_date_response($variation->get_date_modified()), 'permalink' => $variation->get_permalink(), 'description' => $variation->get_description(), 'sku' => $variation->get_sku(), 'price' => $variation->get_price(), 'regular_price' => $variation->get_regular_price(), 'sale_price' => $variation->get_sale_price(), 'date_on_sale_from' => $variation->get_date_on_sale_from() ? date('Y-m-d', $variation->get_date_on_sale_from()) : '', 'date_on_sale_to' => $variation->get_date_on_sale_to() ? date('Y-m-d', $variation->get_date_on_sale_to()) : '', 'on_sale' => $variation->is_on_sale(), 'purchasable' => $variation->is_purchasable(), 'visible' => $variation->is_visible(), 'virtual' => $variation->is_virtual(), 'downloadable' => $variation->is_downloadable(), 'downloads' => $this->get_downloads($variation), 'download_limit' => '' !== $variation->get_download_limit() ? (int) $variation->get_download_limit() : -1, 'download_expiry' => '' !== $variation->get_download_expiry() ? (int) $variation->get_download_expiry() : -1, 'tax_status' => $variation->get_tax_status(), 'tax_class' => $variation->get_tax_class(), 'manage_stock' => $variation->managing_stock(), 'stock_quantity' => $variation->get_stock_quantity(), 'in_stock' => $variation->is_in_stock(), 'backorders' => $variation->get_backorders(), 'backorders_allowed' => $variation->backorders_allowed(), 'backordered' => $variation->is_on_backorder(), 'weight' => $variation->get_weight(), 'dimensions' => array('length' => $variation->get_length(), 'width' => $variation->get_width(), 'height' => $variation->get_height()), 'shipping_class' => $variation->get_shipping_class(), 'shipping_class_id' => $variation->get_shipping_class_id(), 'image' => $this->get_images($variation), 'attributes' => $this->get_attributes($variation));
     }
     return $variations;
 }
コード例 #12
0
 /**
  * Prepare a single customer output for response.
  *
  * @param WP_User $customer Customer object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($customer, $request)
 {
     $last_order = wc_get_customer_last_order($customer->ID);
     $data = array('id' => $customer->ID, 'date_created' => wc_rest_prepare_date_response($customer->user_registered), 'date_modified' => $customer->last_update ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->last_update)) : null, 'email' => $customer->user_email, 'first_name' => $customer->first_name, 'last_name' => $customer->last_name, 'username' => $customer->user_login, 'last_order' => array('id' => is_object($last_order) ? $last_order->id : null, 'date' => is_object($last_order) ? wc_rest_prepare_date_response($last_order->post->post_date_gmt) : null), 'orders_count' => wc_get_customer_order_count($customer->ID), 'total_spent' => wc_format_decimal(wc_get_customer_total_spent($customer->ID), 2), 'avatar_url' => wc_get_customer_avatar_url($customer->customer_email), 'billing' => array('first_name' => $customer->billing_first_name, 'last_name' => $customer->billing_last_name, 'company' => $customer->billing_company, 'address_1' => $customer->billing_address_1, 'address_2' => $customer->billing_address_2, 'city' => $customer->billing_city, 'state' => $customer->billing_state, 'postcode' => $customer->billing_postcode, 'country' => $customer->billing_country, 'email' => $customer->billing_email, 'phone' => $customer->billing_phone), 'shipping' => array('first_name' => $customer->shipping_first_name, 'last_name' => $customer->shipping_last_name, 'company' => $customer->shipping_company, 'address_1' => $customer->shipping_address_1, 'address_2' => $customer->shipping_address_2, 'city' => $customer->shipping_city, 'state' => $customer->shipping_state, 'postcode' => $customer->shipping_postcode, 'country' => $customer->shipping_country));
     $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($customer));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response  The response object.
      * @param WP_User          $customer  User object used to create response.
      * @param WP_REST_Request  $request   Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $customer, $request);
 }
コード例 #13
0
 /**
  * Prepare a single customer output for response.
  *
  * @param WP_User           $user_data User object.
  * @param WP_REST_Request   $request   Request object.
  * @return WP_REST_Response $response  Response data.
  */
 public function prepare_item_for_response($user_data, $request)
 {
     $customer = new WC_Customer($user_data->ID);
     $last_order_data = $customer->get_last_order();
     $last_order = null;
     if ($last_order_data) {
         $last_order = array('id' => $last_order_data->get_id(), 'date' => wc_rest_prepare_date_response($last_order_data->get_date_created()));
     }
     $data = array('id' => $customer->get_id(), 'date_created' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_created())), 'date_modified' => $customer->get_date_modified() ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_modified())) : null, 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'last_order' => $last_order, 'orders_count' => $customer->get_order_count(), 'total_spent' => $customer->get_total_spent(), 'avatar_url' => $customer->get_avatar_url(), 'billing' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country()));
     $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($user_data));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response   The response object.
      * @param WP_User          $user_data  User object used to create response.
      * @param WP_REST_Request  $request    Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request);
 }
コード例 #14
0
 /**
  * Prepare a single product review output for response.
  *
  * @param WP_Comment $review Product review object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($review, $request)
 {
     $data = array('id' => (int) $review->comment_ID, 'date_created' => wc_rest_prepare_date_response($review->comment_date_gmt), 'review' => $review->comment_content, 'rating' => (int) get_comment_meta($review->comment_ID, 'rating', true), 'reviewer_name' => $review->comment_author, 'reviewer_email' => $review->comment_author_email, 'verified' => wc_review_is_from_verified_owner($review->comment_ID));
     $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($review, $request));
     /**
      * Filter product reviews object returned from the REST API.
      *
      * @param WP_REST_Response $response The response object.
      * @param WP_Comment       $review   Product review object used to create response.
      * @param WP_REST_Request  $request  Request object.
      */
     return apply_filters('woocommerce_rest_prepare_product_review', $response, $review, $request);
 }
コード例 #15
0
 /**
  * Prepare a single webhook delivery output for response.
  *
  * @param stdClass $log Delivery log object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($log, $request)
 {
     $data = (array) $log;
     // Add timestamp.
     $data['date_created'] = wc_rest_prepare_date_response($log->comment->comment_date_gmt);
     // Remove comment object.
     unset($data['comment']);
     $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($log));
     /**
      * Filter webhook delivery object returned from the REST API.
      *
      * @param WP_REST_Response $response The response object.
      * @param stdClass         $log      Delivery log object used to create response.
      * @param WP_REST_Request  $request  Request object.
      */
     return apply_filters('woocommerce_rest_prepare_webhook_delivery', $response, $log, $request);
 }
コード例 #16
0
 /**
  * Prepare a single coupon 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)
 {
     $code = wc_get_coupon_code_by_id($post->ID);
     $coupon = new WC_Coupon($code);
     $data = $coupon->get_data();
     $format_decimal = array('amount', 'minimum_amount', 'maximum_amount');
     $format_date = array('date_created', 'date_modified', 'date_expires');
     $format_null = array('usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items');
     // Format decimal values.
     foreach ($format_decimal as $key) {
         $data[$key] = wc_format_decimal($data[$key], 2);
     }
     // Format date values.
     foreach ($format_date as $key) {
         $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : null;
     }
     // Format null values.
     foreach ($format_null as $key) {
         $data[$key] = $data[$key] ? $data[$key] : null;
     }
     $context = !empty($request['context']) ? $request['context'] : 'view';
     $data = $this->add_additional_fields_to_object($data, $request);
     $data = $this->filter_response_by_context($data, $context);
     $response = rest_ensure_response($data);
     $response->add_links($this->prepare_links($post));
     /**
      * 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);
 }
コード例 #17
0
 /**
  * Prepare a single customer output for response.
  *
  * @param WP_User           $user_data User object.
  * @param WP_REST_Request   $request   Request object.
  * @return WP_REST_Response $response  Response data.
  */
 public function prepare_item_for_response($user_data, $request)
 {
     $customer = new WC_Customer($user_data->ID);
     $data = $customer->get_data();
     $format_date = array('date_created', 'date_modified');
     // Format date values.
     foreach ($format_date as $key) {
         $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : null;
     }
     // Remove unwanted CRUD data.
     unset($data['role']);
     // Additional non-crud data.
     $data['last_order'] = null;
     $data['orders_count'] = $customer->get_order_count();
     $data['total_spent'] = $customer->get_total_spent();
     $data['avatar_url'] = $customer->get_avatar_url();
     if ($last_order_data = $customer->get_last_order()) {
         $data['last_order'] = array('id' => $last_order_data->get_id(), 'date' => $last_order_data->get_date_created() ? wc_rest_prepare_date_response($last_order_data->get_date_created()) : null);
     }
     $context = !empty($request['context']) ? $request['context'] : 'view';
     $data = $this->add_additional_fields_to_object($data, $request);
     $data = $this->filter_response_by_context($data, $context);
     $response = rest_ensure_response($data);
     $response->add_links($this->prepare_links($user_data));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response   The response object.
      * @param WP_User          $user_data  User object used to create response.
      * @param WP_REST_Request  $request    Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request);
 }
コード例 #18
0
 /**
  * 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);
 }
 /**
  * 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)
 {
     $this->request = $request;
     $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 || $refund->get_parent_id() !== $order->get_id()) {
         return new WP_Error('woocommerce_rest_invalid_order_refund_id', __('Invalid order refund ID.', 'woocommerce'), 404);
     }
     $data = $refund->get_data();
     $format_decimal = array('amount');
     $format_date = array('date_created');
     $format_line_items = array('line_items');
     // Format decimal values.
     foreach ($format_decimal as $key) {
         $data[$key] = wc_format_decimal($data[$key], $this->request['dp']);
     }
     // Format date values.
     foreach ($format_date as $key) {
         $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : false;
     }
     // Format line items.
     foreach ($format_line_items as $key) {
         $data[$key] = array_values(array_map(array($this, 'get_order_item_data'), $data[$key]));
     }
     // Unset unwanted data
     unset($data['parent_id'], $data['status'], $data['currency'], $data['prices_include_tax'], $data['version'], $data['date_modified'], $data['discount_total'], $data['discount_tax'], $data['shipping_total'], $data['shipping_tax'], $data['cart_tax'], $data['cart_total'], $data['total'], $data['total_tax'], $data['tax_lines'], $data['shipping_lines'], $data['fee_lines'], $data['coupon_lines']);
     $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, $request));
     /**
      * 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);
 }
 /**
  * Prepare a single download output for response.
  *
  * @param stdObject $download Download object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($download, $request)
 {
     $data = (array) $download;
     $data['access_expires'] = $data['access_expires'] ? wc_rest_prepare_date_response($data['access_expires']) : 'never';
     $data['downloads_remaining'] = '' === $data['downloads_remaining'] ? 'unlimited' : $data['downloads_remaining'];
     $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($download, $request));
     /**
      * Filter customer download data returned from the REST API.
      *
      * @param WP_REST_Response $response  The response object.
      * @param stdObject        $download  Download object used to create response.
      * @param WP_REST_Request  $request   Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer_download', $response, $download, $request);
 }
コード例 #21
-1
ファイル: customers.php プロジェクト: woocommerce/woocommerce
 /**
  * Test getting customers.
  *
  * @since 2.7.0
  */
 public function test_get_customers()
 {
     wp_set_current_user(1);
     $customer_1 = WC_Helper_Customer::create_customer();
     WC_Helper_Customer::create_customer('test2', 'test2', '*****@*****.**');
     $request = new WP_REST_Request('GET', '/wc/v1/customers');
     $request->set_query_params(array('orderby' => 'id'));
     $response = $this->server->dispatch($request);
     $customers = $response->get_data();
     $this->assertEquals(200, $response->get_status());
     $this->assertEquals(2, count($customers));
     $this->assertContains(array('id' => $customer_1->get_id(), 'date_created' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer_1->get_date_created())), 'date_modified' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer_1->get_date_modified())), 'email' => '*****@*****.**', 'first_name' => 'Justin', 'last_name' => '', 'role' => 'customer', 'username' => 'testcustomer', 'billing' => array('first_name' => '', 'last_name' => '', 'company' => '', 'address_1' => '123 South Street', 'address_2' => 'Apt 1', 'city' => 'Philadelphia', 'state' => 'PA', 'postcode' => '19123', 'country' => 'US', 'email' => '', 'phone' => ''), 'shipping' => array('first_name' => '', 'last_name' => '', 'company' => '', 'address_1' => '123 South Street', 'address_2' => 'Apt 1', 'city' => 'Philadelphia', 'state' => 'PA', 'postcode' => '19123', 'country' => 'US'), 'is_paying_customer' => false, 'orders_count' => 0, 'total_spent' => '0.00', 'avatar_url' => $customer_1->get_avatar_url(), 'meta_data' => array(), '_links' => array('self' => array(array('href' => rest_url('/wc/v1/customers/' . $customer_1->get_id() . ''))), 'collection' => array(array('href' => rest_url('/wc/v1/customers'))))), $customers);
 }