2016-02-17 19:29:09 +00:00
< ? php
/**
* REST API Products controller
*
* Handles requests to the / products endpoint .
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.6 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Products controller class .
*
* @ package WooCommerce / API
2016-02-22 19:43:52 +00:00
* @ extends WC_REST_Posts_Controller
2016-02-17 19:29:09 +00:00
*/
2016-02-22 19:43:52 +00:00
class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
2016-02-17 19:29:09 +00:00
2016-03-07 18:36:17 +00:00
/**
* Endpoint namespace .
*
* @ var string
*/
2016-03-31 18:25:31 +00:00
protected $namespace = 'wc/v1' ;
2016-03-07 18:36:17 +00:00
2016-02-17 19:29:09 +00:00
/**
* Route base .
*
* @ var string
*/
2016-02-22 18:49:38 +00:00
protected $rest_base = 'products' ;
2016-02-17 19:29:09 +00:00
/**
2016-02-22 19:43:52 +00:00
* Post type .
2016-02-17 19:29:09 +00:00
*
* @ var string
*/
2016-02-22 19:43:52 +00:00
protected $post_type = 'product' ;
2016-02-17 19:29:09 +00:00
2016-03-29 20:20:15 +00:00
/**
* Initialize product actions .
*/
public function __construct () {
add_filter ( " woocommerce_rest_ { $this -> post_type } _query " , array ( $this , 'query_args' ), 10 , 2 );
2016-03-30 00:22:10 +00:00
add_action ( " woocommerce_rest_insert_ { $this -> post_type } " , array ( $this , 'clear_transients' ) );
2016-03-29 20:20:15 +00:00
}
2016-02-17 19:29:09 +00:00
/**
2016-02-23 18:34:42 +00:00
* Register the routes for products .
2016-02-17 19:29:09 +00:00
*/
public function register_routes () {
2016-03-29 18:48:08 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
array (
'methods' => WP_REST_Server :: CREATABLE ,
'callback' => array ( $this , 'create_item' ),
'permission_callback' => array ( $this , 'create_item_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: CREATABLE ),
),
'schema' => array ( $this , 'get_public_item_schema' ),
) );
2016-03-07 18:36:17 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d]+)' , array (
2016-03-29 18:48:08 +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 ),
),
2016-02-23 18:34:42 +00:00
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 ,
'description' => __ ( 'Whether to bypass trash and force deletion.' , 'woocommerce' ),
),
2016-03-29 18:48:08 +00:00
'reassign' => array (),
2016-02-23 18:34:42 +00:00
),
),
2016-03-29 18:48:08 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
2016-02-23 18:34:42 +00:00
) );
2016-05-11 19:34:53 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base . '/batch' , array (
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'batch_items' ),
'permission_callback' => array ( $this , 'batch_items_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
'schema' => array ( $this , 'get_public_batch_schema' ),
) );
2016-02-17 19:29:09 +00:00
}
2016-03-29 18:48:08 +00:00
2016-06-20 18:49:43 +00:00
/**
* Get post types .
*
* @ return array
*/
protected function get_post_types () {
return array ( 'product' , 'product_variation' );
}
2016-03-29 20:20:15 +00:00
/**
* Query args .
*
* @ param array $args
* @ param WP_REST_Request $request
* @ return array
*/
public function query_args ( $args , $request ) {
// Set post_status.
2016-08-31 22:15:28 +00:00
$args [ 'post_status' ] = $request [ 'status' ];
2016-03-29 20:20:15 +00:00
2016-03-30 23:05:12 +00:00
// Taxonomy query to filter products by type, category,
// tag, shipping class, and attribute.
$tax_query = array ();
// Map between taxonomy name and arg's key.
$taxonomies = array (
'product_cat' => 'category' ,
'product_tag' => 'tag' ,
'product_shipping_class' => 'shipping_class' ,
);
// Set tax_query for each passed arg.
foreach ( $taxonomies as $taxonomy => $key ) {
if ( ! empty ( $request [ $key ] ) ) {
$terms = explode ( ',' , $request [ $key ] );
$tax_query [] = array (
'taxonomy' => $taxonomy ,
'field' => 'term_id' ,
'terms' => $terms ,
);
}
}
2016-06-13 14:05:29 +00:00
// Filter product type by slug.
if ( ! empty ( $request [ 'type' ] ) ) {
$terms = explode ( ',' , $request [ 'type' ] );
$tax_query [] = array (
'taxonomy' => 'product_type' ,
'field' => 'slug' ,
'terms' => $terms ,
);
}
2016-03-30 23:05:12 +00:00
// Filter by attribute and term.
if ( ! empty ( $request [ 'attribute' ] ) && ! empty ( $request [ 'attribute_term' ] ) ) {
if ( in_array ( $request [ 'attribute' ], wc_get_attribute_taxonomy_names () ) ) {
$terms = explode ( ',' , $request [ 'attribute_term' ] );
$tax_query [] = array (
'taxonomy' => $request [ 'attribute' ],
'field' => 'term_id' ,
'terms' => $terms ,
);
}
}
if ( ! empty ( $tax_query ) ) {
$args [ 'tax_query' ] = $tax_query ;
}
// Filter by sku.
if ( ! empty ( $request [ 'sku' ] ) ) {
2016-10-11 02:09:47 +00:00
$args [ 'meta_query' ] = $this -> add_meta_query ( $args , array (
2016-03-30 23:05:12 +00:00
'key' => '_sku' ,
'value' => $request [ 'sku' ],
2016-08-27 01:46:45 +00:00
'compare' => '=' ,
2016-10-11 02:09:47 +00:00
) );
2016-03-30 23:05:12 +00:00
}
2016-10-11 02:09:47 +00:00
// Filter featured.
2016-10-11 01:57:04 +00:00
if ( is_bool ( $request [ 'featured' ] ) ) {
2016-10-11 02:09:47 +00:00
$args [ 'meta_query' ] = $this -> add_meta_query ( $args , array (
'key' => '_featured' ,
'value' => true === $request [ 'featured' ] ? 'yes' : 'no' ,
) );
2016-10-11 01:57:04 +00:00
}
2016-08-31 22:36:55 +00:00
// Apply all WP_Query filters again.
2016-08-31 22:15:28 +00:00
if ( is_array ( $request [ 'filter' ] ) ) {
$args = array_merge ( $args , $request [ 'filter' ] );
unset ( $args [ 'filter' ] );
}
2016-08-31 22:22:27 +00:00
// Force the post_type argument, since it's not a user input variable.
2016-08-31 22:36:55 +00:00
if ( ! empty ( $request [ 'sku' ] ) ) {
$args [ 'post_type' ] = $this -> get_post_types ();
} else {
$args [ 'post_type' ] = $this -> post_type ;
}
2016-08-31 22:22:27 +00:00
2016-03-29 20:20:15 +00:00
return $args ;
}
/**
2016-03-29 20:24:32 +00:00
* Get the downloads for a product or product variation .
2016-03-29 20:20:15 +00:00
*
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
protected function get_downloads ( $product ) {
$downloads = array ();
if ( $product -> is_downloadable () ) {
foreach ( $product -> get_files () as $file_id => $file ) {
$downloads [] = array (
'id' => $file_id , // MD5 hash.
'name' => $file [ 'name' ],
'file' => $file [ 'file' ],
);
}
}
return $downloads ;
}
2016-03-30 00:57:05 +00:00
/**
* Get taxonomy terms .
*
* @ param WC_Product $product
* @ param string $taxonomy
* @ return array
*/
protected function get_taxonomy_terms ( $product , $taxonomy = 'cat' ) {
$terms = array ();
foreach ( wp_get_post_terms ( $product -> id , 'product_' . $taxonomy ) as $term ) {
$terms [] = array (
'id' => $term -> term_id ,
'name' => $term -> name ,
'slug' => $term -> slug ,
);
}
return $terms ;
}
2016-03-29 20:20:15 +00:00
/**
* Get the images for a product or product variation .
*
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
protected function get_images ( $product ) {
$images = array ();
$attachment_ids = array ();
if ( $product -> is_type ( 'variation' ) ) {
if ( has_post_thumbnail ( $product -> get_variation_id () ) ) {
// Add variation image if set.
$attachment_ids [] = get_post_thumbnail_id ( $product -> get_variation_id () );
} elseif ( has_post_thumbnail ( $product -> id ) ) {
// Otherwise use the parent product featured image if set.
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
} else {
2016-03-29 20:24:32 +00:00
// Add featured image.
2016-03-29 20:20:15 +00:00
if ( has_post_thumbnail ( $product -> id ) ) {
$attachment_ids [] = get_post_thumbnail_id ( $product -> id );
}
2016-03-29 20:24:32 +00:00
// Add gallery images.
2016-03-29 20:20:15 +00:00
$attachment_ids = array_merge ( $attachment_ids , $product -> get_gallery_attachment_ids () );
}
// Build image data.
foreach ( $attachment_ids as $position => $attachment_id ) {
$attachment_post = get_post ( $attachment_id );
if ( is_null ( $attachment_post ) ) {
continue ;
}
$attachment = wp_get_attachment_image_src ( $attachment_id , 'full' );
if ( ! is_array ( $attachment ) ) {
continue ;
}
$images [] = array (
'id' => ( int ) $attachment_id ,
2016-03-30 00:22:10 +00:00
'date_created' => wc_rest_prepare_date_response ( $attachment_post -> post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response ( $attachment_post -> post_modified_gmt ),
2016-03-29 20:20:15 +00:00
'src' => current ( $attachment ),
2016-06-14 22:16:35 +00:00
'name' => get_the_title ( $attachment_id ),
2016-03-29 20:20:15 +00:00
'alt' => get_post_meta ( $attachment_id , '_wp_attachment_image_alt' , true ),
'position' => ( int ) $position ,
);
}
2016-03-29 20:24:32 +00:00
// Set a placeholder image if the product has no images set.
2016-03-29 20:20:15 +00:00
if ( empty ( $images ) ) {
$images [] = array (
'id' => 0 ,
2016-06-13 22:37:37 +00:00
'date_created' => wc_rest_prepare_date_response ( current_time ( 'mysql' ) ), // Default to now.
'date_modified' => wc_rest_prepare_date_response ( current_time ( 'mysql' ) ),
2016-03-29 20:20:15 +00:00
'src' => wc_placeholder_img_src (),
2016-06-14 22:16:35 +00:00
'name' => __ ( 'Placeholder' , 'woocommerce' ),
2016-03-29 20:20:15 +00:00
'alt' => __ ( 'Placeholder' , 'woocommerce' ),
'position' => 0 ,
);
}
return $images ;
}
2016-06-02 22:47:47 +00:00
/**
* Get attribute taxonomy label .
*
* @ param string $name
* @ return string
*/
protected function get_attribute_taxonomy_label ( $name ) {
$tax = get_taxonomy ( $name );
$labels = get_taxonomy_labels ( $tax );
return $labels -> singular_name ;
}
2016-03-30 00:57:05 +00:00
/**
* Get default attributes .
*
* @ param WC_Product $product
* @ return array
*/
protected function get_default_attributes ( $product ) {
$default = array ();
if ( $product -> is_type ( 'variable' ) ) {
2016-06-21 21:09:22 +00:00
foreach ( array_filter ( ( array ) get_post_meta ( $product -> id , '_default_attributes' , true ), 'strlen' ) as $key => $value ) {
2016-06-02 22:47:47 +00:00
if ( 0 === strpos ( $key , 'pa_' ) ) {
$default [] = array (
'id' => wc_attribute_taxonomy_id_by_name ( $key ),
'name' => $this -> get_attribute_taxonomy_label ( $key ),
'option' => $value ,
);
} else {
$default [] = array (
'id' => 0 ,
'name' => str_replace ( 'pa_' , '' , $key ),
'option' => $value ,
);
}
2016-03-30 00:57:05 +00:00
}
}
return $default ;
}
2016-06-15 17:32:22 +00:00
/**
* Get attribute options .
*
* @ param int $product_id
* @ param array $attribute
* @ return array
*/
protected function get_attribute_options ( $product_id , $attribute ) {
if ( isset ( $attribute [ 'is_taxonomy' ] ) && $attribute [ 'is_taxonomy' ] ) {
return wc_get_product_terms ( $product_id , $attribute [ 'name' ], array ( 'fields' => 'names' ) );
} elseif ( isset ( $attribute [ 'value' ] ) ) {
return array_map ( 'trim' , explode ( '|' , $attribute [ 'value' ] ) );
}
return array ();
}
2016-03-29 20:20:15 +00:00
/**
2016-03-29 20:24:32 +00:00
* Get the attributes for a product or product variation .
2016-03-29 20:20:15 +00:00
*
* @ param WC_Product | WC_Product_Variation $product
* @ return array
*/
protected function get_attributes ( $product ) {
$attributes = array ();
if ( $product -> is_type ( 'variation' ) ) {
// Variation attributes.
foreach ( $product -> get_variation_attributes () as $attribute_name => $attribute ) {
2016-06-02 22:47:47 +00:00
$name = str_replace ( 'attribute_' , '' , $attribute_name );
2016-09-12 14:13:29 +00:00
if ( ! $attribute ) {
continue ;
}
2016-03-29 20:20:15 +00:00
// Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
2016-06-02 22:47:47 +00:00
if ( 0 === strpos ( $attribute_name , 'attribute_pa_' ) ) {
2016-09-08 12:13:39 +00:00
$option_term = get_term_by ( 'slug' , $attribute , $name );
2016-06-02 22:47:47 +00:00
$attributes [] = array (
'id' => wc_attribute_taxonomy_id_by_name ( $name ),
'name' => $this -> get_attribute_taxonomy_label ( $name ),
2016-09-12 14:13:29 +00:00
'option' => $option_term && ! is_wp_error ( $option_term ) ? $option_term -> name : $attribute ,
2016-06-02 22:47:47 +00:00
);
} else {
$attributes [] = array (
'id' => 0 ,
2016-09-08 11:04:46 +00:00
'name' => $name ,
2016-06-02 22:47:47 +00:00
'option' => $attribute ,
);
}
2016-03-29 20:20:15 +00:00
}
} else {
foreach ( $product -> get_attributes () as $attribute ) {
if ( $attribute [ 'is_taxonomy' ] ) {
2016-06-02 22:47:47 +00:00
$attributes [] = array (
2016-06-15 17:32:22 +00:00
'id' => wc_attribute_taxonomy_id_by_name ( $attribute [ 'name' ] ),
2016-06-02 22:47:47 +00:00
'name' => $this -> get_attribute_taxonomy_label ( $attribute [ 'name' ] ),
'position' => ( int ) $attribute [ 'position' ],
'visible' => ( bool ) $attribute [ 'is_visible' ],
'variation' => ( bool ) $attribute [ 'is_variation' ],
2016-06-15 17:32:22 +00:00
'options' => $this -> get_attribute_options ( $product -> id , $attribute ),
2016-06-02 22:47:47 +00:00
);
2016-03-29 20:20:15 +00:00
} else {
2016-06-02 22:47:47 +00:00
$attributes [] = array (
'id' => 0 ,
2016-09-08 11:04:46 +00:00
'name' => $attribute [ 'name' ],
2016-06-02 22:47:47 +00:00
'position' => ( int ) $attribute [ 'position' ],
'visible' => ( bool ) $attribute [ 'is_visible' ],
'variation' => ( bool ) $attribute [ 'is_variation' ],
2016-06-15 17:32:22 +00:00
'options' => $this -> get_attribute_options ( $product -> id , $attribute ),
2016-06-02 22:47:47 +00:00
);
2016-03-29 20:20:15 +00:00
}
}
}
return $attributes ;
}
/**
* Get product menu order .
*
* @ param WC_Product $product
* @ return int
*/
protected function get_product_menu_order ( $product ) {
$menu_order = $product -> get_post_data () -> menu_order ;
if ( $product -> is_type ( 'variation' ) ) {
$variation = get_post ( $product -> get_variation_id () );
$menu_order = $variation -> menu_order ;
}
2016-03-30 00:46:35 +00:00
return $menu_order ;
}
/**
* Get product data .
*
* @ param WC_Product $product
* @ return array
*/
protected function get_product_data ( $product ) {
$data = array (
'id' => ( int ) $product -> is_type ( 'variation' ) ? $product -> get_variation_id () : $product -> id ,
'name' => $product -> get_title (),
'slug' => $product -> get_post_data () -> post_name ,
'permalink' => $product -> get_permalink (),
'date_created' => wc_rest_prepare_date_response ( $product -> get_post_data () -> post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response ( $product -> get_post_data () -> post_modified_gmt ),
'type' => $product -> product_type ,
'status' => $product -> get_post_data () -> post_status ,
'featured' => $product -> is_featured (),
'catalog_visibility' => $product -> visibility ,
'description' => wpautop ( do_shortcode ( $product -> get_post_data () -> post_content ) ),
'short_description' => apply_filters ( 'woocommerce_short_description' , $product -> get_post_data () -> post_excerpt ),
'sku' => $product -> get_sku (),
'price' => $product -> get_price (),
'regular_price' => $product -> get_regular_price (),
'sale_price' => $product -> get_sale_price () ? $product -> get_sale_price () : '' ,
2016-04-01 17:36:10 +00:00
'date_on_sale_from' => $product -> sale_price_dates_from ? date ( 'Y-m-d' , $product -> sale_price_dates_from ) : '' ,
'date_on_sale_to' => $product -> sale_price_dates_to ? date ( 'Y-m-d' , $product -> sale_price_dates_to ) : '' ,
2016-03-30 00:46:35 +00:00
'price_html' => $product -> get_price_html (),
'on_sale' => $product -> is_on_sale (),
2016-04-01 17:36:10 +00:00
'purchasable' => $product -> is_purchasable (),
2016-03-30 00:46:35 +00:00
'total_sales' => ( int ) get_post_meta ( $product -> id , 'total_sales' , true ),
'virtual' => $product -> is_virtual (),
'downloadable' => $product -> is_downloadable (),
'downloads' => $this -> get_downloads ( $product ),
2016-05-31 20:38:50 +00:00
'download_limit' => '' !== $product -> download_limit ? ( int ) $product -> download_limit : - 1 ,
'download_expiry' => '' !== $product -> download_expiry ? ( int ) $product -> download_expiry : - 1 ,
2016-03-30 00:46:35 +00:00
'download_type' => $product -> download_type ? $product -> download_type : 'standard' ,
'external_url' => $product -> is_type ( 'external' ) ? $product -> get_product_url () : '' ,
'button_text' => $product -> is_type ( 'external' ) ? $product -> get_button_text () : '' ,
'tax_status' => $product -> get_tax_status (),
'tax_class' => $product -> get_tax_class (),
2016-04-01 17:36:10 +00:00
'manage_stock' => $product -> managing_stock (),
2016-03-30 00:46:35 +00:00
'stock_quantity' => $product -> get_stock_quantity (),
'in_stock' => $product -> is_in_stock (),
'backorders' => $product -> backorders ,
'backorders_allowed' => $product -> backorders_allowed (),
'backordered' => $product -> is_on_backorder (),
'sold_individually' => $product -> is_sold_individually (),
'weight' => $product -> get_weight (),
2016-04-01 17:57:25 +00:00
'dimensions' => array (
'length' => $product -> get_length (),
'width' => $product -> get_width (),
'height' => $product -> get_height (),
),
2016-03-30 00:46:35 +00:00
'shipping_required' => $product -> needs_shipping (),
'shipping_taxable' => $product -> is_shipping_taxable (),
'shipping_class' => $product -> get_shipping_class (),
'shipping_class_id' => ( int ) $product -> get_shipping_class_id (),
'reviews_allowed' => ( 'open' === $product -> get_post_data () -> comment_status ),
'average_rating' => wc_format_decimal ( $product -> get_average_rating (), 2 ),
'rating_count' => ( int ) $product -> get_rating_count (),
'related_ids' => array_map ( 'absint' , array_values ( $product -> get_related () ) ),
'upsell_ids' => array_map ( 'absint' , $product -> get_upsells () ),
'cross_sell_ids' => array_map ( 'absint' , $product -> get_cross_sells () ),
'parent_id' => $product -> is_type ( 'variation' ) ? $product -> parent -> id : $product -> get_post_data () -> post_parent ,
'purchase_note' => wpautop ( do_shortcode ( wp_kses_post ( $product -> purchase_note ) ) ),
2016-03-30 00:57:05 +00:00
'categories' => $this -> get_taxonomy_terms ( $product ),
'tags' => $this -> get_taxonomy_terms ( $product , 'tag' ),
2016-03-30 00:46:35 +00:00
'images' => $this -> get_images ( $product ),
'attributes' => $this -> get_attributes ( $product ),
'default_attributes' => $this -> get_default_attributes ( $product ),
'variations' => array (),
'grouped_products' => array (),
'menu_order' => $this -> get_product_menu_order ( $product ),
);
return $data ;
}
/**
* Get an individual variation ' s data .
*
* @ param WC_Product $product
* @ return array
*/
protected function get_variation_data ( $product ) {
$variations = array ();
foreach ( $product -> get_children () as $child_id ) {
$variation = $product -> get_child ( $child_id );
if ( ! $variation -> exists () ) {
continue ;
}
2016-06-09 15:36:06 +00:00
$post_data = get_post ( $variation -> get_variation_id () );
2016-03-30 00:46:35 +00:00
$variations [] = array (
2016-04-01 18:00:00 +00:00
'id' => $variation -> get_variation_id (),
2016-06-09 15:36:06 +00:00
'date_created' => wc_rest_prepare_date_response ( $post_data -> post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response ( $post_data -> post_modified_gmt ),
2016-04-01 18:00:00 +00:00
'permalink' => $variation -> get_permalink (),
2016-09-13 22:28:25 +00:00
'description' => $variation -> get_variation_description (),
2016-04-01 18:00:00 +00:00
'sku' => $variation -> get_sku (),
'price' => $variation -> get_price (),
'regular_price' => $variation -> get_regular_price (),
'sale_price' => $variation -> get_sale_price (),
'date_on_sale_from' => $variation -> sale_price_dates_from ? date ( 'Y-m-d' , $variation -> sale_price_dates_from ) : '' ,
'date_on_sale_to' => $variation -> sale_price_dates_to ? date ( 'Y-m-d' , $variation -> sale_price_dates_to ) : '' ,
'on_sale' => $variation -> is_on_sale (),
'purchasable' => $variation -> is_purchasable (),
2016-07-14 06:41:02 +00:00
'visible' => $variation -> is_visible (),
2016-04-01 18:00:00 +00:00
'virtual' => $variation -> is_virtual (),
'downloadable' => $variation -> is_downloadable (),
'downloads' => $this -> get_downloads ( $variation ),
2016-05-31 20:38:50 +00:00
'download_limit' => '' !== $variation -> download_limit ? ( int ) $variation -> download_limit : - 1 ,
'download_expiry' => '' !== $variation -> download_expiry ? ( int ) $variation -> download_expiry : - 1 ,
2016-04-01 18:00:00 +00:00
'tax_status' => $variation -> get_tax_status (),
'tax_class' => $variation -> get_tax_class (),
'manage_stock' => $variation -> managing_stock (),
'stock_quantity' => $variation -> get_stock_quantity (),
'in_stock' => $variation -> is_in_stock (),
'backorders' => $variation -> backorders ,
'backorders_allowed' => $variation -> backorders_allowed (),
'backordered' => $variation -> is_on_backorder (),
'weight' => $variation -> get_weight (),
'dimensions' => array (
2016-04-01 17:57:25 +00:00
'length' => $variation -> get_length (),
'width' => $variation -> get_width (),
'height' => $variation -> get_height (),
),
2016-04-01 18:00:00 +00:00
'shipping_class' => $variation -> get_shipping_class (),
'shipping_class_id' => $variation -> get_shipping_class_id (),
'image' => $this -> get_images ( $variation ),
'attributes' => $this -> get_attributes ( $variation ),
2016-03-30 00:46:35 +00:00
);
}
return $variations ;
2016-03-29 20:20:15 +00:00
}
/**
* Prepare a single product output for response .
*
* @ param WP_Post $post Post object .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $data
*/
public function prepare_item_for_response ( $post , $request ) {
$product = wc_get_product ( $post );
2016-03-31 19:14:18 +00:00
$data = $this -> get_product_data ( $product );
2016-03-29 20:20:15 +00:00
// Add variations to variable products.
if ( $product -> is_type ( 'variable' ) && $product -> has_child () ) {
$data [ 'variations' ] = $this -> get_variation_data ( $product );
}
// Add grouped products data.
if ( $product -> is_type ( 'grouped' ) && $product -> has_child () ) {
$data [ 'grouped_products' ] = $product -> get_children ();
}
$context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ;
$data = $this -> add_additional_fields_to_object ( $data , $request );
$data = $this -> filter_response_by_context ( $data , $context );
// Wrap the data in a response object.
$response = rest_ensure_response ( $data );
2016-09-29 16:24:01 +00:00
$response -> add_links ( $this -> prepare_links ( $product , $request ) );
2016-03-29 20:20:15 +00:00
/**
* Filter the data for a response .
*
* The dynamic portion of the hook name , $this -> post_type , refers to post_type of the post being
* prepared for the response .
*
* @ param WP_REST_Response $response The response object .
* @ param WP_Post $post Post object .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( " woocommerce_rest_prepare_ { $this -> post_type } " , $response , $post , $request );
}
/**
* Prepare links for the request .
*
* @ param WC_Product $product Product object .
2016-09-29 16:24:01 +00:00
* @ param WP_REST_Request $request Request object .
2016-03-29 20:20:15 +00:00
* @ return array Links for the given product .
*/
2016-09-29 16:24:01 +00:00
protected function prepare_links ( $product , $request ) {
2016-03-29 20:20:15 +00:00
$links = array (
'self' => array (
'href' => rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $this -> rest_base , $product -> id ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
);
if ( $product -> is_type ( 'variation' ) && $product -> parent ) {
$links [ 'up' ] = array (
2016-06-20 19:03:30 +00:00
'href' => rest_url ( sprintf ( '/%s/products/%d' , $this -> namespace , $product -> parent -> id ) ),
2016-03-29 20:20:15 +00:00
);
2016-03-29 20:24:32 +00:00
} elseif ( $product -> is_type ( 'simple' ) && ! empty ( $product -> post -> post_parent ) ) {
2016-03-29 20:20:15 +00:00
$links [ 'up' ] = array (
'href' => rest_url ( sprintf ( '/%s/products/%d' , $this -> namespace , $product -> post -> post_parent ) ),
);
}
return $links ;
}
2016-03-30 00:22:10 +00:00
/**
* Prepare a single product for create or update .
*
* @ param WP_REST_Request $request Request object .
* @ return WP_Error | stdClass $data Post object .
*/
protected function prepare_item_for_database ( $request ) {
$data = new stdClass ;
// ID.
if ( isset ( $request [ 'id' ] ) ) {
$data -> ID = absint ( $request [ 'id' ] );
}
// Post title.
if ( isset ( $request [ 'name' ] ) ) {
$data -> post_title = wp_filter_post_kses ( $request [ 'name' ] );
}
// Post content.
if ( isset ( $request [ 'description' ] ) ) {
$data -> post_content = wp_filter_post_kses ( $request [ 'description' ] );
}
// Post excerpt.
if ( isset ( $request [ 'short_description' ] ) ) {
$data -> post_excerpt = wp_filter_post_kses ( $request [ 'short_description' ] );
}
// Post status.
if ( isset ( $request [ 'status' ] ) ) {
$data -> post_status = get_post_status_object ( $request [ 'status' ] ) ? $request [ 'status' ] : 'draft' ;
}
// Post slug.
if ( isset ( $request [ 'slug' ] ) ) {
$data -> post_name = $request [ 'slug' ];
}
// Menu order.
if ( isset ( $request [ 'menu_order' ] ) ) {
$data -> menu_order = ( int ) $request [ 'menu_order' ];
}
// Comment status.
if ( ! empty ( $request [ 'reviews_allowed' ] ) ) {
$data -> comment_status = $request [ 'reviews_allowed' ] ? 'open' : 'closed' ;
}
// Only when creating products.
if ( empty ( $request [ 'id' ] ) ) {
// Post type.
$data -> post_type = $this -> post_type ;
// Ping status.
$data -> ping_status = 'closed' ;
}
/**
* Filter the query_vars used in `get_items` for the constructed query .
*
* The dynamic portion of the hook name , $this -> post_type , refers to post_type of the post being
* prepared for insertion .
*
* @ param stdClass $data An object representing a single item prepared
* for inserting or updating the database .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( " woocommerce_rest_pre_insert_ { $this -> post_type } " , $data , $request );
}
/**
* Save product images .
*
2016-07-13 08:30:00 +00:00
* @ param int $product_id
2016-03-30 00:22:10 +00:00
* @ param array $images
* @ throws WC_REST_Exception
*/
2016-07-13 08:30:00 +00:00
protected function save_product_images ( $product_id , $images ) {
2016-03-30 00:22:10 +00:00
if ( is_array ( $images ) ) {
$gallery = array ();
foreach ( $images as $image ) {
2016-06-30 16:03:52 +00:00
$attachment_id = isset ( $image [ 'id' ] ) ? absint ( $image [ 'id' ] ) : 0 ;
2016-03-30 00:22:10 +00:00
2016-06-30 16:03:52 +00:00
if ( 0 === $attachment_id && isset ( $image [ 'src' ] ) ) {
$upload = wc_rest_upload_image_from_url ( esc_url_raw ( $image [ 'src' ] ) );
2016-03-30 00:22:10 +00:00
2016-06-30 16:03:52 +00:00
if ( is_wp_error ( $upload ) ) {
2016-09-10 02:41:41 +00:00
if ( ! apply_filters ( 'woocommerce_rest_suppress_image_upload_error' , false , $upload , $product_id , $images ) ) {
throw new WC_REST_Exception ( 'woocommerce_product_image_upload_error' , $upload -> get_error_message (), 400 );
2016-09-10 03:49:45 +00:00
} else {
continue ;
2016-09-10 02:41:41 +00:00
}
2016-03-30 00:22:10 +00:00
}
2016-07-13 08:30:00 +00:00
$attachment_id = wc_rest_set_uploaded_image_as_attachment ( $upload , $product_id );
2016-06-30 16:03:52 +00:00
}
if ( isset ( $image [ 'position' ] ) && 0 === $image [ 'position' ] ) {
2016-07-13 08:30:00 +00:00
set_post_thumbnail ( $product_id , $attachment_id );
2016-03-30 00:22:10 +00:00
} else {
$gallery [] = $attachment_id ;
}
// Set the image alt if present.
2016-06-30 16:03:52 +00:00
if ( ! empty ( $image [ 'alt' ] ) ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $attachment_id , '_wp_attachment_image_alt' , wc_clean ( $image [ 'alt' ] ) );
}
2016-06-14 22:16:35 +00:00
// Set the image name if present.
2016-06-30 16:03:52 +00:00
if ( ! empty ( $image [ 'name' ] ) ) {
2016-06-14 22:16:35 +00:00
wp_update_post ( array ( 'ID' => $attachment_id , 'post_title' => $image [ 'name' ] ) );
2016-03-30 00:22:10 +00:00
}
}
if ( ! empty ( $gallery ) ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_product_image_gallery' , implode ( ',' , $gallery ) );
2016-03-30 00:22:10 +00:00
}
} else {
2016-07-13 08:30:00 +00:00
delete_post_meta ( $product_id , '_thumbnail_id' );
update_post_meta ( $product_id , '_product_image_gallery' , '' );
2016-03-30 00:22:10 +00:00
}
}
/**
* Save product shipping data .
*
2016-07-13 08:30:00 +00:00
* @ param int $product_id
2016-03-30 00:22:10 +00:00
* @ param array $data
*/
2016-07-13 08:30:00 +00:00
private function save_product_shipping_data ( $product_id , $data ) {
2016-03-30 00:22:10 +00:00
// Virtual.
if ( isset ( $data [ 'virtual' ] ) && true === $data [ 'virtual' ] ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_weight' , '' );
update_post_meta ( $product_id , '_length' , '' );
update_post_meta ( $product_id , '_width' , '' );
update_post_meta ( $product_id , '_height' , '' );
2016-03-30 00:22:10 +00:00
} else {
if ( isset ( $data [ 'weight' ] ) ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_weight' , '' === $data [ 'weight' ] ? '' : wc_format_decimal ( $data [ 'weight' ] ) );
2016-03-30 00:22:10 +00:00
}
// Height.
2016-04-01 17:57:25 +00:00
if ( isset ( $data [ 'dimensions' ][ 'height' ] ) ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_height' , '' === $data [ 'dimensions' ][ 'height' ] ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'height' ] ) );
2016-03-30 00:22:10 +00:00
}
// Width.
2016-04-01 17:57:25 +00:00
if ( isset ( $data [ 'dimensions' ][ 'width' ] ) ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_width' , '' === $data [ 'dimensions' ][ 'width' ] ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'width' ] ) );
2016-03-30 00:22:10 +00:00
}
// Length.
2016-04-01 17:57:25 +00:00
if ( isset ( $data [ 'dimensions' ][ 'length' ] ) ) {
2016-07-13 08:30:00 +00:00
update_post_meta ( $product_id , '_length' , '' === $data [ 'dimensions' ][ 'length' ] ? '' : wc_format_decimal ( $data [ 'dimensions' ][ 'length' ] ) );
2016-03-30 00:22:10 +00:00
}
}
// Shipping class.
if ( isset ( $data [ 'shipping_class' ] ) ) {
2016-07-13 08:30:00 +00:00
wp_set_object_terms ( $product_id , wc_clean ( $data [ 'shipping_class' ] ), 'product_shipping_class' );
2016-03-30 00:22:10 +00:00
}
}
/**
* Save downloadable files .
*
2016-07-13 08:30:00 +00:00
* @ param in $product_id
2016-03-30 00:22:10 +00:00
* @ param array $downloads
* @ param int $variation_id
*/
2016-07-13 08:30:00 +00:00
private function save_downloadable_files ( $product_id , $downloads , $variation_id = 0 ) {
2016-03-30 00:22:10 +00:00
$files = array ();
// File paths will be stored in an array keyed off md5(file path).
foreach ( $downloads as $key => $file ) {
if ( isset ( $file [ 'url' ] ) ) {
$file [ 'file' ] = $file [ 'url' ];
}
if ( ! isset ( $file [ 'file' ] ) ) {
continue ;
}
$file_name = isset ( $file [ 'name' ] ) ? wc_clean ( $file [ 'name' ] ) : '' ;
if ( 0 === strpos ( $file [ 'file' ], 'http' ) ) {
$file_url = esc_url_raw ( $file [ 'file' ] );
} else {
$file_url = wc_clean ( $file [ 'file' ] );
}
$files [ md5 ( $file_url ) ] = array (
'name' => $file_name ,
2016-04-05 19:58:18 +00:00
'file' => $file_url ,
2016-03-30 00:22:10 +00:00
);
}
// Grant permission to any newly added files on any existing orders for this product prior to saving.
2016-07-13 08:30:00 +00:00
do_action ( 'woocommerce_process_product_file_download_paths' , $product_id , $variation_id , $files );
2016-03-30 00:22:10 +00:00
2016-07-13 08:30:00 +00:00
$id = ( 0 === $variation_id ) ? $product_id : $variation_id ;
2016-03-30 00:22:10 +00:00
update_post_meta ( $id , '_downloadable_files' , $files );
}
2016-03-30 01:06:05 +00:00
/**
* Save taxonomy terms .
*
2016-07-13 08:30:00 +00:00
* @ param int $product_id
2016-03-30 01:06:05 +00:00
* @ param array $terms
* @ param string $taxonomy
* @ return array
*/
2016-07-13 08:30:00 +00:00
protected function save_taxonomy_terms ( $product_id , $terms , $taxonomy = 'cat' ) {
2016-03-30 01:06:05 +00:00
$term_ids = wp_list_pluck ( $terms , 'id' );
$term_ids = array_unique ( array_map ( 'intval' , $term_ids ) );
2016-07-13 08:30:00 +00:00
wp_set_object_terms ( $product_id , $term_ids , 'product_' . $taxonomy );
2016-03-30 01:06:05 +00:00
return $terms ;
}
2016-03-30 00:22:10 +00:00
/**
* Save product meta .
*
* @ param WC_Product $product
* @ param WP_REST_Request $request
* @ return bool
* @ throws WC_REST_Exception
*/
protected function save_product_meta ( $product , $request ) {
global $wpdb ;
// Product Type.
$product_type = null ;
if ( isset ( $request [ 'type' ] ) ) {
$product_type = wc_clean ( $request [ 'type' ] );
wp_set_object_terms ( $product -> id , $product_type , 'product_type' );
} else {
$_product_type = get_the_terms ( $product -> id , 'product_type' );
if ( is_array ( $_product_type ) ) {
$_product_type = current ( $_product_type );
$product_type = $_product_type -> slug ;
}
}
2016-05-11 16:19:56 +00:00
// Default total sales.
2016-05-31 20:38:50 +00:00
add_post_meta ( $product -> id , 'total_sales' , '0' , true );
2016-05-11 16:19:56 +00:00
2016-03-30 00:22:10 +00:00
// Virtual.
if ( isset ( $request [ 'virtual' ] ) ) {
update_post_meta ( $product -> id , '_virtual' , true === $request [ 'virtual' ] ? 'yes' : 'no' );
}
// Tax status.
if ( isset ( $request [ 'tax_status' ] ) ) {
update_post_meta ( $product -> id , '_tax_status' , wc_clean ( $request [ 'tax_status' ] ) );
}
// Tax Class.
if ( isset ( $request [ 'tax_class' ] ) ) {
update_post_meta ( $product -> id , '_tax_class' , wc_clean ( $request [ 'tax_class' ] ) );
}
// Catalog Visibility.
if ( isset ( $request [ 'catalog_visibility' ] ) ) {
update_post_meta ( $product -> id , '_visibility' , wc_clean ( $request [ 'catalog_visibility' ] ) );
}
// Purchase Note.
if ( isset ( $request [ 'purchase_note' ] ) ) {
update_post_meta ( $product -> id , '_purchase_note' , wc_clean ( $request [ 'purchase_note' ] ) );
}
// Featured Product.
if ( isset ( $request [ 'featured' ] ) ) {
update_post_meta ( $product -> id , '_featured' , true === $request [ 'featured' ] ? 'yes' : 'no' );
}
// Shipping data.
2016-07-13 08:30:00 +00:00
$this -> save_product_shipping_data ( $product -> id , $request );
2016-03-30 00:22:10 +00:00
// SKU.
if ( isset ( $request [ 'sku' ] ) ) {
$sku = get_post_meta ( $product -> id , '_sku' , true );
$new_sku = wc_clean ( $request [ 'sku' ] );
2016-04-05 19:58:18 +00:00
if ( '' === $new_sku ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_sku' , '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty ( $new_sku ) ) {
$unique_sku = wc_product_has_unique_sku ( $product -> id , $new_sku );
if ( ! $unique_sku ) {
throw new WC_REST_Exception ( 'woocommerce_rest_product_sku_already_exists' , __ ( 'The SKU already exists on another product.' , 'woocommerce' ), 400 );
} else {
update_post_meta ( $product -> id , '_sku' , $new_sku );
}
} else {
update_post_meta ( $product -> id , '_sku' , '' );
}
}
}
// Attributes.
if ( isset ( $request [ 'attributes' ] ) ) {
$attributes = array ();
foreach ( $request [ 'attributes' ] as $attribute ) {
2016-06-02 21:12:04 +00:00
$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 = wc_clean ( $attribute [ 'name' ] );
2016-03-30 00:22:10 +00:00
}
2016-06-02 21:12:04 +00:00
if ( ! $attribute_id && ! $attribute_name ) {
continue ;
2016-03-30 00:22:10 +00:00
}
2016-06-02 21:12:04 +00:00
if ( $attribute_id ) {
2016-03-30 00:22:10 +00:00
if ( isset ( $attribute [ 'options' ] ) ) {
$options = $attribute [ 'options' ];
if ( ! is_array ( $attribute [ 'options' ] ) ) {
// Text based attributes - Posted values are term names.
$options = explode ( WC_DELIMITER , $options );
}
$values = array_map ( 'wc_sanitize_term_text_based' , $options );
$values = array_filter ( $values , 'strlen' );
} else {
$values = array ();
}
// Update post terms.
2016-06-02 21:12:04 +00:00
if ( taxonomy_exists ( $attribute_name ) ) {
wp_set_object_terms ( $product -> id , $values , $attribute_name );
2016-03-30 00:22:10 +00:00
}
2016-06-06 18:39:23 +00:00
if ( ! empty ( $values ) ) {
2016-03-30 00:22:10 +00:00
// Add attribute to array, but don't set values.
2016-06-02 21:12:04 +00:00
$attributes [ $attribute_name ] = array (
'name' => $attribute_name ,
2016-03-30 00:22:10 +00:00
'value' => '' ,
2016-06-21 21:37:05 +00:00
'position' => isset ( $attribute [ 'position' ] ) ? ( string ) absint ( $attribute [ 'position' ] ) : '0' ,
2016-03-30 00:22:10 +00:00
'is_visible' => ( isset ( $attribute [ 'visible' ] ) && $attribute [ 'visible' ] ) ? 1 : 0 ,
'is_variation' => ( isset ( $attribute [ 'variation' ] ) && $attribute [ 'variation' ] ) ? 1 : 0 ,
2016-06-21 21:37:05 +00:00
'is_taxonomy' => 1 ,
2016-03-30 00:22:10 +00:00
);
}
} elseif ( isset ( $attribute [ 'options' ] ) ) {
// Array based.
if ( is_array ( $attribute [ 'options' ] ) ) {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , $attribute [ 'options' ] ) );
// Text based, separate by pipe.
} else {
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , explode ( WC_DELIMITER , $attribute [ 'options' ] ) ) );
}
// Custom attribute - Add attribute to array and set the values.
2016-06-02 21:12:04 +00:00
$attributes [ sanitize_title ( $attribute_name ) ] = array (
'name' => $attribute_name ,
2016-03-30 00:22:10 +00:00
'value' => $values ,
2016-06-21 21:37:05 +00:00
'position' => isset ( $attribute [ 'position' ] ) ? ( string ) absint ( $attribute [ 'position' ] ) : '0' ,
2016-03-30 00:22:10 +00:00
'is_visible' => ( isset ( $attribute [ 'visible' ] ) && $attribute [ 'visible' ] ) ? 1 : 0 ,
'is_variation' => ( isset ( $attribute [ 'variation' ] ) && $attribute [ 'variation' ] ) ? 1 : 0 ,
2016-06-21 21:37:05 +00:00
'is_taxonomy' => 0 ,
2016-03-30 00:22:10 +00:00
);
}
}
2016-06-06 16:24:31 +00:00
uasort ( $attributes , 'wc_product_attribute_uasort_comparison' );
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_product_attributes' , $attributes );
}
// Sales and prices.
if ( in_array ( $product_type , array ( 'variable' , 'grouped' ) ) ) {
// Variable and grouped products have no prices.
update_post_meta ( $product -> id , '_regular_price' , '' );
update_post_meta ( $product -> id , '_sale_price' , '' );
update_post_meta ( $product -> id , '_sale_price_dates_from' , '' );
update_post_meta ( $product -> id , '_sale_price_dates_to' , '' );
update_post_meta ( $product -> id , '_price' , '' );
} else {
// Regular Price
if ( isset ( $request [ 'regular_price' ] ) ) {
$regular_price = ( '' === $request [ 'regular_price' ] ) ? '' : $request [ 'regular_price' ];
} else {
$regular_price = get_post_meta ( $product -> id , '_regular_price' , true );
}
// Sale Price
if ( isset ( $request [ 'sale_price' ] ) ) {
$sale_price = ( '' === $request [ 'sale_price' ] ) ? '' : $request [ 'sale_price' ];
} else {
$sale_price = get_post_meta ( $product -> id , '_sale_price' , true );
}
2016-04-01 17:36:10 +00:00
if ( isset ( $request [ 'date_on_sale_from' ] ) ) {
$date_from = $request [ 'date_on_sale_from' ];
2016-03-30 00:22:10 +00:00
} else {
$date_from = get_post_meta ( $product -> id , '_sale_price_dates_from' , true );
$date_from = ( '' === $date_from ) ? '' : date ( 'Y-m-d' , $date_from );
}
2016-04-01 17:36:10 +00:00
if ( isset ( $request [ 'date_on_sale_to' ] ) ) {
$date_to = $request [ 'date_on_sale_to' ];
2016-03-30 00:22:10 +00:00
} else {
$date_to = get_post_meta ( $product -> id , '_sale_price_dates_to' , true );
$date_to = ( '' === $date_to ) ? '' : date ( 'Y-m-d' , $date_to );
}
_wc_save_product_price ( $product -> id , $regular_price , $sale_price , $date_from , $date_to );
}
// Product parent ID for groups.
$parent_id = 0 ;
if ( isset ( $request [ 'parent_id' ] ) ) {
$parent_id = wp_update_post ( array ( 'ID' => $product -> id , 'post_parent' => absint ( $request [ 'parent_id' ] ) ) );
}
// Update parent if grouped so price sorting works and stays in sync with the cheapest child.
2016-04-05 19:58:18 +00:00
if ( $parent_id > 0 || 'grouped' === $product_type ) {
2016-03-30 00:22:10 +00:00
$clear_parent_ids = array ();
if ( $parent_id > 0 ) {
$clear_parent_ids [] = $parent_id ;
}
2016-04-05 19:58:18 +00:00
if ( 'grouped' === $product_type ) {
2016-03-30 00:22:10 +00:00
$clear_parent_ids [] = $product -> id ;
}
2016-06-06 18:39:23 +00:00
if ( ! empty ( $clear_parent_ids ) ) {
2016-03-30 00:22:10 +00:00
foreach ( $clear_parent_ids as $clear_id ) {
$children_by_price = get_posts ( array (
'post_parent' => $clear_id ,
'orderby' => 'meta_value_num' ,
'order' => 'asc' ,
'meta_key' => '_price' ,
'posts_per_page' => 1 ,
'post_type' => 'product' ,
2016-08-27 01:46:45 +00:00
'fields' => 'ids' ,
2016-03-30 00:22:10 +00:00
) );
if ( $children_by_price ) {
foreach ( $children_by_price as $child ) {
$child_price = get_post_meta ( $child , '_price' , true );
update_post_meta ( $clear_id , '_price' , $child_price );
}
}
}
}
}
// Sold individually.
if ( isset ( $request [ 'sold_individually' ] ) ) {
update_post_meta ( $product -> id , '_sold_individually' , true === $request [ 'sold_individually' ] ? 'yes' : '' );
}
// Stock status.
if ( isset ( $request [ 'in_stock' ] ) ) {
$stock_status = true === $request [ 'in_stock' ] ? 'instock' : 'outofstock' ;
} else {
$stock_status = get_post_meta ( $product -> id , '_stock_status' , true );
if ( '' === $stock_status ) {
$stock_status = 'instock' ;
}
}
// Stock data.
2016-04-05 19:58:18 +00:00
if ( 'yes' === get_option ( 'woocommerce_manage_stock' ) ) {
2016-03-30 00:22:10 +00:00
// Manage stock.
2016-04-01 17:36:10 +00:00
if ( isset ( $request [ 'manage_stock' ] ) ) {
$manage_stock = ( true === $request [ 'manage_stock' ] ) ? 'yes' : 'no' ;
update_post_meta ( $product -> id , '_manage_stock' , $manage_stock );
2016-03-30 00:22:10 +00:00
} else {
2016-04-01 17:36:10 +00:00
$manage_stock = get_post_meta ( $product -> id , '_manage_stock' , true );
2016-03-30 00:22:10 +00:00
}
// Backorders.
if ( isset ( $request [ 'backorders' ] ) ) {
2016-03-31 12:56:22 +00:00
$backorders = $request [ 'backorders' ];
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_backorders' , $backorders );
} else {
$backorders = get_post_meta ( $product -> id , '_backorders' , true );
}
2016-04-05 19:58:18 +00:00
if ( 'grouped' === $product_type ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_manage_stock' , 'no' );
update_post_meta ( $product -> id , '_backorders' , 'no' );
update_post_meta ( $product -> id , '_stock' , '' );
wc_update_product_stock_status ( $product -> id , $stock_status );
2016-04-05 19:58:18 +00:00
} elseif ( 'external' === $product_type ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_manage_stock' , 'no' );
update_post_meta ( $product -> id , '_backorders' , 'no' );
update_post_meta ( $product -> id , '_stock' , '' );
wc_update_product_stock_status ( $product -> id , 'instock' );
2016-04-05 19:58:18 +00:00
} elseif ( 'yes' === $manage_stock ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $product -> id , '_backorders' , $backorders );
2016-06-23 22:36:39 +00:00
// Stock status is always determined by children so sync later.
if ( 'variable' !== $product_type ) {
wc_update_product_stock_status ( $product -> id , $stock_status );
}
2016-03-30 00:22:10 +00:00
// Stock quantity.
if ( isset ( $request [ 'stock_quantity' ] ) ) {
wc_update_product_stock ( $product -> id , wc_stock_amount ( $request [ 'stock_quantity' ] ) );
2016-05-30 22:22:26 +00:00
} elseif ( isset ( $request [ 'inventory_delta' ] ) ) {
2016-03-30 00:22:10 +00:00
$stock_quantity = wc_stock_amount ( get_post_meta ( $product -> id , '_stock' , true ) );
$stock_quantity += wc_stock_amount ( $request [ 'inventory_delta' ] );
wc_update_product_stock ( $product -> id , wc_stock_amount ( $stock_quantity ) );
}
} else {
// Don't manage stock.
update_post_meta ( $product -> id , '_manage_stock' , 'no' );
update_post_meta ( $product -> id , '_backorders' , $backorders );
2016-10-12 11:51:40 +00:00
wc_update_product_stock ( $product -> id , '' );
2016-03-30 00:22:10 +00:00
wc_update_product_stock_status ( $product -> id , $stock_status );
}
2016-03-31 12:56:22 +00:00
} elseif ( 'variable' !== $product_type ) {
2016-03-30 00:22:10 +00:00
wc_update_product_stock_status ( $product -> id , $stock_status );
}
// Upsells.
if ( isset ( $request [ 'upsell_ids' ] ) ) {
$upsells = array ();
$ids = $request [ 'upsell_ids' ];
if ( ! empty ( $ids ) ) {
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$upsells [] = $id ;
}
}
update_post_meta ( $product -> id , '_upsell_ids' , $upsells );
} else {
delete_post_meta ( $product -> id , '_upsell_ids' );
}
}
// Cross sells.
if ( isset ( $request [ 'cross_sell_ids' ] ) ) {
$crosssells = array ();
$ids = $request [ 'cross_sell_ids' ];
if ( ! empty ( $ids ) ) {
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$crosssells [] = $id ;
}
}
update_post_meta ( $product -> id , '_crosssell_ids' , $crosssells );
} else {
delete_post_meta ( $product -> id , '_crosssell_ids' );
}
}
// Product categories.
if ( isset ( $request [ 'categories' ] ) && is_array ( $request [ 'categories' ] ) ) {
2016-07-13 08:30:00 +00:00
$this -> save_taxonomy_terms ( $product -> id , $request [ 'categories' ] );
2016-03-30 00:22:10 +00:00
}
// Product tags.
if ( isset ( $request [ 'tags' ] ) && is_array ( $request [ 'tags' ] ) ) {
2016-07-13 08:30:00 +00:00
$this -> save_taxonomy_terms ( $product -> id , $request [ 'tags' ], 'tag' );
2016-03-30 00:22:10 +00:00
}
// Downloadable.
if ( isset ( $request [ 'downloadable' ] ) ) {
$is_downloadable = true === $request [ 'downloadable' ] ? 'yes' : 'no' ;
update_post_meta ( $product -> id , '_downloadable' , $is_downloadable );
} else {
$is_downloadable = get_post_meta ( $product -> id , '_downloadable' , true );
}
// Downloadable options.
2016-04-05 19:58:18 +00:00
if ( 'yes' === $is_downloadable ) {
2016-03-30 00:22:10 +00:00
// Downloadable files.
if ( isset ( $request [ 'downloads' ] ) && is_array ( $request [ 'downloads' ] ) ) {
2016-07-13 08:30:00 +00:00
$this -> save_downloadable_files ( $product -> id , $request [ 'downloads' ] );
2016-03-30 00:22:10 +00:00
}
// Download limit.
if ( isset ( $request [ 'download_limit' ] ) ) {
2016-05-31 20:38:50 +00:00
update_post_meta ( $product -> id , '_download_limit' , - 1 === $request [ 'download_limit' ] ? '' : absint ( $request [ 'download_limit' ] ) );
2016-03-30 00:22:10 +00:00
}
// Download expiry.
if ( isset ( $request [ 'download_expiry' ] ) ) {
2016-05-31 20:38:50 +00:00
update_post_meta ( $product -> id , '_download_expiry' , - 1 === $request [ 'download_expiry' ] ? '' : absint ( $request [ 'download_expiry' ] ) );
2016-03-30 00:22:10 +00:00
}
// Download type.
if ( isset ( $request [ 'download_type' ] ) ) {
2016-05-31 20:44:09 +00:00
update_post_meta ( $product -> id , '_download_type' , 'standard' === $request [ 'download_type' ] ? '' : wc_clean ( $request [ 'download_type' ] ) );
2016-03-30 00:22:10 +00:00
}
}
// Product url and button text for external products.
2016-04-05 19:58:18 +00:00
if ( 'external' === $product_type ) {
2016-03-30 00:22:10 +00:00
if ( isset ( $request [ 'external_url' ] ) ) {
update_post_meta ( $product -> id , '_product_url' , wc_clean ( $request [ 'external_url' ] ) );
}
if ( isset ( $request [ 'button_text' ] ) ) {
update_post_meta ( $product -> id , '_button_text' , wc_clean ( $request [ 'button_text' ] ) );
}
}
return true ;
}
/**
* Save variations .
*
* @ param WC_Product $product
* @ param WP_REST_Request $request
* @ return bool
* @ throws WC_REST_Exception
*/
2016-09-29 16:24:01 +00:00
protected function save_variations_data ( $product , $request , $single_variation = false ) {
2016-03-30 00:22:10 +00:00
global $wpdb ;
2016-09-29 16:24:01 +00:00
if ( $single_variation ) {
$variations = array ( $request );
} else {
$variations = $request [ 'variations' ];
}
2016-06-02 21:12:04 +00:00
$attributes = $product -> get_attributes ();
2016-03-30 00:22:10 +00:00
foreach ( $variations as $menu_order => $variation ) {
$variation_id = isset ( $variation [ 'id' ] ) ? absint ( $variation [ 'id' ] ) : 0 ;
// Generate a useful post title.
2016-09-01 20:50:14 +00:00
$variation_post_title = sprintf ( __ ( 'Variation #%1$s of %2$s' , 'woocommerce' ), $variation_id , esc_html ( get_the_title ( $product -> id ) ) );
2016-03-30 00:22:10 +00:00
// Update or Add post.
if ( ! $variation_id ) {
$post_status = ( isset ( $variation [ 'visible' ] ) && false === $variation [ 'visible' ] ) ? 'private' : 'publish' ;
$new_variation = array (
'post_title' => $variation_post_title ,
'post_content' => '' ,
'post_status' => $post_status ,
'post_author' => get_current_user_id (),
'post_parent' => $product -> id ,
'post_type' => 'product_variation' ,
2016-04-05 19:58:18 +00:00
'menu_order' => $menu_order ,
2016-03-30 00:22:10 +00:00
);
$variation_id = wp_insert_post ( $new_variation );
do_action ( 'woocommerce_create_product_variation' , $variation_id );
} else {
$update_variation = array ( 'post_title' => $variation_post_title , 'menu_order' => $menu_order );
if ( isset ( $variation [ 'visible' ] ) ) {
$post_status = ( false === $variation [ 'visible' ] ) ? 'private' : 'publish' ;
$update_variation [ 'post_status' ] = $post_status ;
}
$wpdb -> update ( $wpdb -> posts , $update_variation , array ( 'ID' => $variation_id ) );
do_action ( 'woocommerce_update_product_variation' , $variation_id );
}
// Stop with we don't have a variation ID.
if ( is_wp_error ( $variation_id ) ) {
throw new WC_REST_Exception ( 'woocommerce_rest_cannot_save_product_variation' , $variation_id -> get_error_message (), 400 );
}
// SKU.
if ( isset ( $variation [ 'sku' ] ) ) {
$sku = get_post_meta ( $variation_id , '_sku' , true );
$new_sku = wc_clean ( $variation [ 'sku' ] );
2016-04-05 19:58:18 +00:00
if ( '' === $new_sku ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $variation_id , '_sku' , '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty ( $new_sku ) ) {
$unique_sku = wc_product_has_unique_sku ( $variation_id , $new_sku );
if ( ! $unique_sku ) {
2016-04-05 19:58:18 +00:00
throw new WC_REST_Exception ( 'woocommerce_rest_product_sku_already_exists' , __ ( 'The SKU already exists on another product.' , 'woocommerce' ), 400 );
2016-03-30 00:22:10 +00:00
} else {
update_post_meta ( $variation_id , '_sku' , $new_sku );
}
} else {
update_post_meta ( $variation_id , '_sku' , '' );
}
}
}
// Thumbnail.
if ( isset ( $variation [ 'image' ] ) && is_array ( $variation [ 'image' ] ) ) {
2016-09-29 16:24:01 +00:00
$image = $variation [ 'image' ];
$image = current ( $image );
2016-03-30 00:22:10 +00:00
if ( $image && is_array ( $image ) ) {
2016-06-30 16:03:52 +00:00
if ( isset ( $image [ 'position' ] ) && 0 === $image [ 'position' ] ) {
$attachment_id = isset ( $image [ 'id' ] ) ? absint ( $image [ 'id' ] ) : 0 ;
2016-09-14 01:03:05 +00:00
$skip_image = false ;
2016-03-30 00:22:10 +00:00
2016-06-30 16:03:52 +00:00
if ( 0 === $attachment_id && isset ( $image [ 'src' ] ) ) {
$upload = wc_rest_upload_image_from_url ( wc_clean ( $image [ 'src' ] ) );
if ( is_wp_error ( $upload ) ) {
2016-09-14 01:03:05 +00:00
if ( ! apply_filters ( 'woocommerce_rest_suppress_variation_image_upload_error' , false , $upload , $product ) ) {
throw new WC_REST_Exception ( 'woocommerce_product_image_upload_error' , $upload -> get_error_message (), 400 );
} else {
$skip_image = true ;
}
} else {
$attachment_id = wc_rest_set_uploaded_image_as_attachment ( $upload , $product -> id );
2016-06-30 16:03:52 +00:00
}
}
2016-03-30 00:22:10 +00:00
2016-09-14 01:03:05 +00:00
if ( ! $skip_image ) {
// Set the image alt if present.
if ( ! empty ( $image [ 'alt' ] ) ) {
update_post_meta ( $attachment_id , '_wp_attachment_image_alt' , wc_clean ( $image [ 'alt' ] ) );
}
2016-03-30 00:22:10 +00:00
2016-09-14 01:03:05 +00:00
// Set the image name if present.
if ( ! empty ( $image [ 'name' ] ) ) {
wp_update_post ( array ( 'ID' => $attachment_id , 'post_title' => $image [ 'name' ] ) );
}
2016-03-30 00:22:10 +00:00
2016-09-14 01:03:05 +00:00
update_post_meta ( $variation_id , '_thumbnail_id' , $attachment_id );
}
2016-03-30 00:22:10 +00:00
}
} else {
delete_post_meta ( $variation_id , '_thumbnail_id' );
}
}
// Virtual variation.
if ( isset ( $variation [ 'virtual' ] ) ) {
$is_virtual = ( true === $variation [ 'virtual' ] ) ? 'yes' : 'no' ;
update_post_meta ( $variation_id , '_virtual' , $is_virtual );
}
// Downloadable variation.
if ( isset ( $variation [ 'downloadable' ] ) ) {
$is_downloadable = ( true === $variation [ 'downloadable' ] ) ? 'yes' : 'no' ;
update_post_meta ( $variation_id , '_downloadable' , $is_downloadable );
} else {
$is_downloadable = get_post_meta ( $variation_id , '_downloadable' , true );
}
// Shipping data.
$this -> save_product_shipping_data ( $variation_id , $variation );
// Stock handling.
2016-04-01 17:36:10 +00:00
if ( isset ( $variation [ 'manage_stock' ] ) ) {
$manage_stock = ( true === $variation [ 'manage_stock' ] ) ? 'yes' : 'no' ;
2016-03-30 00:22:10 +00:00
} else {
2016-04-01 17:36:10 +00:00
$manage_stock = get_post_meta ( $variation_id , '_manage_stock' , true );
2016-03-30 00:22:10 +00:00
}
2016-04-01 17:36:10 +00:00
update_post_meta ( $variation_id , '_manage_stock' , '' === $manage_stock ? 'no' : $manage_stock );
2016-03-31 12:56:22 +00:00
2016-03-30 00:22:10 +00:00
if ( isset ( $variation [ 'in_stock' ] ) ) {
$stock_status = ( true === $variation [ 'in_stock' ] ) ? 'instock' : 'outofstock' ;
2016-03-31 12:56:22 +00:00
} else {
$stock_status = get_post_meta ( $variation_id , '_stock_status' , true );
2016-03-30 00:22:10 +00:00
}
2016-03-31 12:56:22 +00:00
wc_update_product_stock_status ( $variation_id , '' === $stock_status ? 'instock' : $stock_status );
2016-04-01 17:36:10 +00:00
if ( 'yes' === $manage_stock ) {
2016-03-30 00:22:10 +00:00
$backorders = get_post_meta ( $variation_id , '_backorders' , true );
if ( isset ( $variation [ 'backorders' ] ) ) {
2016-03-31 12:56:22 +00:00
$backorders = $variation [ 'backorders' ];
2016-03-30 00:22:10 +00:00
}
update_post_meta ( $variation_id , '_backorders' , '' === $backorders ? 'no' : $backorders );
if ( isset ( $variation [ 'stock_quantity' ] ) ) {
wc_update_product_stock ( $variation_id , wc_stock_amount ( $variation [ 'stock_quantity' ] ) );
2016-09-02 03:15:49 +00:00
} elseif ( isset ( $request [ 'inventory_delta' ] ) ) {
2016-03-30 00:22:10 +00:00
$stock_quantity = wc_stock_amount ( get_post_meta ( $variation_id , '_stock' , true ) );
$stock_quantity += wc_stock_amount ( $request [ 'inventory_delta' ] );
wc_update_product_stock ( $variation_id , wc_stock_amount ( $stock_quantity ) );
}
} else {
delete_post_meta ( $variation_id , '_backorders' );
2016-10-12 11:51:40 +00:00
wc_update_product_stock ( $variation_id , '' );
2016-03-30 00:22:10 +00:00
}
// Regular Price.
if ( isset ( $variation [ 'regular_price' ] ) ) {
$regular_price = ( '' === $variation [ 'regular_price' ] ) ? '' : $variation [ 'regular_price' ];
} else {
$regular_price = get_post_meta ( $variation_id , '_regular_price' , true );
}
// Sale Price.
if ( isset ( $variation [ 'sale_price' ] ) ) {
$sale_price = ( '' === $variation [ 'sale_price' ] ) ? '' : $variation [ 'sale_price' ];
} else {
$sale_price = get_post_meta ( $variation_id , '_sale_price' , true );
}
2016-04-01 17:36:10 +00:00
if ( isset ( $variation [ 'date_on_sale_from' ] ) ) {
$date_from = $variation [ 'date_on_sale_from' ];
2016-03-30 00:22:10 +00:00
} else {
$date_from = get_post_meta ( $variation_id , '_sale_price_dates_from' , true );
$date_from = ( '' === $date_from ) ? '' : date ( 'Y-m-d' , $date_from );
}
2016-04-01 17:36:10 +00:00
if ( isset ( $variation [ 'date_on_sale_to' ] ) ) {
$date_to = $variation [ 'date_on_sale_to' ];
2016-03-30 00:22:10 +00:00
} else {
$date_to = get_post_meta ( $variation_id , '_sale_price_dates_to' , true );
$date_to = ( '' === $date_to ) ? '' : date ( 'Y-m-d' , $date_to );
}
_wc_save_product_price ( $variation_id , $regular_price , $sale_price , $date_from , $date_to );
// Tax class.
if ( isset ( $variation [ 'tax_class' ] ) ) {
2016-09-09 00:14:28 +00:00
if ( 'parent' !== $variation [ 'tax_class' ] ) {
2016-03-30 00:22:10 +00:00
update_post_meta ( $variation_id , '_tax_class' , wc_clean ( $variation [ 'tax_class' ] ) );
} else {
delete_post_meta ( $variation_id , '_tax_class' );
}
}
// Downloads.
2016-04-05 19:58:18 +00:00
if ( 'yes' === $is_downloadable ) {
2016-03-30 00:22:10 +00:00
// Downloadable files.
if ( isset ( $variation [ 'downloads' ] ) && is_array ( $variation [ 'downloads' ] ) ) {
$this -> save_downloadable_files ( $product -> id , $variation [ 'downloads' ], $variation_id );
}
// Download limit.
if ( isset ( $variation [ 'download_limit' ] ) ) {
2016-05-31 20:38:50 +00:00
update_post_meta ( $variation_id , '_download_limit' , - 1 === $variation [ 'download_limit' ] ? '' : absint ( $variation [ 'download_limit' ] ) );
2016-03-30 00:22:10 +00:00
}
// Download expiry.
if ( isset ( $variation [ 'download_expiry' ] ) ) {
2016-05-31 20:38:50 +00:00
update_post_meta ( $variation_id , '_download_expiry' , - 1 === $variation [ 'download_expiry' ] ? '' : absint ( $variation [ 'download_expiry' ] ) );
2016-03-30 00:22:10 +00:00
}
} else {
update_post_meta ( $variation_id , '_download_limit' , '' );
update_post_meta ( $variation_id , '_download_expiry' , '' );
update_post_meta ( $variation_id , '_downloadable_files' , '' );
}
// Description.
if ( isset ( $variation [ 'description' ] ) ) {
update_post_meta ( $variation_id , '_variation_description' , wp_kses_post ( $variation [ 'description' ] ) );
}
// Update taxonomies.
if ( isset ( $variation [ 'attributes' ] ) ) {
$updated_attribute_keys = array ();
2016-06-02 21:12:04 +00:00
foreach ( $variation [ 'attributes' ] as $attribute ) {
$attribute_id = 0 ;
$attribute_name = '' ;
2016-03-30 00:22:10 +00:00
2016-06-02 21:12:04 +00:00
// 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' ] );
2016-03-30 00:22:10 +00:00
}
2016-06-02 21:12:04 +00:00
if ( ! $attribute_id && ! $attribute_name ) {
continue ;
2016-03-30 00:22:10 +00:00
}
2016-06-02 21:12:04 +00:00
if ( isset ( $attributes [ $attribute_name ] ) ) {
$_attribute = $attributes [ $attribute_name ];
2016-03-30 00:22:10 +00:00
}
if ( isset ( $_attribute [ 'is_variation' ] ) && $_attribute [ 'is_variation' ] ) {
$_attribute_key = 'attribute_' . sanitize_title ( $_attribute [ 'name' ] );
$updated_attribute_keys [] = $_attribute_key ;
2016-09-12 13:37:44 +00:00
$attribute_value = isset ( $attribute [ 'option' ] ) ? wc_clean ( stripslashes ( $attribute [ 'option' ] ) ) : '' ;
2016-03-30 00:22:10 +00:00
2016-09-12 13:37:44 +00:00
if ( ! empty ( $_attribute [ '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 );
}
2016-03-30 00:22:10 +00:00
}
2016-09-12 13:37:44 +00:00
update_post_meta ( $variation_id , $_attribute_key , $attribute_value );
2016-03-30 00:22:10 +00:00
}
}
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names.
$delete_attribute_keys = $wpdb -> get_col ( $wpdb -> prepare ( " SELECT meta_key FROM { $wpdb -> postmeta } WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( ' " . implode ( " ',' " , $updated_attribute_keys ) . " ' ) AND post_id = %d; " , $variation_id ) );
foreach ( $delete_attribute_keys as $key ) {
delete_post_meta ( $variation_id , $key );
}
}
do_action ( 'woocommerce_rest_save_product_variation' , $variation_id , $menu_order , $variation );
}
// Update parent if variable so price sorting works and stays in sync with the cheapest child.
WC_Product_Variable :: sync ( $product -> id );
// Update default attributes options setting.
if ( isset ( $request [ 'default_attribute' ] ) ) {
$request [ 'default_attributes' ] = $request [ 'default_attribute' ];
}
if ( isset ( $request [ 'default_attributes' ] ) && is_array ( $request [ 'default_attributes' ] ) ) {
$default_attributes = array ();
2016-06-02 21:12:04 +00:00
foreach ( $request [ 'default_attributes' ] as $attribute ) {
$attribute_id = 0 ;
$attribute_name = '' ;
2016-03-30 00:22:10 +00:00
2016-06-02 21:12:04 +00:00
// 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' ] );
}
2016-03-30 00:22:10 +00:00
2016-06-02 21:12:04 +00:00
if ( ! $attribute_id && ! $attribute_name ) {
continue ;
2016-03-30 00:22:10 +00:00
}
2016-06-02 21:12:04 +00:00
if ( isset ( $attributes [ $attribute_name ] ) ) {
$_attribute = $attributes [ $attribute_name ];
2016-03-30 00:22:10 +00:00
if ( $_attribute [ 'is_variation' ] ) {
2016-09-12 13:37:44 +00:00
$value = isset ( $attribute [ 'option' ] ) ? wc_clean ( stripslashes ( $attribute [ 'option' ] ) ) : '' ;
if ( ! empty ( $_attribute [ '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 );
2016-03-30 00:22:10 +00:00
2016-09-12 13:37:44 +00:00
if ( $term && ! is_wp_error ( $term ) ) {
$value = $term -> slug ;
2016-03-30 00:22:10 +00:00
} else {
2016-09-12 13:37:44 +00:00
$value = sanitize_title ( $attribute_value );
2016-03-30 00:22:10 +00:00
}
}
if ( $value ) {
2016-06-02 21:12:04 +00:00
$default_attributes [ $attribute_name ] = $value ;
2016-03-30 00:22:10 +00:00
}
}
}
}
update_post_meta ( $product -> id , '_default_attributes' , $default_attributes );
}
return true ;
}
/**
* Add post meta fields .
*
* @ param WP_Post $post
* @ param WP_REST_Request $request
* @ return bool | WP_Error
*/
protected function add_post_meta_fields ( $post , $request ) {
try {
$product = wc_get_product ( $post );
// Check for featured/gallery images, upload it and set it.
if ( isset ( $request [ 'images' ] ) ) {
2016-07-13 08:30:00 +00:00
$this -> save_product_images ( $product -> id , $request [ 'images' ] );
2016-03-30 00:22:10 +00:00
}
// Save product meta fields.
$this -> save_product_meta ( $product , $request );
// Save variations.
2016-04-05 19:58:18 +00:00
if ( isset ( $request [ 'type' ] ) && 'variable' === $request [ 'type' ] && isset ( $request [ 'variations' ] ) && is_array ( $request [ 'variations' ] ) ) {
2016-03-30 01:06:05 +00:00
$this -> save_variations_data ( $product , $request );
2016-03-30 00:22:10 +00:00
}
return true ;
} catch ( WC_REST_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Update post meta fields .
*
* @ param WP_Post $post
* @ param WP_REST_Request $request
* @ return bool | WP_Error
*/
protected function update_post_meta_fields ( $post , $request ) {
try {
$product = wc_get_product ( $post );
// Check for featured/gallery images, upload it and set it.
if ( isset ( $request [ 'images' ] ) ) {
2016-07-13 08:30:00 +00:00
$this -> save_product_images ( $product -> id , $request [ 'images' ] );
2016-03-30 00:22:10 +00:00
}
// Save product meta fields.
$this -> save_product_meta ( $product , $request );
// Save variations.
if ( $product -> is_type ( 'variable' ) ) {
if ( isset ( $request [ 'variations' ] ) && is_array ( $request [ 'variations' ] ) ) {
2016-03-30 01:06:05 +00:00
$this -> save_variations_data ( $product , $request );
2016-03-30 00:22:10 +00:00
} else {
// Just sync variations.
WC_Product_Variable :: sync ( $product -> id );
WC_Product_Variable :: sync_stock_status ( $product -> id );
}
}
return true ;
} catch ( WC_REST_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Clear cache / transients .
*
* @ param WP_Post $post Post data .
*/
public function clear_transients ( $post ) {
wc_delete_product_transients ( $post -> ID );
}
/**
* Delete post .
*
* @ param WP_Post $post
*/
protected function delete_post ( $post ) {
// Delete product attachments.
$attachments = get_children ( array (
'post_parent' => $post -> ID ,
'post_status' => 'any' ,
'post_type' => 'attachment' ,
) );
foreach ( ( array ) $attachments as $attachment ) {
wp_delete_attachment ( $attachment -> ID , true );
}
// Delete product.
wp_delete_post ( $post -> ID , true );
}
2016-07-04 19:42:07 +00:00
/**
* Delete a single item .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Response | WP_Error
*/
public function delete_item ( $request ) {
$id = ( int ) $request [ 'id' ];
$force = ( bool ) $request [ 'force' ];
$post = get_post ( $id );
if ( empty ( $id ) || empty ( $post -> ID ) || ! in_array ( $post -> post_type , $this -> get_post_types () ) ) {
2016-07-26 13:47:04 +00:00
return new WP_Error ( " woocommerce_rest_ { $this -> post_type } _invalid_id " , __ ( 'Invalid post ID.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-07-04 19:42:07 +00:00
}
$supports_trash = EMPTY_TRASH_DAYS > 0 ;
/**
* Filter whether an item is trashable .
*
* Return false to disable trash support for the item .
*
* @ param boolean $supports_trash Whether the item type support trashing .
* @ param WP_Post $post The Post object being considered for trashing support .
*/
$supports_trash = apply_filters ( " woocommerce_rest_ { $this -> post_type } _trashable " , $supports_trash , $post );
if ( ! wc_rest_check_post_permissions ( $this -> post_type , 'delete' , $post -> ID ) ) {
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_item_for_response ( $post , $request );
// If we're forcing, then delete permanently.
if ( $force ) {
$child_product_variations = get_children ( 'post_parent=' . $id . '&post_type=product_variation' );
if ( ! empty ( $child_product_variations ) ) {
foreach ( $child_product_variations as $child ) {
wp_delete_post ( $child -> ID , true );
}
}
$child_products = get_children ( 'post_parent=' . $id . '&post_type=product' );
if ( ! empty ( $child_products ) ) {
foreach ( $child_products as $child ) {
$child_post = array ();
$child_post [ 'ID' ] = $child -> ID ;
$child_post [ 'post_parent' ] = 0 ;
wp_update_post ( $child_post );
}
}
$result = wp_delete_post ( $id , true );
} else {
// If we don't support trashing for this type, error out.
if ( ! $supports_trash ) {
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 ( 'trash' === $post -> post_status ) {
return new WP_Error ( 'woocommerce_rest_already_trashed' , sprintf ( __ ( 'The %s has already been deleted.' , 'woocommerce' ), $this -> post_type ), array ( 'status' => 410 ) );
}
// (Note that internally this falls through to `wp_delete_post` if
// the trash is disabled.)
$result = wp_trash_post ( $id );
}
if ( ! $result ) {
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 ( $parent_id = wp_get_post_parent_id ( $id ) ) {
wc_delete_product_transients ( $parent_id );
}
/**
* Fires after a single item is deleted or trashed via the REST API .
*
* @ param object $post The deleted or trashed item .
* @ 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 } " , $post , $response , $request );
return $response ;
}
2016-03-29 18:48:08 +00:00
/**
2016-03-29 20:24:32 +00:00
* Get the Product ' s schema , conforming to JSON Schema .
2016-03-29 18:48:08 +00:00
*
* @ return array
*/
public function get_item_schema () {
2016-05-31 23:34:15 +00:00
$weight_unit = get_option ( 'woocommerce_weight_unit' );
$dimension_unit = get_option ( 'woocommerce_dimension_unit' );
$schema = array (
2016-03-29 18:48:08 +00:00
'$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 ,
),
'name' => array (
'description' => __ ( 'Product name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-03-29 20:20:15 +00:00
'slug' => array (
'description' => __ ( 'Product slug.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-03-29 18:48:08 +00:00
'permalink' => array (
'description' => __ ( 'Product URL.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_created' => array (
'description' => __ ( " The date the product was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_modified' => array (
'description' => __ ( " The date the product was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'type' => array (
'description' => __ ( 'Product type.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'simple' ,
'enum' => array_keys ( wc_get_product_types () ),
'context' => array ( 'view' , 'edit' ),
),
'status' => array (
'description' => __ ( 'Product status (post status).' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'publish' ,
'enum' => array_keys ( get_post_statuses () ),
'context' => array ( 'view' , 'edit' ),
),
'featured' => array (
'description' => __ ( 'Featured product.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'catalog_visibility' => array (
'description' => __ ( 'Catalog visibility.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'visible' ,
'enum' => array ( 'visible' , 'catalog' , 'search' , 'hidden' ),
'context' => array ( 'view' , 'edit' ),
),
'description' => array (
'description' => __ ( 'Product description.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'short_description' => array (
'description' => __ ( 'Product short description.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'sku' => array (
'description' => __ ( 'Unique identifier.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'price' => array (
'description' => __ ( 'Current product price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'regular_price' => array (
'description' => __ ( 'Product regular price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'sale_price' => array (
'description' => __ ( 'Product sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:36:10 +00:00
'date_on_sale_from' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'Start date of sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:36:10 +00:00
'date_on_sale_to' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'End data of sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'price_html' => array (
'description' => __ ( 'Price formatted in HTML.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'on_sale' => array (
'description' => __ ( 'Shows if the product is on sale.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-04-01 17:36:10 +00:00
'purchasable' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'Shows if the product can be bought.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total_sales' => array (
'description' => __ ( 'Amount of sales.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'virtual' => array (
'description' => __ ( 'If the product is virtual.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'downloadable' => array (
'description' => __ ( 'If the product is downloadable.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'downloads' => array (
'description' => __ ( 'List of downloadable files.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'File MD5 hash.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'name' => array (
'description' => __ ( 'File name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'file' => array (
'description' => __ ( 'File URL.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'download_limit' => array (
'description' => __ ( 'Amount of times the product can be downloaded.' , 'woocommerce' ),
'type' => 'integer' ,
2016-05-31 20:39:34 +00:00
'default' => - 1 ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
'download_expiry' => array (
'description' => __ ( 'Number of days that the customer has up to be able to download the product.' , 'woocommerce' ),
'type' => 'integer' ,
2016-05-31 20:39:34 +00:00
'default' => - 1 ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
'download_type' => array (
'description' => __ ( 'Download type, this controls the schema on the front-end.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'standard' ,
'enum' => array ( 'standard' , 'application' , 'music' ),
'context' => array ( 'view' , 'edit' ),
),
'external_url' => array (
'description' => __ ( 'Product external URL. Only for external products.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
),
'button_text' => array (
'description' => __ ( 'Product external button text. Only for external products.' , 'woocommerce' ),
'type' => 'string' ,
'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' ),
),
2016-04-01 17:36:10 +00:00
'manage_stock' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'Stock management at product 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 product 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 (
2016-06-04 13:34:37 +00:00
'description' => __ ( 'Shows if the product is on backordered.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'sold_individually' => array (
'description' => __ ( 'Allow one item to be bought in a single order.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'weight' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Product weight (%s).' , 'woocommerce' ), $weight_unit ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:57:25 +00:00
'dimensions' => array (
'description' => __ ( 'Product dimensions.' , 'woocommerce' ),
'type' => 'array' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
2016-04-01 17:57:25 +00:00
'properties' => array (
'length' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Product length (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'width' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Product width (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'height' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Product height (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
2016-03-29 18:48:08 +00:00
),
'shipping_required' => array (
'description' => __ ( 'Shows if the product need to be shipped.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'shipping_taxable' => array (
'description' => __ ( 'Shows whether or not the product shipping is taxable.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'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 ,
),
'reviews_allowed' => array (
'description' => __ ( 'Allow reviews.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => true ,
'context' => array ( 'view' , 'edit' ),
),
'average_rating' => array (
'description' => __ ( 'Reviews average rating.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'rating_count' => array (
'description' => __ ( 'Amount of reviews that the product have.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'related_ids' => array (
'description' => __ ( 'List of related products IDs.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'upsell_ids' => array (
'description' => __ ( 'List of up-sell products IDs.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
'cross_sell_ids' => array (
'description' => __ ( 'List of cross-sell products IDs.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
'parent_id' => array (
'description' => __ ( 'Product parent ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'purchase_note' => array (
'description' => __ ( 'Optional note to send the customer after purchase.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'categories' => array (
'description' => __ ( 'List of categories.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'Category ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'name' => array (
'description' => __ ( 'Category name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'slug' => array (
'description' => __ ( 'Category slug.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
'tags' => array (
'description' => __ ( 'List of tags.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'Tag ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'name' => array (
'description' => __ ( 'Tag name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'slug' => array (
'description' => __ ( 'Tag slug.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
'images' => array (
'description' => __ ( 'List of images.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'Image ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'date_created' => array (
'description' => __ ( " The date the image was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_modified' => array (
'description' => __ ( " The date the image was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'src' => array (
'description' => __ ( 'Image URL.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
),
'name' => array (
'description' => __ ( 'Image name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'alt' => array (
'description' => __ ( 'Image alternative text.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'position' => array (
'description' => __ ( 'Image position. 0 means that the image is featured.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'attributes' => array (
'description' => __ ( 'List of attributes.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-06-02 20:34:31 +00:00
'id' => array (
'description' => __ ( 'Attribute ID.' , 'woocommerce' ),
'type' => 'integer' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
2016-06-02 20:34:31 +00:00
'name' => array (
'description' => __ ( 'Attribute name.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'position' => array (
'description' => __ ( 'Attribute position.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'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-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'variation' => array (
'description' => __ ( 'Define if the attribute can be used as variation.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'options' => array (
'description' => __ ( 'List of available term names of the attribute.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'default_attributes' => array (
'description' => __ ( 'Defaults variation attributes.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-06-02 20:34:31 +00:00
'id' => array (
'description' => __ ( 'Attribute ID.' , 'woocommerce' ),
'type' => 'integer' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
2016-06-02 20:34:31 +00:00
'name' => array (
'description' => __ ( 'Attribute name.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'option' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Selected attribute term name.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'variations' => array (
'description' => __ ( 'List of variations.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'Variation ID.' , '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 ,
),
'permalink' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Variation URL.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-09-13 22:28:25 +00:00
'description' => array (
'description' => __ ( 'Product description.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-03-29 18:48:08 +00:00
'sku' => array (
'description' => __ ( 'Unique identifier.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'price' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Current variation price.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'regular_price' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Variation regular price.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'sale_price' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Variation sale price.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:36:10 +00:00
'date_on_sale_from' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'Start date of sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:36:10 +00:00
'date_on_sale_to' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'End data of sale price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'on_sale' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Shows if the variation is on sale.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-04-01 17:36:10 +00:00
'purchasable' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Shows if the variation can be bought.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-07-14 06:41:02 +00:00
'visible' => array (
'description' => __ ( 'If the variation is visible.' , 'woocommerce' ),
'type' => 'boolean' ,
2016-08-27 01:46:45 +00:00
'context' => array ( 'view' , 'edit' ),
2016-07-14 06:41:02 +00:00
),
2016-03-29 18:48:08 +00:00
'virtual' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'If the variation is virtual.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'downloadable' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'If the variation is downloadable.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'downloads' => array (
'description' => __ ( 'List of downloadable files.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'File MD5 hash.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'name' => array (
'description' => __ ( 'File name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'file' => array (
'description' => __ ( 'File URL.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'download_limit' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Amount of times the variation can be downloaded.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'integer' ,
'default' => null ,
'context' => array ( 'view' , 'edit' ),
),
'download_expiry' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Number of days that the customer has up to be able to download the variation.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'integer' ,
'default' => null ,
'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' ),
),
2016-04-01 17:36:10 +00:00
'manage_stock' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Stock management at variation level.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'stock_quantity' => array (
'description' => __ ( 'Stock quantity.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'in_stock' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Controls whether or not the variation is listed as "in stock" or "out of stock" on the frontend.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'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 (
2016-06-04 13:34:37 +00:00
'description' => __ ( 'Shows if the variation is on backordered.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'weight' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Variation weight (%s).' , 'woocommerce' ), $weight_unit ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-04-01 17:57:25 +00:00
'dimensions' => array (
2016-05-31 23:34:15 +00:00
'description' => __ ( 'Variation dimensions.' , 'woocommerce' ),
2016-04-01 17:57:25 +00:00
'type' => 'array' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
2016-04-01 17:57:25 +00:00
'properties' => array (
'length' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Variation length (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'width' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Variation width (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'height' => array (
2016-05-31 23:34:15 +00:00
'description' => sprintf ( __ ( 'Variation height (%s).' , 'woocommerce' ), $dimension_unit ),
2016-04-01 17:57:25 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
2016-03-29 18:48:08 +00:00
),
'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 (
2016-06-05 22:27:53 +00:00
'description' => __ ( 'Variation image data.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'id' => array (
'description' => __ ( 'Image ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'date_created' => array (
'description' => __ ( " The date the image was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_modified' => array (
'description' => __ ( " The date the image was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'src' => array (
'description' => __ ( 'Image URL.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'uri' ,
'context' => array ( 'view' , 'edit' ),
),
'name' => array (
'description' => __ ( 'Image name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'alt' => array (
'description' => __ ( 'Image alternative text.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'position' => array (
'description' => __ ( 'Image position. 0 means that the image is featured.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
'attributes' => array (
'description' => __ ( 'List of attributes.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-06-02 20:34:31 +00:00
'id' => array (
'description' => __ ( 'Attribute ID.' , 'woocommerce' ),
'type' => 'integer' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
2016-06-02 20:34:31 +00:00
'name' => array (
'description' => __ ( 'Attribute name.' , 'woocommerce' ),
2016-03-29 18:48:08 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-05-31 23:34:15 +00:00
'option' => array (
'description' => __ ( 'Selected attribute term name.' , 'woocommerce' ),
'type' => 'string' ,
2016-03-29 18:48:08 +00:00
'context' => array ( 'view' , 'edit' ),
),
),
),
),
),
2016-06-14 22:20:07 +00:00
'grouped_products' => array (
2016-03-29 18:48:08 +00:00
'description' => __ ( 'List of grouped products ID.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
2016-04-01 17:36:10 +00:00
'readonly' => true ,
2016-03-29 18:48:08 +00:00
),
'menu_order' => array (
'description' => __ ( 'Menu order, used to custom sort products.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
2016-03-29 20:20:15 +00:00
/**
* Get the query params for collections of attachments .
*
* @ return array
*/
public function get_collection_params () {
$params = parent :: get_collection_params ();
2016-05-30 20:42:42 +00:00
$params [ 'slug' ] = array (
2016-09-01 20:50:14 +00:00
'description' => __ ( 'Limit result set to products with a specific slug.' , 'woocommerce' ),
2016-05-30 20:42:42 +00:00
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-29 20:20:15 +00:00
$params [ 'status' ] = array (
'default' => 'any' ,
'description' => __ ( 'Limit result set to products assigned a specific status.' , 'woocommerce' ),
'type' => 'string' ,
'enum' => array_merge ( array ( 'any' ), array_keys ( get_post_statuses () ) ),
'sanitize_callback' => 'sanitize_key' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-30 23:05:12 +00:00
$params [ 'type' ] = array (
'description' => __ ( 'Limit result set to products assigned a specific type.' , 'woocommerce' ),
'type' => 'string' ,
'enum' => array_keys ( wc_get_product_types () ),
'sanitize_callback' => 'sanitize_key' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-10-11 02:09:47 +00:00
$params [ 'sku' ] = array (
'description' => __ ( 'Limit result set to products with a specific SKU.' , 'woocommerce' ),
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'featured' ] = array (
'description' => __ ( 'Limit result set to featured products.' , 'woocommerce' ),
'type' => 'boolean' ,
'sanitize_callback' => 'wp_validate_boolean' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-30 23:05:12 +00:00
$params [ 'category' ] = array (
2016-10-11 01:12:03 +00:00
'description' => __ ( 'Limit result set to products assigned a specific category ID.' , 'woocommerce' ),
2016-03-30 23:05:12 +00:00
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'tag' ] = array (
2016-10-11 01:12:03 +00:00
'description' => __ ( 'Limit result set to products assigned a specific tag ID.' , 'woocommerce' ),
2016-03-30 23:05:12 +00:00
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'shipping_class' ] = array (
2016-10-11 01:12:03 +00:00
'description' => __ ( 'Limit result set to products assigned a specific shipping class ID.' , 'woocommerce' ),
2016-03-30 23:05:12 +00:00
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'attribute' ] = array (
'description' => __ ( 'Limit result set to products with a specific attribute.' , 'woocommerce' ),
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'attribute_term' ] = array (
2016-10-11 01:12:03 +00:00
'description' => __ ( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).' , 'woocommerce' ),
2016-03-30 23:05:12 +00:00
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-29 20:20:15 +00:00
return $params ;
}
2016-02-17 19:29:09 +00:00
}