2019-05-10 16:56:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Products controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /products endpoint.
|
|
|
|
*
|
2020-09-17 14:56:08 +00:00
|
|
|
* @package WooCommerce\RestApi
|
2019-05-10 16:56:07 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Products controller class.
|
|
|
|
*
|
2020-09-17 14:56:08 +00:00
|
|
|
* @package WooCommerce\RestApi
|
2019-05-10 16:56:07 +00:00
|
|
|
* @extends WC_REST_Products_V2_Controller
|
|
|
|
*/
|
|
|
|
class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace = 'wc/v3';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the images for a product or product variation.
|
|
|
|
*
|
|
|
|
* @param WC_Product|WC_Product_Variation $product Product instance.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_images( $product ) {
|
|
|
|
$images = array();
|
|
|
|
$attachment_ids = array();
|
|
|
|
|
|
|
|
// Add featured image.
|
|
|
|
if ( $product->get_image_id() ) {
|
|
|
|
$attachment_ids[] = $product->get_image_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add gallery images.
|
|
|
|
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
|
|
|
|
|
|
|
|
// Build image data.
|
|
|
|
foreach ( $attachment_ids as $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,
|
|
|
|
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
|
|
|
|
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
|
|
|
|
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
|
|
|
|
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
|
|
|
|
'src' => current( $attachment ),
|
|
|
|
'name' => get_the_title( $attachment_id ),
|
|
|
|
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $images;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make extra product orderby features supported by WooCommerce available to the WC API.
|
|
|
|
* This includes 'price', 'popularity', and 'rating'.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_objects_query( $request ) {
|
|
|
|
$args = WC_REST_CRUD_Controller::prepare_objects_query( $request );
|
|
|
|
|
|
|
|
// Set post_status.
|
|
|
|
$args['post_status'] = $request['status'];
|
|
|
|
|
|
|
|
// 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 ] ) ) {
|
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => $taxonomy,
|
|
|
|
'field' => 'term_id',
|
|
|
|
'terms' => $request[ $key ],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter product type by slug.
|
|
|
|
if ( ! empty( $request['type'] ) ) {
|
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => 'product_type',
|
|
|
|
'field' => 'slug',
|
|
|
|
'terms' => $request['type'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by attribute and term.
|
|
|
|
if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) {
|
|
|
|
if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
|
|
|
|
$tax_query[] = array(
|
|
|
|
'taxonomy' => $request['attribute'],
|
|
|
|
'field' => 'term_id',
|
|
|
|
'terms' => $request['attribute_term'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build tax_query if taxonomies are set.
|
|
|
|
if ( ! empty( $tax_query ) ) {
|
|
|
|
if ( ! empty( $args['tax_query'] ) ) {
|
|
|
|
$args['tax_query'] = array_merge( $tax_query, $args['tax_query'] ); // WPCS: slow query ok.
|
|
|
|
} else {
|
|
|
|
$args['tax_query'] = $tax_query; // WPCS: slow query ok.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter featured.
|
|
|
|
if ( is_bool( $request['featured'] ) ) {
|
|
|
|
$args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'field' => 'name',
|
|
|
|
'terms' => 'featured',
|
|
|
|
'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by sku.
|
|
|
|
if ( ! empty( $request['sku'] ) ) {
|
|
|
|
$skus = explode( ',', $request['sku'] );
|
|
|
|
// Include the current string as a SKU too.
|
|
|
|
if ( 1 < count( $skus ) ) {
|
|
|
|
$skus[] = $request['sku'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
|
2021-01-06 12:25:56 +00:00
|
|
|
$args,
|
|
|
|
array(
|
2019-05-10 16:56:07 +00:00
|
|
|
'key' => '_sku',
|
|
|
|
'value' => $skus,
|
|
|
|
'compare' => 'IN',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by tax class.
|
|
|
|
if ( ! empty( $request['tax_class'] ) ) {
|
|
|
|
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
|
2021-01-06 12:25:56 +00:00
|
|
|
$args,
|
|
|
|
array(
|
2019-05-10 16:56:07 +00:00
|
|
|
'key' => '_tax_class',
|
|
|
|
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Price filter.
|
|
|
|
if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) {
|
|
|
|
$args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); // WPCS: slow query ok.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter product by stock_status.
|
|
|
|
if ( ! empty( $request['stock_status'] ) ) {
|
|
|
|
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
|
2021-01-06 12:25:56 +00:00
|
|
|
$args,
|
|
|
|
array(
|
2019-05-10 16:56:07 +00:00
|
|
|
'key' => '_stock_status',
|
|
|
|
'value' => $request['stock_status'],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by on sale products.
|
|
|
|
if ( is_bool( $request['on_sale'] ) ) {
|
|
|
|
$on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in';
|
|
|
|
$on_sale_ids = wc_get_product_ids_on_sale();
|
|
|
|
|
|
|
|
// Use 0 when there's no on sale products to avoid return all products.
|
|
|
|
$on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids;
|
|
|
|
|
|
|
|
$args[ $on_sale_key ] += $on_sale_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the post_type argument, since it's not a user input variable.
|
|
|
|
if ( ! empty( $request['sku'] ) ) {
|
|
|
|
$args['post_type'] = array( 'product', 'product_variation' );
|
|
|
|
} else {
|
|
|
|
$args['post_type'] = $this->post_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderby = $request->get_param( 'orderby' );
|
|
|
|
$order = $request->get_param( 'order' );
|
|
|
|
|
|
|
|
$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
|
|
|
|
$args['orderby'] = $ordering_args['orderby'];
|
|
|
|
$args['order'] = $ordering_args['order'];
|
|
|
|
if ( $ordering_args['meta_key'] ) {
|
|
|
|
$args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok.
|
|
|
|
}
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set product images.
|
|
|
|
*
|
|
|
|
* @throws WC_REST_Exception REST API exceptions.
|
|
|
|
* @param WC_Product $product Product instance.
|
|
|
|
* @param array $images Images data.
|
|
|
|
* @return WC_Product
|
|
|
|
*/
|
|
|
|
protected function set_product_images( $product, $images ) {
|
|
|
|
$images = is_array( $images ) ? array_filter( $images ) : array();
|
|
|
|
|
|
|
|
if ( ! empty( $images ) ) {
|
|
|
|
$gallery = array();
|
|
|
|
|
|
|
|
foreach ( $images as $index => $image ) {
|
|
|
|
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
|
|
|
|
|
|
|
|
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
|
|
|
|
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
|
|
|
|
|
|
|
|
if ( is_wp_error( $upload ) ) {
|
|
|
|
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images ) ) {
|
|
|
|
throw new WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! wp_attachment_is_image( $attachment_id ) ) {
|
|
|
|
/* translators: %s: image ID */
|
2020-08-06 12:48:18 +00:00
|
|
|
throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
|
2019-05-10 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$featured_image = $product->get_image_id();
|
|
|
|
|
|
|
|
if ( 0 === $index ) {
|
|
|
|
$product->set_image_id( $attachment_id );
|
|
|
|
} else {
|
|
|
|
$gallery[] = $attachment_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the image alt if present.
|
|
|
|
if ( ! empty( $image['alt'] ) ) {
|
|
|
|
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the image name if present.
|
|
|
|
if ( ! empty( $image['name'] ) ) {
|
|
|
|
wp_update_post(
|
|
|
|
array(
|
|
|
|
'ID' => $attachment_id,
|
|
|
|
'post_title' => $image['name'],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$product->set_gallery_image_ids( $gallery );
|
|
|
|
} else {
|
|
|
|
$product->set_image_id( '' );
|
|
|
|
$product->set_gallery_image_ids( array() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare a single product for create or update.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request object.
|
|
|
|
* @param bool $creating If is creating a new object.
|
|
|
|
* @return WP_Error|WC_Data
|
|
|
|
*/
|
|
|
|
protected function prepare_object_for_database( $request, $creating = false ) {
|
|
|
|
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
|
|
|
|
|
|
|
|
// Type is the most important part here because we need to be using the correct class and methods.
|
|
|
|
if ( isset( $request['type'] ) ) {
|
|
|
|
$classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] );
|
|
|
|
|
|
|
|
if ( ! class_exists( $classname ) ) {
|
|
|
|
$classname = 'WC_Product_Simple';
|
|
|
|
}
|
|
|
|
|
|
|
|
$product = new $classname( $id );
|
|
|
|
} elseif ( isset( $request['id'] ) ) {
|
|
|
|
$product = wc_get_product( $id );
|
|
|
|
} else {
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'variation' === $product->get_type() ) {
|
|
|
|
return new WP_Error(
|
2021-01-06 12:25:56 +00:00
|
|
|
"woocommerce_rest_invalid_{$this->post_type}_id",
|
|
|
|
__( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ),
|
|
|
|
array(
|
2019-05-10 16:56:07 +00:00
|
|
|
'status' => 404,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post title.
|
|
|
|
if ( isset( $request['name'] ) ) {
|
|
|
|
$product->set_name( wp_filter_post_kses( $request['name'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post content.
|
|
|
|
if ( isset( $request['description'] ) ) {
|
|
|
|
$product->set_description( wp_filter_post_kses( $request['description'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post excerpt.
|
|
|
|
if ( isset( $request['short_description'] ) ) {
|
|
|
|
$product->set_short_description( wp_filter_post_kses( $request['short_description'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post status.
|
|
|
|
if ( isset( $request['status'] ) ) {
|
|
|
|
$product->set_status( get_post_status_object( $request['status'] ) ? $request['status'] : 'draft' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post slug.
|
|
|
|
if ( isset( $request['slug'] ) ) {
|
|
|
|
$product->set_slug( $request['slug'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Menu order.
|
|
|
|
if ( isset( $request['menu_order'] ) ) {
|
|
|
|
$product->set_menu_order( $request['menu_order'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment status.
|
|
|
|
if ( isset( $request['reviews_allowed'] ) ) {
|
|
|
|
$product->set_reviews_allowed( $request['reviews_allowed'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Virtual.
|
|
|
|
if ( isset( $request['virtual'] ) ) {
|
|
|
|
$product->set_virtual( $request['virtual'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tax status.
|
|
|
|
if ( isset( $request['tax_status'] ) ) {
|
|
|
|
$product->set_tax_status( $request['tax_status'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tax Class.
|
|
|
|
if ( isset( $request['tax_class'] ) ) {
|
|
|
|
$product->set_tax_class( $request['tax_class'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catalog Visibility.
|
|
|
|
if ( isset( $request['catalog_visibility'] ) ) {
|
|
|
|
$product->set_catalog_visibility( $request['catalog_visibility'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purchase Note.
|
|
|
|
if ( isset( $request['purchase_note'] ) ) {
|
|
|
|
$product->set_purchase_note( wp_kses_post( wp_unslash( $request['purchase_note'] ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Featured Product.
|
|
|
|
if ( isset( $request['featured'] ) ) {
|
|
|
|
$product->set_featured( $request['featured'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shipping data.
|
|
|
|
$product = $this->save_product_shipping_data( $product, $request );
|
|
|
|
|
|
|
|
// SKU.
|
|
|
|
if ( isset( $request['sku'] ) ) {
|
|
|
|
$product->set_sku( wc_clean( $request['sku'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes.
|
|
|
|
if ( isset( $request['attributes'] ) ) {
|
|
|
|
$attributes = array();
|
|
|
|
|
|
|
|
foreach ( $request['attributes'] as $attribute ) {
|
|
|
|
$attribute_id = 0;
|
|
|
|
$attribute_name = '';
|
|
|
|
|
|
|
|
// Check ID for global attributes or name for product attributes.
|
|
|
|
if ( ! empty( $attribute['id'] ) ) {
|
|
|
|
$attribute_id = absint( $attribute['id'] );
|
|
|
|
$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
|
|
|
|
} elseif ( ! empty( $attribute['name'] ) ) {
|
|
|
|
$attribute_name = wc_clean( $attribute['name'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $attribute_id && ! $attribute_name ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $attribute_id ) {
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $values ) ) {
|
|
|
|
// Add attribute to array, but don't set values.
|
|
|
|
$attribute_object = new WC_Product_Attribute();
|
|
|
|
$attribute_object->set_id( $attribute_id );
|
|
|
|
$attribute_object->set_name( $attribute_name );
|
|
|
|
$attribute_object->set_options( $values );
|
|
|
|
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
|
|
|
|
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
|
|
|
|
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
|
|
|
|
$attributes[] = $attribute_object;
|
|
|
|
}
|
|
|
|
} elseif ( isset( $attribute['options'] ) ) {
|
|
|
|
// Custom attribute - Add attribute to array and set the values.
|
|
|
|
if ( is_array( $attribute['options'] ) ) {
|
|
|
|
$values = $attribute['options'];
|
|
|
|
} else {
|
|
|
|
$values = explode( WC_DELIMITER, $attribute['options'] );
|
|
|
|
}
|
|
|
|
$attribute_object = new WC_Product_Attribute();
|
|
|
|
$attribute_object->set_name( $attribute_name );
|
|
|
|
$attribute_object->set_options( $values );
|
|
|
|
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
|
|
|
|
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
|
|
|
|
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
|
|
|
|
$attributes[] = $attribute_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$product->set_attributes( $attributes );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sales and prices.
|
|
|
|
if ( in_array( $product->get_type(), array( 'variable', 'grouped' ), true ) ) {
|
|
|
|
$product->set_regular_price( '' );
|
|
|
|
$product->set_sale_price( '' );
|
|
|
|
$product->set_date_on_sale_to( '' );
|
|
|
|
$product->set_date_on_sale_from( '' );
|
|
|
|
$product->set_price( '' );
|
|
|
|
} else {
|
|
|
|
// Regular Price.
|
|
|
|
if ( isset( $request['regular_price'] ) ) {
|
|
|
|
$product->set_regular_price( $request['regular_price'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sale Price.
|
|
|
|
if ( isset( $request['sale_price'] ) ) {
|
|
|
|
$product->set_sale_price( $request['sale_price'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $request['date_on_sale_from'] ) ) {
|
|
|
|
$product->set_date_on_sale_from( $request['date_on_sale_from'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $request['date_on_sale_from_gmt'] ) ) {
|
|
|
|
$product->set_date_on_sale_from( $request['date_on_sale_from_gmt'] ? strtotime( $request['date_on_sale_from_gmt'] ) : null );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $request['date_on_sale_to'] ) ) {
|
|
|
|
$product->set_date_on_sale_to( $request['date_on_sale_to'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $request['date_on_sale_to_gmt'] ) ) {
|
|
|
|
$product->set_date_on_sale_to( $request['date_on_sale_to_gmt'] ? strtotime( $request['date_on_sale_to_gmt'] ) : null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Product parent ID.
|
|
|
|
if ( isset( $request['parent_id'] ) ) {
|
|
|
|
$product->set_parent_id( $request['parent_id'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sold individually.
|
|
|
|
if ( isset( $request['sold_individually'] ) ) {
|
|
|
|
$product->set_sold_individually( $request['sold_individually'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stock status; stock_status has priority over in_stock.
|
|
|
|
if ( isset( $request['stock_status'] ) ) {
|
|
|
|
$stock_status = $request['stock_status'];
|
|
|
|
} else {
|
|
|
|
$stock_status = $product->get_stock_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stock data.
|
|
|
|
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
|
|
|
|
// Manage stock.
|
|
|
|
if ( isset( $request['manage_stock'] ) ) {
|
|
|
|
$product->set_manage_stock( $request['manage_stock'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backorders.
|
|
|
|
if ( isset( $request['backorders'] ) ) {
|
|
|
|
$product->set_backorders( $request['backorders'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $product->is_type( 'grouped' ) ) {
|
|
|
|
$product->set_manage_stock( 'no' );
|
|
|
|
$product->set_backorders( 'no' );
|
|
|
|
$product->set_stock_quantity( '' );
|
|
|
|
$product->set_stock_status( $stock_status );
|
|
|
|
} elseif ( $product->is_type( 'external' ) ) {
|
|
|
|
$product->set_manage_stock( 'no' );
|
|
|
|
$product->set_backorders( 'no' );
|
|
|
|
$product->set_stock_quantity( '' );
|
|
|
|
$product->set_stock_status( 'instock' );
|
|
|
|
} elseif ( $product->get_manage_stock() ) {
|
|
|
|
// Stock status is always determined by children so sync later.
|
|
|
|
if ( ! $product->is_type( 'variable' ) ) {
|
|
|
|
$product->set_stock_status( $stock_status );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stock quantity.
|
|
|
|
if ( isset( $request['stock_quantity'] ) ) {
|
|
|
|
$product->set_stock_quantity( wc_stock_amount( $request['stock_quantity'] ) );
|
|
|
|
} elseif ( isset( $request['inventory_delta'] ) ) {
|
|
|
|
$stock_quantity = wc_stock_amount( $product->get_stock_quantity() );
|
|
|
|
$stock_quantity += wc_stock_amount( $request['inventory_delta'] );
|
|
|
|
$product->set_stock_quantity( wc_stock_amount( $stock_quantity ) );
|
|
|
|
}
|
2021-03-11 15:21:43 +00:00
|
|
|
|
|
|
|
// Low stock amount.
|
2021-03-15 11:37:39 +00:00
|
|
|
// isset() returns false for value null, thus we need to check whether the value has been sent by the request.
|
|
|
|
if ( array_key_exists( 'low_stock_amount', $request->get_params() ) ) {
|
|
|
|
if ( null === $request['low_stock_amount'] ) {
|
|
|
|
$product->set_low_stock_amount( '' );
|
|
|
|
} else {
|
|
|
|
$product->set_low_stock_amount( wc_stock_amount( $request['low_stock_amount'] ) );
|
|
|
|
}
|
2021-03-11 15:21:43 +00:00
|
|
|
}
|
2019-05-10 16:56:07 +00:00
|
|
|
} else {
|
|
|
|
// Don't manage stock.
|
|
|
|
$product->set_manage_stock( 'no' );
|
|
|
|
$product->set_stock_quantity( '' );
|
|
|
|
$product->set_stock_status( $stock_status );
|
2021-03-11 15:21:43 +00:00
|
|
|
$product->set_low_stock_amount( '' );
|
2019-05-10 16:56:07 +00:00
|
|
|
}
|
|
|
|
} elseif ( ! $product->is_type( 'variable' ) ) {
|
|
|
|
$product->set_stock_status( $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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$product->set_upsell_ids( $upsells );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$product->set_cross_sell_ids( $crosssells );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Product categories.
|
|
|
|
if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) {
|
|
|
|
$product = $this->save_taxonomy_terms( $product, $request['categories'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Product tags.
|
|
|
|
if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) {
|
2021-01-04 22:59:28 +00:00
|
|
|
$new_tags = array();
|
|
|
|
|
|
|
|
foreach ( $request['tags'] as $tag ) {
|
|
|
|
if ( ! isset( $tag['name'] ) ) {
|
|
|
|
$new_tags[] = $tag;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! term_exists( $tag['name'], 'product_tag' ) ) {
|
|
|
|
// Create the tag if it doesn't exist.
|
2021-01-05 16:14:39 +00:00
|
|
|
$term = wp_insert_term( $tag['name'], 'product_tag' );
|
2021-01-04 22:59:28 +00:00
|
|
|
|
2021-01-05 16:14:39 +00:00
|
|
|
if ( ! is_wp_error( $term ) ) {
|
2021-01-04 22:59:28 +00:00
|
|
|
$new_tags[] = array(
|
2021-01-05 16:14:39 +00:00
|
|
|
'id' => $term['term_id'],
|
2021-01-04 22:59:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Tag exists, assume user wants to set the product with this tag.
|
|
|
|
$new_tags[] = array(
|
|
|
|
'id' => get_term_by( 'name', $tag['name'], 'product_tag' )->term_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$product = $this->save_taxonomy_terms( $product, $new_tags, 'tag' );
|
2019-05-10 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Downloadable.
|
|
|
|
if ( isset( $request['downloadable'] ) ) {
|
|
|
|
$product->set_downloadable( $request['downloadable'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Downloadable options.
|
|
|
|
if ( $product->get_downloadable() ) {
|
|
|
|
|
|
|
|
// Downloadable files.
|
|
|
|
if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) {
|
|
|
|
$product = $this->save_downloadable_files( $product, $request['downloads'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download limit.
|
|
|
|
if ( isset( $request['download_limit'] ) ) {
|
|
|
|
$product->set_download_limit( $request['download_limit'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download expiry.
|
|
|
|
if ( isset( $request['download_expiry'] ) ) {
|
|
|
|
$product->set_download_expiry( $request['download_expiry'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Product url and button text for external products.
|
|
|
|
if ( $product->is_type( 'external' ) ) {
|
|
|
|
if ( isset( $request['external_url'] ) ) {
|
|
|
|
$product->set_product_url( $request['external_url'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $request['button_text'] ) ) {
|
|
|
|
$product->set_button_text( $request['button_text'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save default attributes for variable products.
|
|
|
|
if ( $product->is_type( 'variable' ) ) {
|
|
|
|
$product = $this->save_default_attributes( $product, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set children for a grouped product.
|
|
|
|
if ( $product->is_type( 'grouped' ) && isset( $request['grouped_products'] ) ) {
|
|
|
|
$product->set_children( $request['grouped_products'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for featured/gallery images, upload it and set it.
|
|
|
|
if ( isset( $request['images'] ) ) {
|
|
|
|
$product = $this->set_product_images( $product, $request['images'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow set meta_data.
|
|
|
|
if ( is_array( $request['meta_data'] ) ) {
|
|
|
|
foreach ( $request['meta_data'] as $meta ) {
|
|
|
|
$product->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $request['date_created'] ) ) {
|
|
|
|
$date = rest_parse_date( $request['date_created'] );
|
|
|
|
|
|
|
|
if ( $date ) {
|
|
|
|
$product->set_date_created( $date );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $request['date_created_gmt'] ) ) {
|
|
|
|
$date = rest_parse_date( $request['date_created_gmt'], true );
|
|
|
|
|
|
|
|
if ( $date ) {
|
|
|
|
$product->set_date_created( $date );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters an object before it is inserted via the REST API.
|
|
|
|
*
|
|
|
|
* The dynamic portion of the hook name, `$this->post_type`,
|
|
|
|
* refers to the object type slug.
|
|
|
|
*
|
|
|
|
* @param WC_Data $product Object object.
|
|
|
|
* @param WP_REST_Request $request Request object.
|
|
|
|
* @param bool $creating If is creating a new object.
|
|
|
|
*/
|
|
|
|
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $product, $request, $creating );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Product's schema, conforming to JSON Schema.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_item_schema() {
|
|
|
|
$weight_unit = get_option( 'woocommerce_weight_unit' );
|
|
|
|
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
|
|
|
|
$schema = array(
|
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
|
|
|
'title' => $this->post_type,
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'slug' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product slug.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'permalink' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product URL.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'uri',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_created' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "The date the product was created, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_created_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'The date the product was created, as GMT.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_modified' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "The date the product was last modified, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_modified_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'The date the product was last modified, as GMT.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'type' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product type.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'simple',
|
|
|
|
'enum' => array_keys( wc_get_product_types() ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'status' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product status (post status).', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'publish',
|
|
|
|
'enum' => array_merge( array_keys( get_post_statuses() ), array( 'future' ) ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'featured' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Featured product.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'catalog_visibility' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Catalog visibility.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'visible',
|
|
|
|
'enum' => array( 'visible', 'catalog', 'search', 'hidden' ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'description' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product description.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'short_description' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product short description.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'sku' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Unique identifier.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'price' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Current product price.', 'woocommerce' ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'regular_price' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product regular price.', 'woocommerce' ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'sale_price' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product sale price.', 'woocommerce' ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_on_sale_from' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "Start date of sale price, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_on_sale_from_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Start date of sale price, as GMT.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_on_sale_to' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_on_sale_to_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'price_html' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Price formatted in HTML.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'on_sale' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows if the product is on sale.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'purchasable' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows if the product can be bought.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'total_sales' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Amount of sales.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'virtual' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'If the product is virtual.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'downloadable' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'If the product is downloadable.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'downloads' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of downloadable files.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'File ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'File name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'file' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'File URL.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'download_limit' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Number of times downloadable files can be downloaded after purchase.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => -1,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'download_expiry' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Number of days until access to downloadable files expires.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => -1,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'external_url' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product external URL. Only for external products.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'uri',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'button_text' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product external button text. Only for external products.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'tax_status' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Tax status.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'taxable',
|
|
|
|
'enum' => array( 'taxable', 'shipping', 'none' ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'tax_class' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Tax class.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'manage_stock' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Stock management at product level.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'stock_quantity' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Stock quantity.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'stock_status' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Controls the stock status of the product.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'instock',
|
|
|
|
'enum' => array_keys( wc_get_product_stock_status_options() ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'backorders' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'no',
|
|
|
|
'enum' => array( 'no', 'notify', 'yes' ),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'backorders_allowed' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'backordered' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows if the product is on backordered.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2021-03-11 15:21:43 +00:00
|
|
|
'low_stock_amount' => array(
|
|
|
|
'description' => __( 'Low Stock amount for the product.', 'woocommerce' ),
|
2021-03-15 11:37:39 +00:00
|
|
|
'type' => array( 'integer', 'null' ),
|
2021-03-11 15:21:43 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
2019-05-10 16:56:07 +00:00
|
|
|
'sold_individually' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Allow one item to be bought in a single order.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'weight' => array(
|
|
|
|
/* translators: %s: weight unit */
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => sprintf( __( 'Product weight (%s).', 'woocommerce' ), $weight_unit ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'dimensions' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product dimensions.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'object',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'properties' => array(
|
|
|
|
'length' => array(
|
|
|
|
/* translators: %s: dimension unit */
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => sprintf( __( 'Product length (%s).', 'woocommerce' ), $dimension_unit ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'width' => array(
|
|
|
|
/* translators: %s: dimension unit */
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => sprintf( __( 'Product width (%s).', 'woocommerce' ), $dimension_unit ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'height' => array(
|
|
|
|
/* translators: %s: dimension unit */
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => sprintf( __( 'Product height (%s).', 'woocommerce' ), $dimension_unit ),
|
2020-01-23 13:58:33 +00:00
|
|
|
'type' => 'string',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'shipping_required' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows if the product need to be shipped.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'shipping_taxable' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shows whether or not the product shipping is taxable.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'shipping_class' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shipping class slug.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'shipping_class_id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Shipping class ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'reviews_allowed' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Allow reviews.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'average_rating' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Reviews average rating.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'rating_count' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Amount of reviews that the product have.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'related_ids' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of related products IDs.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'upsell_ids' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of up-sell products IDs.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'cross_sell_ids' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of cross-sell products IDs.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'parent_id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Product parent ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'purchase_note' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Optional note to send the customer after purchase.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'categories' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of categories.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Category ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Category name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'slug' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Category slug.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'tags' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of tags.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Tag ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Tag name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'slug' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Tag slug.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'images' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of images.', 'woocommerce' ),
|
2019-12-12 01:26:18 +00:00
|
|
|
'type' => 'array',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Image ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'date_created' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_created_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_modified' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_modified_gmt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'src' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Image URL.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'uri',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Image name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'alt' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Image alternative text.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'attributes' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of attributes.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Attribute ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Attribute name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'position' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Attribute position.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'visible' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( "Define if the attribute is visible on the \"Additional information\" tab in the product's page.", 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'variation' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Define if the attribute can be used as variation.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'options' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of available term names of the attribute.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'default_attributes' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Defaults variation attributes.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Attribute ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'name' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Attribute name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'option' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Selected attribute term name.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'variations' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of variations IDs.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'grouped_products' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'List of grouped products ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'menu_order' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'meta_data' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Meta data.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Meta ID.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'key' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Meta key.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'value' => array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Meta value.', 'woocommerce' ),
|
2020-08-12 14:01:40 +00:00
|
|
|
'type' => 'mixed',
|
2019-05-10 16:56:07 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new options for 'orderby' to the collection params.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_params() {
|
|
|
|
$params = parent::get_collection_params();
|
|
|
|
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'price', 'popularity', 'rating' ) );
|
|
|
|
|
|
|
|
unset( $params['in_stock'] );
|
|
|
|
$params['stock_status'] = array(
|
2020-08-06 12:48:18 +00:00
|
|
|
'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ),
|
2019-05-10 16:56:07 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'enum' => array_keys( wc_get_product_stock_status_options() ),
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get product data.
|
|
|
|
*
|
|
|
|
* @param WC_Product $product Product instance.
|
2020-12-28 08:40:22 +00:00
|
|
|
* @param string $context Request context. Options: 'view' and 'edit'.
|
|
|
|
*
|
2019-05-10 16:56:07 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-06-03 12:19:41 +00:00
|
|
|
protected function get_product_data( $product, $context = 'view' ) {
|
2020-12-28 08:40:22 +00:00
|
|
|
$data = parent::get_product_data( ...func_get_args() );
|
2021-01-06 12:25:56 +00:00
|
|
|
// Add stock_status if needed.
|
|
|
|
if ( isset( $this->request ) ) {
|
|
|
|
$fields = $this->get_fields_for_response( $this->request );
|
|
|
|
if ( in_array( 'stock_status', $fields ) ) {
|
|
|
|
$data['stock_status'] = $product->get_stock_status( $context );
|
|
|
|
}
|
2020-06-04 20:35:12 +00:00
|
|
|
}
|
|
|
|
return $data;
|
2019-05-10 16:56:07 +00:00
|
|
|
}
|
|
|
|
}
|