2016-09-29 16:24:01 +00:00
< ? php
/**
* REST API variations controller
*
* Handles requests to the / products /< product_id >/ variations endpoints .
*
2018-03-06 18:04:58 +00:00
* @ package WooCommerce\API
* @ since 3.0 . 0
2016-09-29 16:24:01 +00:00
*/
2018-03-06 18:04:58 +00:00
defined ( 'ABSPATH' ) || exit ;
2016-09-29 16:24:01 +00:00
/**
* REST API variations controller class .
*
* @ package WooCommerce / API
* @ extends WC_REST_Products_Controller
*/
class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller {
/**
2017-10-17 16:22:30 +00:00
* Endpoint namespace .
2016-10-04 17:36:18 +00:00
*
* @ var string
*/
2017-02-09 17:06:13 +00:00
protected $namespace = 'wc/v2' ;
2016-09-29 16:24:01 +00:00
/**
2016-10-04 17:36:18 +00:00
* Route base .
*
* @ var string
*/
2016-09-29 16:24:01 +00:00
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 () {
2018-02-22 19:00:21 +00:00
register_rest_route (
$this -> namespace , '/' . $this -> rest_base , array (
'args' => array (
'product_id' => array (
'description' => __ ( 'Unique identifier for the variable product.' , 'woocommerce' ),
'type' => 'integer' ,
),
2017-01-26 20:06:18 +00:00
),
2018-02-22 19:00:21 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
2017-01-26 20:06:18 +00:00
),
2018-02-22 19:00:21 +00:00
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 ),
2017-01-26 19:22:57 +00:00
),
2018-02-22 19:00:21 +00:00
'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' ,
),
2016-09-29 16:24:01 +00:00
),
2018-02-22 19:00:21 +00:00
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' ,
)
),
2016-09-29 16:24:01 +00:00
),
),
2018-02-22 19:00:21 +00:00
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' ),
),
),
2017-01-26 20:06:18 +00:00
),
2018-02-22 19:00:21 +00:00
'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' ),
)
);
2016-09-29 16:24:01 +00:00
}
/**
2017-02-17 00:51:36 +00:00
* Get object .
2016-09-29 16:24:01 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-17 00:51:36 +00:00
* @ param int $id Object ID .
* @ return WC_Data
2016-09-29 16:24:01 +00:00
*/
2017-02-17 00:51:36 +00:00
protected function get_object ( $id ) {
return wc_get_product ( $id );
2016-09-29 16:24:01 +00:00
}
2018-02-22 19:22:42 +00:00
/**
* Check if a given request has access to update an item .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function update_item_permissions_check ( $request ) {
$object = $this -> get_object ( ( int ) $request [ 'id' ] );
if ( $object && 0 !== $object -> get_id () && ! wc_rest_check_post_permissions ( $this -> post_type , 'edit' , $object -> get_id () ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_edit' , __ ( 'Sorry, you are not allowed to edit this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
// Check if variation belongs to the correct parent product.
2018-02-27 16:32:59 +00:00
if ( $object && 0 !== $object -> get_parent_id () && absint ( $request [ 'product_id' ] ) !== $object -> get_parent_id () ) {
2018-02-22 19:22:42 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_edit' , __ ( 'Parent product does not match current variation.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
2016-09-29 16:24:01 +00:00
/**
* Prepare a single variation output for response .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-17 00:51:36 +00:00
* @ param WC_Data $object Object data .
* @ param WP_REST_Request $request Request object .
2017-02-16 05:07:51 +00:00
* @ return WP_REST_Response
2016-09-29 16:24:01 +00:00
*/
2017-02-17 00:51:36 +00:00
public function prepare_object_for_response ( $object , $request ) {
$data = array (
2017-03-13 20:26:52 +00:00
'id' => $object -> get_id (),
'date_created' => wc_rest_prepare_date_response ( $object -> get_date_created (), false ),
'date_created_gmt' => wc_rest_prepare_date_response ( $object -> get_date_created () ),
'date_modified' => wc_rest_prepare_date_response ( $object -> get_date_modified (), false ),
'date_modified_gmt' => wc_rest_prepare_date_response ( $object -> get_date_modified () ),
2017-04-06 16:02:51 +00:00
'description' => wc_format_content ( $object -> get_description () ),
2017-03-13 20:26:52 +00:00
'permalink' => $object -> get_permalink (),
'sku' => $object -> get_sku (),
'price' => $object -> get_price (),
'regular_price' => $object -> get_regular_price (),
'sale_price' => $object -> get_sale_price (),
'date_on_sale_from' => wc_rest_prepare_date_response ( $object -> get_date_on_sale_from (), false ),
'date_on_sale_from_gmt' => wc_rest_prepare_date_response ( $object -> get_date_on_sale_from () ),
'date_on_sale_to' => wc_rest_prepare_date_response ( $object -> get_date_on_sale_to (), false ),
'date_on_sale_to_gmt' => wc_rest_prepare_date_response ( $object -> get_date_on_sale_to () ),
'on_sale' => $object -> is_on_sale (),
'visible' => $object -> is_visible (),
'purchasable' => $object -> is_purchasable (),
'virtual' => $object -> is_virtual (),
'downloadable' => $object -> is_downloadable (),
'downloads' => $this -> get_downloads ( $object ),
'download_limit' => '' !== $object -> get_download_limit () ? ( int ) $object -> get_download_limit () : - 1 ,
'download_expiry' => '' !== $object -> get_download_expiry () ? ( int ) $object -> get_download_expiry () : - 1 ,
'tax_status' => $object -> get_tax_status (),
'tax_class' => $object -> get_tax_class (),
'manage_stock' => $object -> managing_stock (),
'stock_quantity' => $object -> get_stock_quantity (),
'in_stock' => $object -> is_in_stock (),
'backorders' => $object -> get_backorders (),
'backorders_allowed' => $object -> backorders_allowed (),
'backordered' => $object -> is_on_backorder (),
'weight' => $object -> get_weight (),
'dimensions' => array (
2018-02-22 19:00:21 +00:00
'length' => $object -> get_length (),
'width' => $object -> get_width (),
'height' => $object -> get_height (),
2016-09-29 16:24:01 +00:00
),
2017-03-13 20:26:52 +00:00
'shipping_class' => $object -> get_shipping_class (),
'shipping_class_id' => $object -> get_shipping_class_id (),
2017-03-23 03:38:52 +00:00
'image' => current ( $this -> get_images ( $object ) ),
2017-03-13 20:26:52 +00:00
'attributes' => $this -> get_attributes ( $object ),
'menu_order' => $object -> get_menu_order (),
'meta_data' => $object -> get_meta_data (),
2016-09-29 16:24:01 +00:00
);
2017-02-17 00:51:36 +00:00
$context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ;
$data = $this -> add_additional_fields_to_object ( $data , $request );
$data = $this -> filter_response_by_context ( $data , $context );
2016-09-29 16:24:01 +00:00
$response = rest_ensure_response ( $data );
2017-02-17 00:51:36 +00:00
$response -> add_links ( $this -> prepare_links ( $object , $request ) );
2016-09-29 16:24:01 +00:00
/**
* Filter the data for a response .
*
2017-02-17 00:51:36 +00:00
* The dynamic portion of the hook name , $this -> post_type ,
* refers to object type being prepared for the response .
2016-09-29 16:24:01 +00:00
*
2017-02-17 00:51:36 +00:00
* @ param WP_REST_Response $response The response object .
* @ param WC_Data $object Object data .
* @ param WP_REST_Request $request Request object .
2016-09-29 16:24:01 +00:00
*/
2017-02-17 00:51:36 +00:00
return apply_filters ( " woocommerce_rest_prepare_ { $this -> post_type } _object " , $response , $object , $request );
2016-09-29 16:24:01 +00:00
}
2017-02-16 05:07:51 +00:00
/**
2017-02-17 00:51:36 +00:00
* Prepare objects query .
2017-02-16 05:07:51 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-02-17 00:51:36 +00:00
* @ param WP_REST_Request $request Full details about the request .
* @ return array
2016-09-29 16:24:01 +00:00
*/
2017-02-17 00:51:36 +00:00
protected function prepare_objects_query ( $request ) {
$args = parent :: prepare_objects_query ( $request );
2017-02-16 05:07:51 +00:00
2017-02-17 00:51:36 +00:00
$args [ 'post_parent' ] = $request [ 'product_id' ];
2018-06-23 11:55:42 +00:00
$taxonomies = isset ( $args [ 'tax_query' ] ) ? $args [ 'tax_query' ] : array ();
2018-06-05 11:22:22 +00:00
2018-07-09 15:14:12 +00:00
$parent_query = new WC_REST_Products_Controller ();
$parent_request = clone $request ;
$parent_request [ 'include' ] = array ( $request [ 'product_id' ] );
// Shipping class can be set separately for variations and parent product. Unlike for attributes, the parent
// does not have all shipping classes of the variations assigned to it, so the parent filtering should ignore
// shipping class.
$parent_request [ 'shipping_class' ] = '' ;
$query_args = $parent_query -> prepare_objects_query ( $parent_request );
$query_results = $parent_query -> get_objects ( $query_args );
2018-06-05 11:22:22 +00:00
2018-07-09 15:14:12 +00:00
// In case the parent does not fulfil the constraints, no variations should be returned.
if ( 0 === $query_results [ 'total' ] ) {
$args [ 'post_parent' ] = 0 ;
}
2018-06-05 11:22:22 +00:00
2018-07-09 15:14:12 +00:00
// Ignore taxonomy queries, as they were already matched against parent (or will be transformed to meta query
// later on).
unset ( $args [ 'tax_query' ] );
2018-06-05 11:22:22 +00:00
2018-06-18 08:15:36 +00:00
$GLOBALS [ 'wpdb' ] -> query ( 'SET SESSION SQL_BIG_SELECTS=1' );
2018-07-09 15:14:12 +00:00
// Attributes needs to be transformed to meta query, shipping class to taxonomy.
2018-06-05 11:22:22 +00:00
foreach ( $taxonomies as $taxonomy ) {
2018-07-09 15:14:12 +00:00
// Ignore taxonomies tied to parent product.
if ( in_array ( $taxonomy [ 'taxonomy' ], array ( 'product_type' , 'product_visibility' , 'product_cat' , 'product_tag' ), true ) ) {
2018-06-05 11:22:22 +00:00
continue ;
}
2018-07-09 15:14:12 +00:00
// Shipping class can be associated with either the parent or the individual variation.
if ( 'product_shipping_class' === $taxonomy [ 'taxonomy' ] ) {
// If requested shipping class is different from parent's shipping class, it's assigned to variation,
// so passing the taxonomy query forward to check against variations.
if ( $query_results [ 'total' ] > 0 && $taxonomy [ 'terms' ][ 0 ] !== $query_results [ 'objects' ][ 0 ] -> get_shipping_class_id () ) {
$args [ 'tax_query' ][] = $taxonomy ;
continue ;
} else {
// In case the request should to return variations with shipping class equal to parent's shipping class,
// there are no records in taxonomy relationship table.
$args [ 'tax_query' ][] = array (
'taxonomy' => 'product_shipping_class' ,
'operator' => 'NOT EXISTS' ,
);
continue ;
}
}
// Attributes switched to meta query.
2018-06-05 11:22:22 +00:00
if ( 'term_id' === $taxonomy [ 'field' ] ) {
2018-07-09 15:14:12 +00:00
$terms = wc_get_product_terms ( $request [ 'product_id' ], $taxonomy [ 'taxonomy' ], array ( 'fields' => 'slugs' ) );
2018-06-05 11:22:22 +00:00
$value = isset ( $terms [ $taxonomy [ 'terms' ][ 0 ] ] ) ? $terms [ $taxonomy [ 'terms' ][ 0 ] ] : null ;
} else {
$value = $taxonomy [ 'terms' ][ 0 ];
}
$args [ 'meta_query' ] = $this -> add_meta_query ( // WPCS: slow query ok.
$args , array (
'key' => 'attribute_' . $taxonomy [ 'taxonomy' ],
'value' => $value ,
)
);
}
2017-02-16 05:07:51 +00:00
2017-02-17 00:51:36 +00:00
return $args ;
2016-09-29 16:24:01 +00:00
}
/**
2017-02-17 00:51:36 +00:00
* Prepare a single variation for create or update .
2016-09-29 16:24:01 +00:00
*
2017-02-17 00:51:36 +00:00
* @ param WP_REST_Request $request Request object .
* @ param bool $creating If is creating a new object .
* @ return WP_Error | WC_Data
2016-09-29 16:24:01 +00:00
*/
2017-02-17 00:51:36 +00:00
protected function prepare_object_for_database ( $request , $creating = false ) {
if ( isset ( $request [ 'id' ] ) ) {
$variation = wc_get_product ( absint ( $request [ 'id' ] ) );
} else {
$variation = new WC_Product_Variation ();
}
2018-02-22 19:22:42 +00:00
// Update parent ID just once.
if ( 0 === $variation -> get_parent_id () ) {
$variation -> set_parent_id ( absint ( $request [ 'product_id' ] ) );
}
2017-02-17 00:51:36 +00:00
2017-02-16 05:07:51 +00:00
// 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.
2017-04-06 09:41:09 +00:00
if ( isset ( $request [ 'image' ] ) ) {
2018-05-04 13:47:39 +00:00
if ( is_array ( $request [ 'image' ] ) && ! empty ( $request [ 'image' ] ) ) {
2017-04-06 09:41:09 +00:00
$image = $request [ 'image' ];
if ( is_array ( $image ) ) {
$image [ 'position' ] = 0 ;
}
2017-02-16 05:07:51 +00:00
2017-04-06 09:41:09 +00:00
$variation = $this -> set_product_images ( $variation , array ( $image ) );
} else {
$variation -> set_image_id ( '' );
}
2017-02-16 05:07:51 +00:00
}
// 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' ] ) ) {
2018-04-03 16:46:43 +00:00
if ( 'parent' === $request [ 'manage_stock' ] ) {
$variation -> set_manage_stock ( false ); // This just indicates the variation does not manage stock, but the parent does.
} else {
$variation -> set_manage_stock ( wc_string_to_bool ( $request [ 'manage_stock' ] ) );
}
2016-09-29 16:24:01 +00:00
}
2017-02-16 05:07:51 +00:00
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' ] );
}
2017-03-13 21:36:31 +00:00
if ( isset ( $request [ 'date_on_sale_from_gmt' ] ) ) {
$variation -> set_date_on_sale_from ( $request [ 'date_on_sale_from_gmt' ] ? strtotime ( $request [ 'date_on_sale_from_gmt' ] ) : null );
}
2017-02-16 05:07:51 +00:00
if ( isset ( $request [ 'date_on_sale_to' ] ) ) {
$variation -> set_date_on_sale_to ( $request [ 'date_on_sale_to' ] );
}
2017-03-13 21:36:31 +00:00
if ( isset ( $request [ 'date_on_sale_to_gmt' ] ) ) {
$variation -> set_date_on_sale_to ( $request [ 'date_on_sale_to_gmt' ] ? strtotime ( $request [ 'date_on_sale_to_gmt' ] ) : null );
}
2017-02-16 05:07:51 +00:00
// 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' ] ) ) {
2018-06-19 11:54:57 +00:00
$attributes = array ();
$parent = wc_get_product ( $variation -> get_parent_id () );
if ( ! $parent ) {
return new WP_Error (
// Translators: %d parent ID.
" woocommerce_rest_ { $this -> post_type } _invalid_parent " , sprintf ( __ ( 'Cannot set attributes due to invalid parent product.' , 'woocommerce' ), $variation -> get_parent_id () ), array (
'status' => 404 ,
)
);
}
2017-02-16 05:07:51 +00:00
$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' ] ) ) {
2018-01-11 15:11:36 +00:00
$attribute_id = absint ( $attribute [ 'id' ] );
$raw_attribute_name = wc_attribute_taxonomy_name_by_id ( $attribute_id );
2017-02-16 05:07:51 +00:00
} elseif ( ! empty ( $attribute [ 'name' ] ) ) {
2018-01-11 15:11:36 +00:00
$raw_attribute_name = sanitize_title ( $attribute [ 'name' ] );
2017-02-16 05:07:51 +00:00
}
2018-01-11 15:11:36 +00:00
if ( ! $attribute_id && ! $raw_attribute_name ) {
2017-02-16 05:07:51 +00:00
continue ;
}
2018-01-11 15:11:36 +00:00
$attribute_name = sanitize_title ( $raw_attribute_name );
2017-02-16 05:07:51 +00:00
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.
2018-01-11 15:11:36 +00:00
$term = get_term_by ( 'name' , $attribute_value , $raw_attribute_name ); // @codingStandardsIgnoreLine
2017-02-16 05:07:51 +00:00
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' ] : '' );
}
}
2017-02-17 00:51:36 +00:00
/**
* Filters an object before it is inserted via the REST API .
*
* The dynamic portion of the hook name , `$this->post_type` ,
* refers to the object type slug .
*
* @ param WC_Data $variation Object object .
* @ param WP_REST_Request $request Request object .
* @ param bool $creating If is creating a new object .
*/
return apply_filters ( " woocommerce_rest_pre_insert_ { $this -> post_type } _object " , $variation , $request , $creating );
}
/**
* Clear caches here so in sync with any new variations .
*
* @ param WC_Data $object Object data .
*/
public function clear_transients ( $object ) {
wc_delete_product_transients ( $object -> get_parent_id () );
wp_cache_delete ( 'product-' . $object -> get_parent_id (), 'products' );
2016-09-29 16:24:01 +00:00
}
/**
* Delete a variation .
*
2017-10-17 16:22:30 +00:00
* @ param WP_REST_Request $request Full details about the request .
2017-05-15 11:50:52 +00:00
*
* @ return bool | WP_Error | WP_REST_Response
2016-09-29 16:24:01 +00:00
*/
public function delete_item ( $request ) {
2017-02-17 02:10:25 +00:00
$force = ( bool ) $request [ 'force' ];
$object = $this -> get_object ( ( int ) $request [ 'id' ] );
$result = false ;
if ( ! $object || 0 === $object -> get_id () ) {
2018-02-22 19:00:21 +00:00
return new WP_Error (
" woocommerce_rest_ { $this -> post_type } _invalid_id " , __ ( 'Invalid ID.' , 'woocommerce' ), array (
'status' => 404 ,
)
);
2017-02-17 02:10:25 +00:00
}
$supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable ( array ( $object , 'get_status' ) );
/**
* Filter whether an object is trashable .
*
* Return false to disable trash support for the object .
*
* @ param boolean $supports_trash Whether the object type support trashing .
* @ param WC_Data $object The object being considered for trashing support .
*/
$supports_trash = apply_filters ( " woocommerce_rest_ { $this -> post_type } _object_trashable " , $supports_trash , $object );
if ( ! wc_rest_check_post_permissions ( $this -> post_type , 'delete' , $object -> get_id () ) ) {
2018-02-22 19:00:21 +00:00
return new WP_Error (
/* translators: %s: post type */
" woocommerce_rest_user_cannot_delete_ { $this -> post_type } " , sprintf ( __ ( 'Sorry, you are not allowed to delete %s.' , 'woocommerce' ), $this -> post_type ), array (
'status' => rest_authorization_required_code (),
)
);
2017-02-17 02:10:25 +00:00
}
$request -> set_param ( 'context' , 'edit' );
$response = $this -> prepare_object_for_response ( $object , $request );
// If we're forcing, then delete permanently.
if ( $force ) {
$object -> delete ( true );
$result = 0 === $object -> get_id ();
} else {
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
2018-02-22 19:00:21 +00:00
return new WP_Error (
/* translators: %s: post type */
'woocommerce_rest_trash_not_supported' , sprintf ( __ ( 'The %s does not support trashing.' , 'woocommerce' ), $this -> post_type ), array (
'status' => 501 ,
)
);
2017-02-17 02:10:25 +00:00
}
// Otherwise, only trash if we haven't already.
if ( is_callable ( array ( $object , 'get_status' ) ) ) {
if ( 'trash' === $object -> get_status () ) {
2018-02-22 19:00:21 +00:00
return new WP_Error (
/* translators: %s: post type */
'woocommerce_rest_already_trashed' , sprintf ( __ ( 'The %s has already been deleted.' , 'woocommerce' ), $this -> post_type ), array (
'status' => 410 ,
)
);
2017-02-17 02:10:25 +00:00
}
$object -> delete ();
$result = 'trash' === $object -> get_status ();
}
}
if ( ! $result ) {
2018-02-22 19:00:21 +00:00
return new WP_Error (
/* translators: %s: post type */
'woocommerce_rest_cannot_delete' , sprintf ( __ ( 'The %s cannot be deleted.' , 'woocommerce' ), $this -> post_type ), array (
'status' => 500 ,
)
);
2017-02-17 02:10:25 +00:00
}
// Delete parent product transients.
if ( 0 !== $object -> get_parent_id () ) {
wc_delete_product_transients ( $object -> get_parent_id () );
}
/**
* Fires after a single object is deleted or trashed via the REST API .
*
* @ param WC_Data $object The deleted or trashed object .
* @ param WP_REST_Response $response The response data .
* @ param WP_REST_Request $request The request sent to the API .
*/
do_action ( " woocommerce_rest_delete_ { $this -> post_type } _object " , $object , $response , $request );
return $response ;
2016-09-29 16:24:01 +00:00
}
/**
* Bulk create , update and delete items .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-09-29 16:24:01 +00:00
* @ 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 ) {
2018-02-22 19:00:21 +00:00
$injected_items [] = is_array ( $item ) ? array_merge (
array (
'product_id' => $product_id ,
), $item
) : $item ;
2016-09-29 16:24:01 +00:00
}
$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 );
}
2017-02-17 00:51:36 +00:00
/**
* Prepare links for the request .
*
* @ param WC_Data $object Object data .
* @ param WP_REST_Request $request Request object .
* @ return array Links for the given post .
*/
protected function prepare_links ( $object , $request ) {
$product_id = ( int ) $request [ 'product_id' ];
$base = str_replace ( '(?P<product_id>[\d]+)' , $product_id , $this -> rest_base );
$links = array (
2018-02-22 19:00:21 +00:00
'self' => array (
2017-02-17 00:51:36 +00:00
'href' => rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $base , $object -> get_id () ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $base ) ),
),
2018-02-22 19:00:21 +00:00
'up' => array (
2017-02-17 00:51:36 +00:00
'href' => rest_url ( sprintf ( '/%s/products/%d' , $this -> namespace , $product_id ) ),
),
);
return $links ;
}
2016-09-29 16:24:01 +00:00
/**
* 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 (
2018-02-22 19:00:21 +00:00
'id' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'date_created' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( " The date the variation was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'date_modified' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( " The date the variation was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'description' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation description.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'permalink' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation URL.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'sku' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Unique identifier.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'price' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Current variation price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'regular_price' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation regular price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'sale_price' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'date_on_sale_from' => array (
2017-03-13 21:36:31 +00:00
'description' => __ ( " Start date of sale price, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
),
'date_on_sale_from_gmt' => array (
'description' => __ ( 'Start date of sale price, as GMT.' , 'woocommerce' ),
'type' => 'date-time' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'date_on_sale_to' => array (
2017-03-13 21:36:31 +00:00
'description' => __ ( " End date of sale price, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'date_on_sale_to_gmt' => array (
2018-01-30 17:03:01 +00:00
'description' => __ ( 'End date of sale price, as GMT.' , 'woocommerce' ),
2017-03-13 21:36:31 +00:00
'type' => 'date-time' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'on_sale' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shows if the variation is on sale.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'visible' => array (
2017-10-17 16:22:56 +00:00
'description' => __ ( " Define if the variation is visible on the product's page. " , 'woocommerce' ),
2016-09-29 16:24:01 +00:00
'type' => 'boolean' ,
WIP - Product CRUD (#12065)
* Created function to get the catalog visibility options
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Sanitize ids later
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Bump template version
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* Made abstract function useful
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* Allow WP_Query to preload post data, and meta in wc_get_products()
Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Sanitize ids later
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Bump template version
* Made abstract function useful
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* Prevent notices
* Handle image_id from parent
* Fix error
* Remove _wc_save_product_price
* Remove todo
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* [Product CRUD] Abstract todos (#12305)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Revert "[Product CRUD] Abstract todos (#12305)"
This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2.
* Remove deprecated get_variation_id()
* New default attributes method
* [Product CRUD] Product Datastore (#12317)
* Fix up tests in the product/* folder.
* Handle data store updates for grouped, variable, external, simple, and general data store updates for products.
* Variations & variable changes.
* Update -functions.php calls to use data store.
* Add an interface for the public product data store methods.
* Finished product factory tests
* Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods.
* Fix up delete in all versions of the api
* Handle feedback
* Match protected decloration to parent
* Product crud abstract todos (#12316)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Fix up store
* Fixed method returning in write context
* Fix error in variation admin
* Check for parent value - fixes tax class
* Remove old/complete todos
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* Remove deprecated get_variation_id()
* New default attributes method
* Fixed method returning in write context
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed coding standards
* TODO is not accurate.
* Should pass WC_Product instancies to WC_Comments methods (#12327)
* Use new method in abstract order class to prevent headers sent issue in tests
* Fixed variable description in REST API
* Updated how create initial product variation
* Fixed a few fatal errors and warnings in Products CRUD (#12329)
* Fixed a few fatal errors and warnings in Products CRUD
* Fixed sync functions
* Add variations CRUD to legacy API (#12331)
* Apply crud to variable products in legacy API v1
* New REST API do not need fallback for default attributes
* Apply variations CRUD to legacy API v2
* Legacy v2 - save default attributes
* Variations in legacy API v2 do not have descriptions
* Fixed legacy API v2 variations params
* Applied variations CRUD to legacy API v3
* Sync before save in legacy apis
* Punc
* Removed API todos
* Removed test
* Products endpoint tweaks (#12354)
* Var type already normalized on CRUD
* Let Product CRUD handle with validation, sanitization and conditional checks
* Set downloads using WC_Product_Download
* Stop try catch exceptions more than one time
* Handle WC_Data_Exception in legacy API
* Complete remove products when fails on creating
* On creating I mean!
* Already have a method to complete delete products
* Fixed standards using WP CodeSniffer
* get_the_terms() returns false when empty
* get_manage_stock returns boolean
@claudiosanches
* Merge conflict
* Variations API endpoint fixes
* Product CRUD improvements (#12359)
* args is not used any more - remove todo
* Added test for attributes
* wc_get_price_excluding_tax usage
* parent usage
* Fix rating counts
* Test fixes
* Cleanup after tests
* Make sure status transition code runs even during API calls, not just in admin.
* Default visibility
* Fix attribute setting in API
* Use get name instead of get title
* variation id usage
* Improved cross sell templates
* variation_data
* Grouped product sync
* Notices
* Sync is not needed in API
* Delete
* Rename interfaces
* Update counts in data store
2016-11-16 12:38:24 +00:00
'default' => true ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'purchasable' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shows if the variation can be bought.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'virtual' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'If the variation is virtual.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'downloadable' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'If the variation is downloadable.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'downloads' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'List of downloadable files.' , 'woocommerce' ),
2016-12-07 12:20:56 +00:00
'type' => 'array' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
2016-12-07 14:24:44 +00:00
'items' => array (
'type' => 'object' ,
'properties' => array (
2018-02-22 19:00:21 +00:00
'id' => array (
2016-12-07 14:24:44 +00:00
'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' ),
),
2016-09-29 16:24:01 +00:00
),
),
),
2018-02-22 19:00:21 +00:00
'download_limit' => array (
2017-03-20 16:01:55 +00:00
'description' => __ ( 'Number of times downloadable files can be downloaded after purchase.' , 'woocommerce' ),
2016-09-29 16:24:01 +00:00
'type' => 'integer' ,
'default' => - 1 ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'download_expiry' => array (
2017-03-20 16:01:55 +00:00
'description' => __ ( 'Number of days until access to downloadable files expires.' , 'woocommerce' ),
2016-09-29 16:24:01 +00:00
'type' => 'integer' ,
'default' => - 1 ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'tax_status' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Tax status.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'taxable' ,
'enum' => array ( 'taxable' , 'shipping' , 'none' ),
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'tax_class' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Tax class.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'manage_stock' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Stock management at variation level.' , 'woocommerce' ),
2018-04-03 16:46:43 +00:00
'type' => 'mixed' ,
2016-09-29 16:24:01 +00:00
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'stock_quantity' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Stock quantity.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'in_stock' => array (
2016-09-29 16:24:01 +00:00
'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' ),
),
2018-02-22 19:00:21 +00:00
'backorders' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'If managing stock, this controls if backorders are allowed.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'no' ,
'enum' => array ( 'no' , 'notify' , 'yes' ),
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'backorders_allowed' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shows if backorders are allowed.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'backordered' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shows if the variation is on backordered.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'weight' => array (
2016-10-29 17:32:38 +00:00
/* translators: %s: weight unit */
2016-09-29 16:24:01 +00:00
'description' => sprintf ( __ ( 'Variation weight (%s).' , 'woocommerce' ), $weight_unit ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'dimensions' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation dimensions.' , 'woocommerce' ),
2016-12-07 11:36:46 +00:00
'type' => 'object' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'length' => array (
2016-10-29 17:32:38 +00:00
/* translators: %s: dimension unit */
2016-09-29 16:24:01 +00:00
'description' => sprintf ( __ ( 'Variation length (%s).' , 'woocommerce' ), $dimension_unit ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'width' => array (
2016-10-29 17:32:38 +00:00
/* translators: %s: dimension unit */
2016-09-29 16:24:01 +00:00
'description' => sprintf ( __ ( 'Variation width (%s).' , 'woocommerce' ), $dimension_unit ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'height' => array (
2016-10-29 17:32:38 +00:00
/* translators: %s: dimension unit */
2016-09-29 16:24:01 +00:00
'description' => sprintf ( __ ( 'Variation height (%s).' , 'woocommerce' ), $dimension_unit ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
2018-02-22 19:00:21 +00:00
'shipping_class' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shipping class slug.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'shipping_class_id' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Shipping class ID.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'image' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Variation image data.' , 'woocommerce' ),
2016-12-07 11:36:46 +00:00
'type' => 'object' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2018-02-22 19:00:21 +00:00
'id' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Image ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'date_created' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( " The date the image was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'date_created_gmt' => array (
2017-03-22 21:22:10 +00:00
'description' => __ ( 'The date the image was created, as GMT.' , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'date_modified' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( " The date the image was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2017-03-22 21:22:10 +00:00
'date_modified_gmt' => array (
'description' => __ ( 'The date the image was last modified, as GMT.' , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'src' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Image URL.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'name' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Image name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'alt' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Image alternative text.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'position' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'Image position. 0 means that the image is featured.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
2018-02-22 19:00:21 +00:00
'attributes' => array (
2016-09-29 16:24:01 +00:00
'description' => __ ( 'List of attributes.' , 'woocommerce' ),
2016-12-07 12:20:56 +00:00
'type' => 'array' ,
2016-09-29 16:24:01 +00:00
'context' => array ( 'view' , 'edit' ),
2016-12-07 14:24:44 +00:00
'items' => array (
'type' => 'object' ,
'properties' => array (
2018-02-22 19:00:21 +00:00
'id' => array (
2016-12-07 14:24:44 +00:00
'description' => __ ( 'Attribute ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'name' => array (
2016-12-07 14:24:44 +00:00
'description' => __ ( 'Attribute name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'option' => array (
'description' => __ ( 'Selected attribute term name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-09-29 16:24:01 +00:00
),
),
),
2018-02-22 19:00:21 +00:00
'menu_order' => array (
2017-02-16 05:07:51 +00:00
'description' => __ ( 'Menu order, used to custom sort products.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
2018-02-22 19:00:21 +00:00
'meta_data' => array (
2017-02-16 05:07:51 +00:00
'description' => __ ( 'Meta data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'items' => array (
'type' => 'object' ,
'properties' => array (
2018-02-22 19:00:21 +00:00
'id' => array (
2017-02-16 05:07:51 +00:00
'description' => __ ( 'Meta ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-02-22 19:00:21 +00:00
'key' => array (
2017-02-16 05:07:51 +00:00
'description' => __ ( 'Meta key.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'value' => array (
'description' => __ ( 'Meta value.' , 'woocommerce' ),
2017-11-21 18:15:51 +00:00
'type' => 'mixed' ,
2017-02-16 05:07:51 +00:00
'context' => array ( 'view' , 'edit' ),
),
),
),
),
2016-09-29 16:24:01 +00:00
),
);
return $this -> add_additional_fields_schema ( $schema );
}
}