861 lines
29 KiB
PHP
861 lines
29 KiB
PHP
<?php
|
|
/**
|
|
* REST API variations controller
|
|
*
|
|
* Handles requests to the /products/<product_id>/variations endpoints.
|
|
*
|
|
* @author WooThemes
|
|
* @category API
|
|
* @package WooCommerce/API
|
|
* @since 2.7.0
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* REST API variations controller class.
|
|
*
|
|
* @package WooCommerce/API
|
|
* @extends WC_REST_Products_Controller
|
|
*/
|
|
class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller {
|
|
|
|
/**
|
|
* Endpoint namespace.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $namespace = 'wc/v2';
|
|
|
|
/**
|
|
* Route base.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $rest_base = 'products/(?P<product_id>[\d]+)/variations';
|
|
|
|
/**
|
|
* Post type.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $post_type = 'product_variation';
|
|
|
|
/**
|
|
* Initialize product actions (parent).
|
|
*/
|
|
public function __construct() {
|
|
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'add_product_id' ), 9, 2 );
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Register the routes for products.
|
|
*/
|
|
public function register_routes() {
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
|
'args' => array(
|
|
'product_id' => array(
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
),
|
|
),
|
|
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(
|
|
'product_id' => array(
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
),
|
|
'id' => array(
|
|
'description' => __( 'Unique identifier for the variation.', '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(
|
|
'args' => array(
|
|
'product_id' => array(
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
),
|
|
),
|
|
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' ),
|
|
) );
|
|
}
|
|
|
|
/**
|
|
* Adds the parent product ID to the query so we filter / get the correct variations.
|
|
*
|
|
* @param array $args
|
|
* @param WP_REST_Request $request
|
|
* @return array
|
|
*/
|
|
public function add_product_id( $args, $request ) {
|
|
$args['post_parent'] = $request['product_id'];
|
|
return $args;
|
|
}
|
|
|
|
/**
|
|
* Prepare a single variation output for response.
|
|
*
|
|
* @param WP_Post $post Post object.
|
|
* @param WP_REST_Request $request Request object.
|
|
* @return WP_REST_Response
|
|
*/
|
|
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 ),
|
|
'menu_order' => $variation->get_menu_order(),
|
|
'meta_data' => $variation->get_meta_data(),
|
|
);
|
|
|
|
$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 );
|
|
}
|
|
|
|
/**
|
|
* Prepare a single variation for create or update.
|
|
*
|
|
* @param WP_REST_Request $request Request object.
|
|
* @return WP_Error|stdClass Post object.
|
|
*/
|
|
protected function prepare_item_for_database( $request ) {
|
|
if ( isset( $request['id'] ) ) {
|
|
$variation = wc_get_product( absint( $request['id'] ) );
|
|
} else {
|
|
$variation = new WC_Product_Variation();
|
|
}
|
|
|
|
$variation->set_parent_id( absint( $request['product_id'] ) );
|
|
|
|
/**
|
|
* Filter the query_vars used in `get_items` for the constructed query.
|
|
*
|
|
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
|
* prepared for insertion.
|
|
*
|
|
* @param WC_Product_Variation $variation An object representing a single item prepared
|
|
* for inserting or updating the database.
|
|
* @param WP_REST_Request $request Request object.
|
|
*/
|
|
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $variation, $request );
|
|
}
|
|
|
|
/**
|
|
* Add post meta fields.
|
|
*
|
|
* @param WP_Post $post
|
|
* @param WP_REST_Request $request Request data.
|
|
* @return bool
|
|
*/
|
|
protected function add_post_meta_fields( $post, $request ) {
|
|
return $this->update_post_meta_fields( $post, $request );
|
|
}
|
|
|
|
/**
|
|
* Update post meta fields.
|
|
*
|
|
* @param WP_Post $post
|
|
* @param WP_REST_Request $request Request data.
|
|
* @return bool
|
|
*/
|
|
protected function update_post_meta_fields( $post, $request ) {
|
|
$variation = wc_get_product( $post );
|
|
|
|
// Save variation meta fields.
|
|
$variation = $this->set_variation_meta( $variation, $request );
|
|
|
|
// Save the variation data.
|
|
$variation->save();
|
|
|
|
// Clear caches here so in sync with any new variations.
|
|
wc_delete_product_transients( $variation->get_parent_id() );
|
|
wp_cache_delete( 'product-' . $variation->get_parent_id(), 'products' );
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Set variation meta.
|
|
*
|
|
* @throws WC_REST_Exception REST API exceptions.
|
|
* @param WC_Product $product Product instance.
|
|
* @param WP_REST_Request $request Request data.
|
|
* @return WC_Product_Variation
|
|
*/
|
|
protected function set_variation_meta( $variation, $request ) {
|
|
// Status.
|
|
if ( isset( $request['visible'] ) ) {
|
|
$variation->set_status( false === $request['visible'] ? 'private' : 'publish' );
|
|
}
|
|
|
|
// SKU.
|
|
if ( isset( $request['sku'] ) ) {
|
|
$variation->set_sku( wc_clean( $request['sku'] ) );
|
|
}
|
|
|
|
// Thumbnail.
|
|
if ( isset( $request['image'] ) && is_array( $request['image'] ) ) {
|
|
$image = $request['image'];
|
|
$image = current( $image );
|
|
if ( is_array( $image ) ) {
|
|
$image['position'] = 0;
|
|
}
|
|
|
|
$variation = $this->set_product_images( $variation, array( $image ) );
|
|
}
|
|
|
|
// Virtual variation.
|
|
if ( isset( $request['virtual'] ) ) {
|
|
$variation->set_virtual( $request['virtual'] );
|
|
}
|
|
|
|
// Downloadable variation.
|
|
if ( isset( $request['downloadable'] ) ) {
|
|
$variation->set_downloadable( $request['downloadable'] );
|
|
}
|
|
|
|
// Downloads.
|
|
if ( $variation->get_downloadable() ) {
|
|
// Downloadable files.
|
|
if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) {
|
|
$variation = $this->save_downloadable_files( $variation, $request['downloads'] );
|
|
}
|
|
|
|
// Download limit.
|
|
if ( isset( $request['download_limit'] ) ) {
|
|
$variation->set_download_limit( $request['download_limit'] );
|
|
}
|
|
|
|
// Download expiry.
|
|
if ( isset( $request['download_expiry'] ) ) {
|
|
$variation->set_download_expiry( $request['download_expiry'] );
|
|
}
|
|
}
|
|
|
|
// Shipping data.
|
|
$variation = $this->save_product_shipping_data( $variation, $request );
|
|
|
|
// Stock handling.
|
|
if ( isset( $request['manage_stock'] ) ) {
|
|
$variation->set_manage_stock( $request['manage_stock'] );
|
|
}
|
|
|
|
if ( isset( $request['in_stock'] ) ) {
|
|
$variation->set_stock_status( true === $request['in_stock'] ? 'instock' : 'outofstock' );
|
|
}
|
|
|
|
if ( isset( $request['backorders'] ) ) {
|
|
$variation->set_backorders( $request['backorders'] );
|
|
}
|
|
|
|
if ( $variation->get_manage_stock() ) {
|
|
if ( isset( $request['stock_quantity'] ) ) {
|
|
$variation->set_stock_quantity( $request['stock_quantity'] );
|
|
} elseif ( isset( $request['inventory_delta'] ) ) {
|
|
$stock_quantity = wc_stock_amount( $variation->get_stock_quantity() );
|
|
$stock_quantity += wc_stock_amount( $request['inventory_delta'] );
|
|
$variation->set_stock_quantity( $stock_quantity );
|
|
}
|
|
} else {
|
|
$variation->set_backorders( 'no' );
|
|
$variation->set_stock_quantity( '' );
|
|
}
|
|
|
|
// Regular Price.
|
|
if ( isset( $request['regular_price'] ) ) {
|
|
$variation->set_regular_price( $request['regular_price'] );
|
|
}
|
|
|
|
// Sale Price.
|
|
if ( isset( $request['sale_price'] ) ) {
|
|
$variation->set_sale_price( $request['sale_price'] );
|
|
}
|
|
|
|
if ( isset( $request['date_on_sale_from'] ) ) {
|
|
$variation->set_date_on_sale_from( $request['date_on_sale_from'] );
|
|
}
|
|
|
|
if ( isset( $request['date_on_sale_to'] ) ) {
|
|
$variation->set_date_on_sale_to( $request['date_on_sale_to'] );
|
|
}
|
|
|
|
// Tax class.
|
|
if ( isset( $request['tax_class'] ) ) {
|
|
$variation->set_tax_class( $request['tax_class'] );
|
|
}
|
|
|
|
// Description.
|
|
if ( isset( $request['description'] ) ) {
|
|
$variation->set_description( wp_kses_post( $request['description'] ) );
|
|
}
|
|
|
|
// Update taxonomies.
|
|
if ( isset( $request['attributes'] ) ) {
|
|
$attributes = array();
|
|
$parent = wc_get_product( $variation->get_parent_id() );
|
|
$parent_attributes = $parent->get_attributes();
|
|
|
|
foreach ( $request['attributes'] as $attribute ) {
|
|
$attribute_id = 0;
|
|
$attribute_name = '';
|
|
|
|
// Check ID for global attributes or name for product attributes.
|
|
if ( ! empty( $attribute['id'] ) ) {
|
|
$attribute_id = absint( $attribute['id'] );
|
|
$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
|
|
} elseif ( ! empty( $attribute['name'] ) ) {
|
|
$attribute_name = sanitize_title( $attribute['name'] );
|
|
}
|
|
|
|
if ( ! $attribute_id && ! $attribute_name ) {
|
|
continue;
|
|
}
|
|
|
|
if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) {
|
|
continue;
|
|
}
|
|
|
|
$attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() );
|
|
$attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : '';
|
|
|
|
if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) {
|
|
// If dealing with a taxonomy, we need to get the slug from the name posted to the API.
|
|
$term = get_term_by( 'name', $attribute_value, $attribute_name );
|
|
|
|
if ( $term && ! is_wp_error( $term ) ) {
|
|
$attribute_value = $term->slug;
|
|
} else {
|
|
$attribute_value = sanitize_title( $attribute_value );
|
|
}
|
|
}
|
|
|
|
$attributes[ $attribute_key ] = $attribute_value;
|
|
}
|
|
|
|
$variation->set_attributes( $attributes );
|
|
}
|
|
|
|
// Menu order.
|
|
if ( $request['menu_order'] ) {
|
|
$variation->set_menu_order( $request['menu_order'] );
|
|
}
|
|
|
|
// Meta data.
|
|
if ( is_array( $request['meta_data'] ) ) {
|
|
foreach ( $request['meta_data'] as $meta ) {
|
|
$variation->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
|
}
|
|
}
|
|
|
|
return $variation;
|
|
}
|
|
|
|
/**
|
|
* Delete a variation.
|
|
*
|
|
* @param WP_REST_Request $request Full details about the request
|
|
* @return WP_Error|boolean
|
|
*/
|
|
public function delete_item( $request ) {
|
|
$request['id'] = absint( is_array( $request['id'] ) ? $request['id']['id'] : $request['id'] );
|
|
return parent::delete_item( $request );
|
|
}
|
|
|
|
/**
|
|
* Bulk create, update and delete items.
|
|
*
|
|
* @since 2.7.0
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
* @return array Of WP_Error or WP_REST_Response.
|
|
*/
|
|
public function batch_items( $request ) {
|
|
$items = array_filter( $request->get_params() );
|
|
$params = $request->get_url_params();
|
|
$product_id = $params['product_id'];
|
|
$body_params = array();
|
|
|
|
foreach ( array( 'update', 'create', 'delete' ) as $batch_type ) {
|
|
if ( ! empty( $items[ $batch_type ] ) ) {
|
|
$injected_items = array();
|
|
foreach ( $items[ $batch_type ] as $item ) {
|
|
$injected_items[] = is_array( $item ) ? array_merge( array( 'product_id' => $product_id ), $item ) : $item;
|
|
}
|
|
$body_params[ $batch_type ] = $injected_items;
|
|
}
|
|
}
|
|
|
|
$request = new WP_REST_Request( $request->get_method() );
|
|
$request->set_body_params( $body_params );
|
|
|
|
return parent::batch_items( $request );
|
|
}
|
|
|
|
/**
|
|
* Get the Variation's schema, conforming to JSON Schema.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_item_schema() {
|
|
$weight_unit = get_option( 'woocommerce_weight_unit' );
|
|
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
|
|
$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,
|
|
),
|
|
'date_created' => array(
|
|
'description' => __( "The date the variation was created, in the site's timezone.", 'woocommerce' ),
|
|
'type' => 'date-time',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'date_modified' => array(
|
|
'description' => __( "The date the variation was last modified, in the site's timezone.", 'woocommerce' ),
|
|
'type' => 'date-time',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'description' => array(
|
|
'description' => __( 'Variation description.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'permalink' => array(
|
|
'description' => __( 'Variation URL.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'format' => 'uri',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'sku' => array(
|
|
'description' => __( 'Unique identifier.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'price' => array(
|
|
'description' => __( 'Current variation price.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'regular_price' => array(
|
|
'description' => __( 'Variation regular price.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'sale_price' => array(
|
|
'description' => __( 'Variation sale price.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'date_on_sale_from' => array(
|
|
'description' => __( 'Start date of sale price.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'date_on_sale_to' => array(
|
|
'description' => __( 'End data of sale price.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'on_sale' => array(
|
|
'description' => __( 'Shows if the variation is on sale.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'visible' => array(
|
|
'description' => __( "Define if the attribute is visible on the \"Additional information\" tab in the product's page.", 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'default' => true,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'purchasable' => array(
|
|
'description' => __( 'Shows if the variation can be bought.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'virtual' => array(
|
|
'description' => __( 'If the variation is virtual.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'default' => false,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'downloadable' => array(
|
|
'description' => __( 'If the variation is downloadable.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'default' => false,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'downloads' => array(
|
|
'description' => __( 'List of downloadable files.', 'woocommerce' ),
|
|
'type' => 'array',
|
|
'context' => array( 'view', 'edit' ),
|
|
'items' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'id' => array(
|
|
'description' => __( 'File MD5 hash.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'name' => array(
|
|
'description' => __( 'File name.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'file' => array(
|
|
'description' => __( 'File URL.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
'download_limit' => array(
|
|
'description' => __( 'Amount of times the variation can be downloaded.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'default' => -1,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'download_expiry' => array(
|
|
'description' => __( 'Number of days that the customer has up to be able to download the variation.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'default' => -1,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'tax_status' => array(
|
|
'description' => __( 'Tax status.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'default' => 'taxable',
|
|
'enum' => array( 'taxable', 'shipping', 'none' ),
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'tax_class' => array(
|
|
'description' => __( 'Tax class.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'manage_stock' => array(
|
|
'description' => __( 'Stock management at variation level.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'default' => false,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'stock_quantity' => array(
|
|
'description' => __( 'Stock quantity.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'in_stock' => array(
|
|
'description' => __( 'Controls whether or not the variation is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'default' => true,
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'backorders' => array(
|
|
'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'default' => 'no',
|
|
'enum' => array( 'no', 'notify', 'yes' ),
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'backorders_allowed' => array(
|
|
'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'backordered' => array(
|
|
'description' => __( 'Shows if the variation is on backordered.', 'woocommerce' ),
|
|
'type' => 'boolean',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'weight' => array(
|
|
/* translators: %s: weight unit */
|
|
'description' => sprintf( __( 'Variation weight (%s).', 'woocommerce' ), $weight_unit ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'dimensions' => array(
|
|
'description' => __( 'Variation dimensions.', 'woocommerce' ),
|
|
'type' => 'object',
|
|
'context' => array( 'view', 'edit' ),
|
|
'properties' => array(
|
|
'length' => array(
|
|
/* translators: %s: dimension unit */
|
|
'description' => sprintf( __( 'Variation length (%s).', 'woocommerce' ), $dimension_unit ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'width' => array(
|
|
/* translators: %s: dimension unit */
|
|
'description' => sprintf( __( 'Variation width (%s).', 'woocommerce' ), $dimension_unit ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'height' => array(
|
|
/* translators: %s: dimension unit */
|
|
'description' => sprintf( __( 'Variation height (%s).', 'woocommerce' ), $dimension_unit ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
),
|
|
'shipping_class' => array(
|
|
'description' => __( 'Shipping class slug.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'shipping_class_id' => array(
|
|
'description' => __( 'Shipping class ID.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'image' => array(
|
|
'description' => __( 'Variation image data.', 'woocommerce' ),
|
|
'type' => 'object',
|
|
'context' => array( 'view', 'edit' ),
|
|
'properties' => array(
|
|
'id' => array(
|
|
'description' => __( 'Image ID.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'date_created' => array(
|
|
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
|
|
'type' => 'date-time',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'date_modified' => array(
|
|
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
|
|
'type' => 'date-time',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'src' => array(
|
|
'description' => __( 'Image URL.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'format' => 'uri',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'name' => array(
|
|
'description' => __( 'Image name.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'alt' => array(
|
|
'description' => __( 'Image alternative text.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'position' => array(
|
|
'description' => __( 'Image position. 0 means that the image is featured.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
),
|
|
'attributes' => array(
|
|
'description' => __( 'List of attributes.', 'woocommerce' ),
|
|
'type' => 'array',
|
|
'context' => array( 'view', 'edit' ),
|
|
'items' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'id' => array(
|
|
'description' => __( 'Attribute ID.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'name' => array(
|
|
'description' => __( 'Attribute name.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'option' => array(
|
|
'description' => __( 'Selected attribute term name.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
'menu_order' => array(
|
|
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'meta_data' => array(
|
|
'description' => __( 'Meta data.', 'woocommerce' ),
|
|
'type' => 'array',
|
|
'context' => array( 'view', 'edit' ),
|
|
'items' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'id' => array(
|
|
'description' => __( 'Meta ID.', 'woocommerce' ),
|
|
'type' => 'integer',
|
|
'context' => array( 'view', 'edit' ),
|
|
'readonly' => true,
|
|
),
|
|
'key' => array(
|
|
'description' => __( 'Meta key.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
'value' => array(
|
|
'description' => __( 'Meta value.', 'woocommerce' ),
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
|
}
|
|
|
|
/**
|
|
* Prepare links for the request.
|
|
*
|
|
* @param WC_Product $product Product object.
|
|
* @param WP_REST_Request $request Request object.
|
|
* @return array Links for the given product.
|
|
*/
|
|
protected function prepare_links( $variation, $request ) {
|
|
$product_id = (int) $request['product_id'];
|
|
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
|
$links = array(
|
|
'self' => array(
|
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $variation->get_id() ) ),
|
|
),
|
|
'collection' => array(
|
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
|
),
|
|
'up' => array(
|
|
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ),
|
|
),
|
|
);
|
|
return $links;
|
|
}
|
|
|
|
}
|