2016-09-29 16:24:01 +00:00
< ? 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 {
/**
2016-10-04 17:36:18 +00:00
* Endpoint namespace .
*
* @ 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 () {
register_rest_route ( $this -> namespace , '/' . $this -> rest_base , array (
2017-01-26 20:06:18 +00:00
'args' => array (
'product_id' => array (
'description' => __ ( 'Unique identifier for the variable product.' , 'woocommerce' ),
'type' => 'integer' ,
),
),
2016-09-29 16:24:01 +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 (),
),
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 (
2017-01-26 19:22:57 +00:00
'args' => array (
2017-01-26 20:06:18 +00:00
'product_id' => array (
'description' => __ ( 'Unique identifier for the variable product.' , 'woocommerce' ),
'type' => 'integer' ,
),
2017-01-26 19:22:57 +00:00
'id' => array (
2017-01-26 20:06:18 +00:00
'description' => __ ( 'Unique identifier for the variation.' , 'woocommerce' ),
2017-01-26 19:22:57 +00:00
'type' => 'integer' ,
),
),
2016-09-29 16:24:01 +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' ) ),
),
),
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 ,
2017-01-26 20:06:18 +00:00
'type' => 'boolean' ,
2016-09-29 16:24:01 +00:00
'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 (
2017-01-26 20:06:18 +00:00
'args' => array (
'product_id' => array (
'description' => __ ( 'Unique identifier for the variable product.' , 'woocommerce' ),
'type' => 'integer' ,
),
),
2016-09-29 16:24:01 +00:00
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' ),
) );
}
/**
2017-02-17 00:51:36 +00:00
* Get object .
2016-09-29 16:24:01 +00:00
*
2017-02-17 00:51:36 +00:00
* @ since 2.7 . 0
* @ 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
}
/**
* Prepare a single variation output for response .
*
2017-02-17 00:51:36 +00:00
* @ since 2.7 . 0
* @ 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 (
'id' => $object -> get_id (),
'date_created' => wc_rest_prepare_date_response ( $object -> get_date_created () ),
'date_modified' => wc_rest_prepare_date_response ( $object -> get_date_modified () ),
'description' => $object -> get_description (),
'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' => $object -> get_date_on_sale_from () ? date ( 'Y-m-d' , $object -> get_date_on_sale_from () ) : '' ,
'date_on_sale_to' => $object -> get_date_on_sale_to () ? date ( 'Y-m-d' , $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 (),
2016-09-29 16:24:01 +00:00
'dimensions' => array (
2017-02-17 00:51:36 +00:00
'length' => $object -> get_length (),
'width' => $object -> get_width (),
'height' => $object -> get_height (),
2016-09-29 16:24:01 +00:00
),
2017-02-17 00:51:36 +00:00
'shipping_class' => $object -> get_shipping_class (),
'shipping_class_id' => $object -> get_shipping_class_id (),
'image' => $this -> get_images ( $object ),
'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-02-17 00:51:36 +00:00
* @ since 2.7 . 0
* @ 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' ];
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 ();
}
$variation -> set_parent_id ( absint ( $request [ 'product_id' ] ) );
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.
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' ] );
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' ] );
}
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' ] : '' );
}
}
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 .
*
* @ param WP_REST_Request $request Full details about the request
* @ return WP_Error | boolean
*/
public function delete_item ( $request ) {
2017-02-17 02:10:25 +00:00
$id = absint ( is_array ( $request [ 'id' ] ) ? $request [ 'id' ][ 'id' ] : $request [ 'id' ] );
$force = ( bool ) $request [ 'force' ];
$object = $this -> get_object ( ( int ) $request [ 'id' ] );
$result = false ;
if ( ! $object || 0 === $object -> get_id () ) {
return new WP_Error ( " woocommerce_rest_ { $this -> post_type } _invalid_id " , __ ( 'Invalid ID.' , 'woocommerce' ), array ( 'status' => 404 ) );
}
$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 () ) ) {
/* translators: %s: post type */
return new WP_Error ( " 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 () ) );
}
$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 ) {
/* translators: %s: post type */
return new WP_Error ( 'woocommerce_rest_trash_not_supported' , sprintf ( __ ( 'The %s does not support trashing.' , 'woocommerce' ), $this -> post_type ), array ( 'status' => 501 ) );
}
// Otherwise, only trash if we haven't already.
if ( is_callable ( array ( $object , 'get_status' ) ) ) {
if ( 'trash' === $object -> get_status () ) {
/* translators: %s: post type */
return new WP_Error ( 'woocommerce_rest_already_trashed' , sprintf ( __ ( 'The %s has already been deleted.' , 'woocommerce' ), $this -> post_type ), array ( 'status' => 410 ) );
}
$object -> delete ();
$result = 'trash' === $object -> get_status ();
}
}
if ( ! $result ) {
/* translators: %s: post type */
return new WP_Error ( 'woocommerce_rest_cannot_delete' , sprintf ( __ ( 'The %s cannot be deleted.' , 'woocommerce' ), $this -> post_type ), array ( 'status' => 500 ) );
}
// 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 .
*
* @ 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 ) {
2017-02-07 18:46:03 +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 (
'self' => array (
'href' => rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $base , $object -> 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 ;
}
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 (
'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 (
2016-10-12 10:16:30 +00:00
'description' => __ ( " Define if the attribute is visible on the \" Additional information \" tab in 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' ),
),
'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' ),
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 (
'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' ),
),
2016-09-29 16:24:01 +00:00
),
),
),
'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 (
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' ),
),
'dimensions' => array (
'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' ),
),
'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' ),
),
),
),
'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' ),
2016-12-07 11:36:46 +00:00
'type' => 'object' ,
2016-09-29 16:24:01 +00:00
'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' ),
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 (
'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' ),
),
2016-09-29 16:24:01 +00:00
),
),
),
2017-02-16 05:07:51 +00:00
'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' ),
),
),
),
),
2016-09-29 16:24:01 +00:00
),
);
return $this -> add_additional_fields_schema ( $schema );
}
}