| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626 |
- <?php
- /**
- * REST API Orders controller
- *
- * Handles requests to the /orders endpoint.
- *
- * @author WooThemes
- * @category API
- * @package WooCommerce/API
- * @since 3.0.0
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- /**
- * REST API Orders controller class.
- *
- * @package WooCommerce/API
- * @extends WC_REST_Posts_Controller
- */
- class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
- /**
- * Endpoint namespace.
- *
- * @var string
- */
- protected $namespace = 'wc/v1';
- /**
- * Route base.
- *
- * @var string
- */
- protected $rest_base = 'orders';
- /**
- * Post type.
- *
- * @var string
- */
- protected $post_type = 'shop_order';
- /**
- * Initialize orders actions.
- */
- public function __construct() {
- add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
- }
- /**
- * Register the routes for orders.
- */
- public function register_routes() {
- register_rest_route( $this->namespace, '/' . $this->rest_base, array(
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_items' ),
- 'permission_callback' => array( $this, 'get_items_permissions_check' ),
- 'args' => $this->get_collection_params(),
- ),
- array(
- 'methods' => WP_REST_Server::CREATABLE,
- 'callback' => array( $this, 'create_item' ),
- 'permission_callback' => array( $this, 'create_item_permissions_check' ),
- 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
- ),
- 'schema' => array( $this, 'get_public_item_schema' ),
- ) );
- register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
- 'args' => array(
- 'id' => array(
- 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
- 'type' => 'integer',
- ),
- ),
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_item' ),
- 'permission_callback' => array( $this, 'get_item_permissions_check' ),
- 'args' => array(
- 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
- ),
- ),
- array(
- 'methods' => WP_REST_Server::EDITABLE,
- 'callback' => array( $this, 'update_item' ),
- 'permission_callback' => array( $this, 'update_item_permissions_check' ),
- 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
- ),
- array(
- 'methods' => WP_REST_Server::DELETABLE,
- 'callback' => array( $this, 'delete_item' ),
- 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
- 'args' => array(
- 'force' => array(
- 'default' => false,
- 'type' => 'boolean',
- 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
- ),
- ),
- ),
- 'schema' => array( $this, 'get_public_item_schema' ),
- ) );
- register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
- array(
- 'methods' => WP_REST_Server::EDITABLE,
- 'callback' => array( $this, 'batch_items' ),
- 'permission_callback' => array( $this, 'batch_items_permissions_check' ),
- 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
- ),
- 'schema' => array( $this, 'get_public_batch_schema' ),
- ) );
- }
- /**
- * 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 ) {
- $order = wc_get_order( $post );
- $dp = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $request['dp'] );
- $data = array(
- 'id' => $order->get_id(),
- 'parent_id' => $order->get_parent_id(),
- 'status' => $order->get_status(),
- 'order_key' => $order->get_order_key(),
- 'number' => $order->get_order_number(),
- 'currency' => $order->get_currency(),
- 'version' => $order->get_version(),
- 'prices_include_tax' => $order->get_prices_include_tax(),
- 'date_created' => wc_rest_prepare_date_response( $order->get_date_created() ), // v1 API used UTC.
- 'date_modified' => wc_rest_prepare_date_response( $order->get_date_modified() ), // v1 API used UTC.
- 'customer_id' => $order->get_customer_id(),
- 'discount_total' => wc_format_decimal( $order->get_total_discount(), $dp ),
- 'discount_tax' => wc_format_decimal( $order->get_discount_tax(), $dp ),
- 'shipping_total' => wc_format_decimal( $order->get_shipping_total(), $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->get_payment_method(),
- 'payment_method_title' => $order->get_payment_method_title(),
- 'transaction_id' => $order->get_transaction_id(),
- 'customer_ip_address' => $order->get_customer_ip_address(),
- 'customer_user_agent' => $order->get_customer_user_agent(),
- 'created_via' => $order->get_created_via(),
- 'customer_note' => $order->get_customer_note(),
- 'date_completed' => wc_rest_prepare_date_response( $order->get_date_completed(), false ), // v1 API used local time.
- 'date_paid' => wc_rest_prepare_date_response( $order->get_date_paid(), false ), // v1 API used local time.
- 'cart_hash' => $order->get_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 = $item->get_product_id();
- $variation_id = $item->get_variation_id();
- $product_sku = $product->get_sku();
- }
- $item_meta = array();
- $hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
- foreach ( $item->get_formatted_meta_data( $hideprefix, true ) as $meta_key => $formatted_meta ) {
- $item_meta[] = array(
- 'key' => $formatted_meta->key,
- 'label' => $formatted_meta->display_key,
- 'value' => wc_clean( $formatted_meta->display_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 = $shipping_item->get_taxes();
- if ( ! empty( $shipping_taxes['total'] ) ) {
- $shipping_line['total_tax'] = wc_format_decimal( array_sum( $shipping_taxes['total'] ), $dp );
- foreach ( $shipping_taxes['total'] 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' => '',
- );
- }
- if ( isset( $fee_line_taxes['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->get_id(),
- 'refund' => $refund->get_reason() ? $refund->get_reason() : '',
- 'total' => '-' . wc_format_decimal( $refund->get_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, $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 links for the request.
- *
- * @param WC_Order $order Order object.
- * @param WP_REST_Request $request Request object.
- * @return array Links for the given order.
- */
- protected function prepare_links( $order, $request ) {
- $links = array(
- 'self' => array(
- 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $order->get_id() ) ),
- ),
- 'collection' => array(
- 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
- ),
- );
- if ( 0 !== (int) $order->get_user_id() ) {
- $links['customer'] = array(
- 'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $order->get_user_id() ) ),
- );
- }
- if ( 0 !== (int) $order->get_parent_id() ) {
- $links['up'] = array(
- 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order->get_parent_id() ) ),
- );
- }
- return $links;
- }
- /**
- * Query args.
- *
- * @param array $args
- * @param WP_REST_Request $request
- * @return array
- */
- public function query_args( $args, $request ) {
- global $wpdb;
- // Set post_status.
- if ( 'any' !== $request['status'] ) {
- $args['post_status'] = 'wc-' . $request['status'];
- } else {
- $args['post_status'] = 'any';
- }
- if ( isset( $request['customer'] ) ) {
- if ( ! empty( $args['meta_query'] ) ) {
- $args['meta_query'] = array();
- }
- $args['meta_query'][] = array(
- 'key' => '_customer_user',
- 'value' => $request['customer'],
- 'type' => 'NUMERIC',
- );
- }
- // Search by product.
- if ( ! empty( $request['product'] ) ) {
- $order_ids = $wpdb->get_col( $wpdb->prepare( "
- SELECT order_id
- FROM {$wpdb->prefix}woocommerce_order_items
- WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
- AND order_item_type = 'line_item'
- ", $request['product'] ) );
- // Force WP_Query return empty if don't found any order.
- $order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 );
- $args['post__in'] = $order_ids;
- }
- // Search.
- if ( ! empty( $args['s'] ) ) {
- $order_ids = wc_order_search( $args['s'] );
- if ( ! empty( $order_ids ) ) {
- unset( $args['s'] );
- $args['post__in'] = array_merge( $order_ids, array( 0 ) );
- }
- }
- return $args;
- }
- /**
- * Prepare a single order for create.
- *
- * @param WP_REST_Request $request Request object.
- * @return WP_Error|WC_Order $data Object.
- */
- protected function prepare_item_for_database( $request ) {
- $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
- $order = new WC_Order( $id );
- $schema = $this->get_item_schema();
- $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
- // Handle all writable props
- foreach ( $data_keys as $key ) {
- $value = $request[ $key ];
- if ( ! is_null( $value ) ) {
- switch ( $key ) {
- case 'billing' :
- case 'shipping' :
- $this->update_address( $order, $value, $key );
- break;
- case 'line_items' :
- case 'shipping_lines' :
- case 'fee_lines' :
- case 'coupon_lines' :
- if ( is_array( $value ) ) {
- foreach ( $value as $item ) {
- if ( is_array( $item ) ) {
- if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) {
- $order->remove_item( $item['id'] );
- } else {
- $this->set_item( $order, $key, $item );
- }
- }
- }
- }
- break;
- default :
- if ( is_callable( array( $order, "set_{$key}" ) ) ) {
- $order->{"set_{$key}"}( $value );
- }
- break;
- }
- }
- }
- /**
- * Filter the data for the insert.
- *
- * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
- * prepared for the response.
- *
- * @param WC_Order $order The order object.
- * @param WP_REST_Request $request Request object.
- */
- return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $order, $request );
- }
- /**
- * Create base WC Order object.
- * @deprecated 3.0.0
- * @param array $data
- * @return WC_Order
- */
- protected function create_base_order( $data ) {
- return wc_create_order( $data );
- }
- /**
- * Only return writable props from schema.
- * @param array $schema
- * @return bool
- */
- protected function filter_writable_props( $schema ) {
- return empty( $schema['readonly'] );
- }
- /**
- * Create order.
- *
- * @param WP_REST_Request $request Full details about the request.
- * @return int|WP_Error
- */
- protected function create_order( $request ) {
- try {
- // Make sure customer exists.
- if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
- }
- // Make sure customer is part of blog.
- if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id_network',__( 'Customer ID does not belong to this site.', 'woocommerce' ), 400 );
- }
- $order = $this->prepare_item_for_database( $request );
- $order->set_created_via( 'rest-api' );
- $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
- $order->calculate_totals();
- $order->save();
- // Handle set paid.
- if ( true === $request['set_paid'] ) {
- $order->payment_complete( $request['transaction_id'] );
- }
- return $order->get_id();
- } catch ( WC_Data_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
- } catch ( WC_REST_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Update order.
- *
- * @param WP_REST_Request $request Full details about the request.
- * @return int|WP_Error
- */
- protected function update_order( $request ) {
- try {
- $order = $this->prepare_item_for_database( $request );
- $order->save();
- // Handle set paid.
- if ( $order->needs_payment() && true === $request['set_paid'] ) {
- $order->payment_complete( $request['transaction_id'] );
- }
- // If items have changed, recalculate order totals.
- if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) {
- $order->calculate_totals( true );
- }
- return $order->get_id();
- } catch ( WC_Data_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
- } catch ( WC_REST_Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Update address.
- *
- * @param WC_Order $order
- * @param array $posted
- * @param string $type
- */
- protected function update_address( $order, $posted, $type = 'billing' ) {
- foreach ( $posted as $key => $value ) {
- if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) {
- $order->{"set_{$type}_{$key}"}( $value );
- }
- }
- }
- /**
- * Gets the product ID from the SKU or posted ID.
- *
- * @param array $posted Request data
- *
- * @return int
- * @throws WC_REST_Exception
- */
- protected function get_product_id( $posted ) {
- if ( ! empty( $posted['sku'] ) ) {
- $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] );
- } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) {
- $product_id = (int) $posted['product_id'];
- } elseif ( ! empty( $posted['variation_id'] ) ) {
- $product_id = (int) $posted['variation_id'];
- } else {
- throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 );
- }
- return $product_id;
- }
- /**
- * Maybe set an item prop if the value was posted.
- * @param WC_Order_Item $item
- * @param string $prop
- * @param array $posted Request data.
- */
- protected function maybe_set_item_prop( $item, $prop, $posted ) {
- if ( isset( $posted[ $prop ] ) ) {
- $item->{"set_$prop"}( $posted[ $prop ] );
- }
- }
- /**
- * Maybe set item props if the values were posted.
- * @param WC_Order_Item $item
- * @param string[] $props
- * @param array $posted Request data.
- */
- protected function maybe_set_item_props( $item, $props, $posted ) {
- foreach ( $props as $prop ) {
- $this->maybe_set_item_prop( $item, $prop, $posted );
- }
- }
- /**
- * Create or update a line item.
- *
- * @param array $posted Line item data.
- * @param string $action 'create' to add line item or 'update' to update it.
- *
- * @return WC_Order_Item_Product
- * @throws WC_REST_Exception Invalid data, server error.
- */
- protected function prepare_line_items( $posted, $action = 'create' ) {
- $item = new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' );
- $product = wc_get_product( $this->get_product_id( $posted ) );
- if ( $product !== $item->get_product() ) {
- $item->set_product( $product );
- if ( 'create' === $action ) {
- $quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1;
- $total = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
- $item->set_total( $total );
- $item->set_subtotal( $total );
- }
- }
- $this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted );
- return $item;
- }
- /**
- * Create or update an order shipping method.
- *
- * @param $posted $shipping Item data.
- * @param string $action 'create' to add shipping or 'update' to update it.
- *
- * @return WC_Order_Item_Shipping
- * @throws WC_REST_Exception Invalid data, server error.
- */
- protected function prepare_shipping_lines( $posted, $action ) {
- $item = new WC_Order_Item_Shipping( ! empty( $posted['id'] ) ? $posted['id'] : '' );
- if ( 'create' === $action ) {
- if ( empty( $posted['method_id'] ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 );
- }
- }
- $this->maybe_set_item_props( $item, array( 'method_id', 'method_title', 'total' ), $posted );
- return $item;
- }
- /**
- * Create or update an order fee.
- *
- * @param array $posted Item data.
- * @param string $action 'create' to add fee or 'update' to update it.
- *
- * @return WC_Order_Item_Fee
- * @throws WC_REST_Exception Invalid data, server error.
- */
- protected function prepare_fee_lines( $posted, $action ) {
- $item = new WC_Order_Item_Fee( ! empty( $posted['id'] ) ? $posted['id'] : '' );
- if ( 'create' === $action ) {
- if ( empty( $posted['name'] ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_fee_item', __( 'Fee name is required.', 'woocommerce' ), 400 );
- }
- }
- $this->maybe_set_item_props( $item, array( 'name', 'tax_class', 'tax_status', 'total' ), $posted );
- return $item;
- }
- /**
- * Create or update an order coupon.
- *
- * @param array $posted Item data.
- * @param string $action 'create' to add coupon or 'update' to update it.
- *
- * @return WC_Order_Item_Coupon
- * @throws WC_REST_Exception Invalid data, server error.
- */
- protected function prepare_coupon_lines( $posted, $action ) {
- $item = new WC_Order_Item_Coupon( ! empty( $posted['id'] ) ? $posted['id'] : '' );
- if ( 'create' === $action ) {
- if ( empty( $posted['code'] ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 );
- }
- }
- $this->maybe_set_item_props( $item, array( 'code', 'discount' ), $posted );
- return $item;
- }
- /**
- * Wrapper method to create/update order items.
- * When updating, the item ID provided is checked to ensure it is associated
- * with the order.
- *
- * @param WC_Order $order order
- * @param string $item_type
- * @param array $posted item provided in the request body
- * @throws WC_REST_Exception If item ID is not associated with order
- */
- protected function set_item( $order, $item_type, $posted ) {
- global $wpdb;
- if ( ! empty( $posted['id'] ) ) {
- $action = 'update';
- } else {
- $action = 'create';
- }
- $method = 'prepare_' . $item_type;
- // Verify provided line item ID is associated with order.
- if ( 'update' === $action ) {
- $result = $wpdb->get_row(
- $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
- absint( $posted['id'] ),
- absint( $order->get_id() )
- ) );
- if ( is_null( $result ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
- }
- }
- // Prepare item data
- $item = $this->$method( $posted, $action );
- /**
- * Action hook to adjust item before save.
- * @since 3.0.0
- */
- do_action( 'woocommerce_rest_set_order_item', $item, $posted );
- // Save or add to order
- if ( 'create' === $action ) {
- $order->add_item( $item );
- } else {
- $item->save();
- }
- }
- /**
- * Helper method to check if the resource ID associated with the provided item is null.
- * Items can be deleted by setting the resource ID to null.
- *
- * @param array $item Item provided in the request body.
- * @return bool True if the item resource ID is null, false otherwise.
- */
- protected function item_is_null( $item ) {
- $keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' );
- foreach ( $keys as $key ) {
- if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) {
- return true;
- }
- }
- return false;
- }
- /**
- * Create a single item.
- *
- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
- */
- public function create_item( $request ) {
- if ( ! empty( $request['id'] ) ) {
- /* translators: %s: post type */
- return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
- }
- $order_id = $this->create_order( $request );
- if ( is_wp_error( $order_id ) ) {
- return $order_id;
- }
- $post = get_post( $order_id );
- $this->update_additional_fields_for_object( $post, $request );
- /**
- * Fires after a single item is created or updated via the REST API.
- *
- * @param WP_Post $post Post object.
- * @param WP_REST_Request $request Request object.
- * @param boolean $creating True when creating item, false when updating.
- */
- do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
- $request->set_param( 'context', 'edit' );
- $response = $this->prepare_item_for_response( $post, $request );
- $response = rest_ensure_response( $response );
- $response->set_status( 201 );
- $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
- return $response;
- }
- /**
- * Update a single order.
- *
- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
- */
- public function update_item( $request ) {
- try {
- $post_id = (int) $request['id'];
- if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
- return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
- }
- $order_id = $this->update_order( $request );
- if ( is_wp_error( $order_id ) ) {
- return $order_id;
- }
- $post = get_post( $order_id );
- $this->update_additional_fields_for_object( $post, $request );
- /**
- * Fires after a single item is created or updated via the REST API.
- *
- * @param WP_Post $post Post object.
- * @param WP_REST_Request $request Request object.
- * @param boolean $creating True when creating item, false when updating.
- */
- do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
- $request->set_param( 'context', 'edit' );
- $response = $this->prepare_item_for_response( $post, $request );
- return rest_ensure_response( $response );
- } catch ( Exception $e ) {
- return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
- }
- }
- /**
- * Get order statuses without prefixes.
- * @return array
- */
- protected function get_order_statuses() {
- $order_statuses = array();
- foreach ( array_keys( wc_get_order_statuses() ) as $status ) {
- $order_statuses[] = str_replace( 'wc-', '', $status );
- }
- return $order_statuses;
- }
- /**
- * Get the Order's schema, conforming to JSON Schema.
- *
- * @return array
- */
- public function get_item_schema() {
- $schema = array(
- '$schema' => 'http://json-schema.org/draft-04/schema#',
- 'title' => $this->post_type,
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'parent_id' => array(
- 'description' => __( 'Parent order ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- ),
- 'status' => array(
- 'description' => __( 'Order status.', 'woocommerce' ),
- 'type' => 'string',
- 'default' => 'pending',
- 'enum' => $this->get_order_statuses(),
- 'context' => array( 'view', 'edit' ),
- ),
- 'order_key' => array(
- 'description' => __( 'Order key.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'number' => array(
- 'description' => __( 'Order number.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'currency' => array(
- 'description' => __( 'Currency the order was created with, in ISO format.', 'woocommerce' ),
- 'type' => 'string',
- 'default' => get_woocommerce_currency(),
- 'enum' => array_keys( get_woocommerce_currencies() ),
- 'context' => array( 'view', 'edit' ),
- ),
- 'version' => array(
- 'description' => __( 'Version of WooCommerce which last updated the order.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'prices_include_tax' => array(
- 'description' => __( 'True the prices included tax during checkout.', 'woocommerce' ),
- 'type' => 'boolean',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'date_created' => array(
- 'description' => __( "The date the order was created, as GMT.", 'woocommerce' ),
- 'type' => 'date-time',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'date_modified' => array(
- 'description' => __( "The date the order was last modified, as GMT.", 'woocommerce' ),
- 'type' => 'date-time',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'customer_id' => array(
- 'description' => __( 'User ID who owns the order. 0 for guests.', 'woocommerce' ),
- 'type' => 'integer',
- 'default' => 0,
- 'context' => array( 'view', 'edit' ),
- ),
- 'discount_total' => array(
- 'description' => __( 'Total discount amount for the order.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'discount_tax' => array(
- 'description' => __( 'Total discount tax amount for the order.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'shipping_total' => array(
- 'description' => __( 'Total shipping amount for the order.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'shipping_tax' => array(
- 'description' => __( 'Total shipping tax amount for the order.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'cart_tax' => array(
- 'description' => __( 'Sum of line item taxes only.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total' => array(
- 'description' => __( 'Grand total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total_tax' => array(
- 'description' => __( 'Sum of all taxes.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'billing' => array(
- 'description' => __( 'Billing address.', 'woocommerce' ),
- 'type' => 'object',
- 'context' => array( 'view', 'edit' ),
- 'properties' => array(
- 'first_name' => array(
- 'description' => __( 'First name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'last_name' => array(
- 'description' => __( 'Last name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'company' => array(
- 'description' => __( 'Company name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'address_1' => array(
- 'description' => __( 'Address line 1.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'address_2' => array(
- 'description' => __( 'Address line 2.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'city' => array(
- 'description' => __( 'City name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'state' => array(
- 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'postcode' => array(
- 'description' => __( 'Postal code.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'country' => array(
- 'description' => __( 'Country code in ISO 3166-1 alpha-2 format.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'email' => array(
- 'description' => __( 'Email address.', 'woocommerce' ),
- 'type' => 'string',
- 'format' => 'email',
- 'context' => array( 'view', 'edit' ),
- ),
- 'phone' => array(
- 'description' => __( 'Phone number.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- ),
- ),
- 'shipping' => array(
- 'description' => __( 'Shipping address.', 'woocommerce' ),
- 'type' => 'object',
- 'context' => array( 'view', 'edit' ),
- 'properties' => array(
- 'first_name' => array(
- 'description' => __( 'First name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'last_name' => array(
- 'description' => __( 'Last name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'company' => array(
- 'description' => __( 'Company name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'address_1' => array(
- 'description' => __( 'Address line 1.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'address_2' => array(
- 'description' => __( 'Address line 2.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'city' => array(
- 'description' => __( 'City name.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'state' => array(
- 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'postcode' => array(
- 'description' => __( 'Postal code.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'country' => array(
- 'description' => __( 'Country code in ISO 3166-1 alpha-2 format.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- ),
- ),
- 'payment_method' => array(
- 'description' => __( 'Payment method ID.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'payment_method_title' => array(
- 'description' => __( 'Payment method title.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'set_paid' => array(
- 'description' => __( 'Define if the order is paid. It will set the status to processing and reduce stock items.', 'woocommerce' ),
- 'type' => 'boolean',
- 'default' => false,
- 'context' => array( 'edit' ),
- ),
- 'transaction_id' => array(
- 'description' => __( 'Unique transaction ID.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'customer_ip_address' => array(
- 'description' => __( "Customer's IP address.", 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'customer_user_agent' => array(
- 'description' => __( 'User agent of the customer.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'created_via' => array(
- 'description' => __( 'Shows where the order was created.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'customer_note' => array(
- 'description' => __( 'Note left by customer during checkout.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'date_completed' => array(
- 'description' => __( "The date the order was completed, in the site's timezone.", 'woocommerce' ),
- 'type' => 'date-time',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'date_paid' => array(
- 'description' => __( "The date the order was paid, in the site's timezone.", 'woocommerce' ),
- 'type' => 'date-time',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'cart_hash' => array(
- 'description' => __( 'MD5 hash of cart items to ensure orders are not modified.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'line_items' => array(
- 'description' => __( 'Line items data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Item ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'name' => array(
- 'description' => __( 'Product name.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'sku' => array(
- 'description' => __( 'Product SKU.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'product_id' => array(
- 'description' => __( 'Product ID.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- ),
- 'variation_id' => array(
- 'description' => __( 'Variation ID, if applicable.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- ),
- 'quantity' => array(
- 'description' => __( 'Quantity ordered.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- ),
- 'tax_class' => array(
- 'description' => __( 'Tax class of product.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'price' => array(
- 'description' => __( 'Product price.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'subtotal' => array(
- 'description' => __( 'Line subtotal (before discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'subtotal_tax' => array(
- 'description' => __( 'Line subtotal tax (before discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'total' => array(
- 'description' => __( 'Line total (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'total_tax' => array(
- 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'taxes' => array(
- 'description' => __( 'Line taxes.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Tax rate ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total' => array(
- 'description' => __( 'Tax total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'subtotal' => array(
- 'description' => __( 'Tax subtotal.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- 'meta' => array(
- 'description' => __( 'Line item meta data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'key' => array(
- 'description' => __( 'Meta key.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'label' => array(
- 'description' => __( 'Meta label.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'value' => array(
- 'description' => __( 'Meta value.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- 'tax_lines' => array(
- 'description' => __( 'Tax lines data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Item ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'rate_code' => array(
- 'description' => __( 'Tax rate code.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'rate_id' => array(
- 'description' => __( 'Tax rate ID.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'label' => array(
- 'description' => __( 'Tax rate label.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'compound' => array(
- 'description' => __( 'Show if is a compound tax rate.', 'woocommerce' ),
- 'type' => 'boolean',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'tax_total' => array(
- 'description' => __( 'Tax total (not including shipping taxes).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'shipping_tax_total' => array(
- 'description' => __( 'Shipping tax total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- 'shipping_lines' => array(
- 'description' => __( 'Shipping lines data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Item ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'method_title' => array(
- 'description' => __( 'Shipping method name.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- ),
- 'method_id' => array(
- 'description' => __( 'Shipping method ID.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- ),
- 'total' => array(
- 'description' => __( 'Line total (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'total_tax' => array(
- 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'taxes' => array(
- 'description' => __( 'Line taxes.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Tax rate ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total' => array(
- 'description' => __( 'Tax total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- 'fee_lines' => array(
- 'description' => __( 'Fee lines data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Item ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'name' => array(
- 'description' => __( 'Fee name.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- ),
- 'tax_class' => array(
- 'description' => __( 'Tax class of fee.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'tax_status' => array(
- 'description' => __( 'Tax status of fee.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'enum' => array( 'taxable', 'none' ),
- ),
- 'total' => array(
- 'description' => __( 'Line total (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'total_tax' => array(
- 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'taxes' => array(
- 'description' => __( 'Line taxes.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Tax rate ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total' => array(
- 'description' => __( 'Tax total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'subtotal' => array(
- 'description' => __( 'Tax subtotal.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- 'coupon_lines' => array(
- 'description' => __( 'Coupons line data.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Item ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'code' => array(
- 'description' => __( 'Coupon code.', 'woocommerce' ),
- 'type' => 'mixed',
- 'context' => array( 'view', 'edit' ),
- ),
- 'discount' => array(
- 'description' => __( 'Discount total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- ),
- 'discount_tax' => array(
- 'description' => __( 'Discount total tax.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- 'refunds' => array(
- 'description' => __( 'List of refunds.', 'woocommerce' ),
- 'type' => 'array',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array(
- 'description' => __( 'Refund ID.', 'woocommerce' ),
- 'type' => 'integer',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'reason' => array(
- 'description' => __( 'Refund reason.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- 'total' => array(
- 'description' => __( 'Refund total.', 'woocommerce' ),
- 'type' => 'string',
- 'context' => array( 'view', 'edit' ),
- 'readonly' => true,
- ),
- ),
- ),
- ),
- ),
- );
- return $this->add_additional_fields_schema( $schema );
- }
- /**
- * Get the query params for collections.
- *
- * @return array
- */
- public function get_collection_params() {
- $params = parent::get_collection_params();
- $params['status'] = array(
- 'default' => 'any',
- 'description' => __( 'Limit result set to orders assigned a specific status.', 'woocommerce' ),
- 'type' => 'string',
- 'enum' => array_merge( array( 'any' ), $this->get_order_statuses() ),
- 'sanitize_callback' => 'sanitize_key',
- 'validate_callback' => 'rest_validate_request_arg',
- );
- $params['customer'] = array(
- 'description' => __( 'Limit result set to orders assigned a specific customer.', 'woocommerce' ),
- 'type' => 'integer',
- 'sanitize_callback' => 'absint',
- 'validate_callback' => 'rest_validate_request_arg',
- );
- $params['product'] = array(
- 'description' => __( 'Limit result set to orders assigned a specific product.', 'woocommerce' ),
- 'type' => 'integer',
- 'sanitize_callback' => 'absint',
- 'validate_callback' => 'rest_validate_request_arg',
- );
- $params['dp'] = array(
- 'default' => wc_get_price_decimals(),
- 'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ),
- 'type' => 'integer',
- 'sanitize_callback' => 'absint',
- 'validate_callback' => 'rest_validate_request_arg',
- );
- return $params;
- }
- }
|