evel.', 'woocommerce' ), 'type' => 'boolean', 'default' => false, 'context' => array( 'view', 'edit' ), ), 'stock_quantity' => array( 'description' => __( 'Stock quantity.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), ), 'stock_status' => array( 'description' => __( 'Controls the stock status of the product.', 'woocommerce' ), 'type' => 'string', 'default' => 'instock', 'enum' => array_keys( wc_get_product_stock_status_options() ), 'context' => array( 'view', 'edit' ), ), 'backorders' => array( 'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ), 'type' => 'string', 'default' => 'no', 'enum' => array( 'no', 'notify', 'yes' ), 'context' => array( 'view', 'edit' ), ), 'backorders_allowed' => array( 'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'backordered' => array( 'description' => __( 'Shows if the variation is on backordered.', 'woocommerce' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'low_stock_amount' => array( 'description' => __( 'Low Stock amount for the variation.', 'woocommerce' ), 'type' => array( 'integer', 'null' ), 'context' => array( 'view', 'edit' ), ), 'weight' => array( /* translators: %s: weight unit */ 'description' => sprintf( __( 'Variation weight (%s).', 'woocommerce' ), $weight_unit_label ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'dimensions' => array( 'description' => __( 'Variation dimensions.', 'woocommerce' ), 'type' => 'object', 'context' => array( 'view', 'edit' ), 'properties' => array( 'length' => array( /* translators: %s: dimension unit */ 'description' => sprintf( __( 'Variation length (%s).', 'woocommerce' ), $dimension_unit_label ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'width' => array( /* translators: %s: dimension unit */ 'description' => sprintf( __( 'Variation width (%s).', 'woocommerce' ), $dimension_unit_label ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'height' => array( /* translators: %s: dimension unit */ 'description' => sprintf( __( 'Variation height (%s).', 'woocommerce' ), $dimension_unit_label ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), ), ), 'shipping_class' => array( 'description' => __( 'Shipping class slug.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'shipping_class_id' => array( 'description' => __( 'Shipping class ID.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'image' => array( 'description' => __( 'Variation image data.', 'woocommerce' ), 'type' => 'object', 'context' => array( 'view', 'edit' ), 'properties' => array( 'id' => array( 'description' => __( 'Image ID.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), ), 'date_created' => array( 'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ), 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'date_created_gmt' => array( 'description' => __( 'The date the image was created, as GMT.', '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, ), 'date_modified_gmt' => array( 'description' => __( 'The date the image was last modified, as GMT.', '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' ), ), ), ), 'attributes' => array( 'description' => __( 'List of attributes.', 'woocommerce' ), 'type' => 'array', 'context' => array( 'view', 'edit' ), 'items' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'description' => __( 'Attribute ID.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), ), 'name' => array( 'description' => __( 'Attribute name.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'option' => array( 'description' => __( 'Selected attribute term name.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), ), ), ), 'menu_order' => array( 'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), ), 'meta_data' => array( 'description' => __( 'Meta data.', 'woocommerce' ), 'type' => 'array', 'context' => array( 'view', 'edit' ), 'items' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'description' => __( 'Meta ID.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'key' => array( 'description' => __( 'Meta key.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), ), 'value' => array( 'description' => __( 'Meta value.', 'woocommerce' ), 'type' => 'mixed', 'context' => array( 'view', 'edit' ), ), ), ), ), ), ); if ( $this->cogs_is_enabled() ) { $schema = $this->add_cogs_related_product_schema( $schema, true ); } return $this->add_additional_fields_schema( $schema ); } /** * Prepare objects query. * * @since 3.0.0 * @param WP_REST_Request $request Full details about the request. * @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']; /** * @deprecated 8.1.0 replaced by attributes. * Filter by local attributes. */ if ( ! empty( $request['local_attributes'] ) && is_array( $request['local_attributes'] ) ) { wc_deprecated_argument( 'local_attributes', '8.1', 'Use "attributes" instead.' ); foreach ( $request['local_attributes'] as $attribute ) { if ( ! isset( $attribute['attribute'] ) || ! isset( $attribute['term'] ) ) { continue; } $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'value' => $attribute['term'], ) ); } } // Filter by attributes. if ( ! empty( $request['attributes'] ) && is_array( $request['attributes'] ) ) { foreach ( $request['attributes'] as $attribute ) { if ( isset( $attribute['attribute'] ) ) { if ( isset( $attribute['term'] ) ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'value' => $attribute['term'], ) ); } elseif ( ! empty( $attribute['terms'] ) && is_array( $attribute['terms'] ) ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'compare' => 'IN', 'value' => $attribute['terms'], ), ); } } } } // 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. $args, array( '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. $args, array( '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. } // Price filter. if ( is_bool( $request['has_price'] ) ) { if ( $request['has_price'] ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore Standard.Category.SniffName.ErrorCode slow query ok. $args, array( 'relation' => 'AND', array( 'key' => '_price', 'compare' => 'EXISTS', ), array( 'key' => '_price', 'compare' => '!=', 'value' => null, ), ) ); } else { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore Standard.Category.SniffName.ErrorCode slow query ok. $args, array( 'relation' => 'OR', array( 'key' => '_price', 'compare' => 'NOT EXISTS', ), array( 'key' => '_price', 'compare' => '=', 'value' => null, ), ) ); } } // Filter product based on stock_status. if ( ! empty( $request['stock_status'] ) ) { $args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok. $args, array( '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; } $args['post_parent'] = $request['product_id']; return $args; } /** * Get the query params for collections of attachments. * * @return array */ public function get_collection_params() { $params = parent::get_collection_params(); unset( $params['in_stock'], $params['type'], $params['featured'], $params['category'], $params['tag'], $params['shipping_class'], $params['attribute'], $params['attribute_term'] ); $params['stock_status'] = array( 'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), 'type' => 'string', 'enum' => array_keys( wc_get_product_stock_status_options() ), 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ); $params['has_price'] = array( 'description' => __( 'Limit result set to products with or without price.', 'woocommerce' ), 'type' => 'boolean', 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg', ); $params['attributes'] = array( 'description' => __( 'Limit result set to products with specified attributes.', 'woocommerce' ), 'type' => 'array', 'items' => array( 'type' => 'object', 'properties' => array( 'attribute' => array( 'type' => 'string', 'description' => __( 'Attribute slug.', 'woocommerce' ), ), 'term' => array( 'type' => 'string', 'description' => __( 'Attribute term.', 'woocommerce' ), ), 'terms' => array( 'type' => 'array', 'description' => __( 'Attribute terms.', 'woocommerce' ), ), ), ), ); return $params; } /** * Deletes all unmatched variations (aka duplicates). * * @param WC_Product $product Variable product. * @return int Number of deleted variations. */ private function delete_unmatched_product_variations( $product ) { $deleted_count = 0; if ( ! $product ) { return $deleted_count; } $attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' ); // Get existing variations so we don't create duplicates. $existing_variations = array_map( 'wc_get_product', $product->get_children() ); $possible_attribute_combinations = array_reverse( wc_array_cartesian( $attributes ) ); foreach ( $existing_variations as $existing_variation ) { $matching_attribute_key = array_search( $existing_variation->get_attributes(), $possible_attribute_combinations ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict if ( false !== $matching_attribute_key ) { // We only want one possible variation for each possible attribute combination. unset( $possible_attribute_combinations[ $matching_attribute_key ] ); continue; } $existing_variation->delete( true ); $deleted_count ++; } return $deleted_count; } /** * Generate all variations for a given product. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function generate( $request ) { $product_id = (int) $request['product_id']; if ( 'product' !== get_post_type( $product_id ) ) { return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); } wc_maybe_define_constant( 'WC_MAX_LINKED_VARIATIONS', 99 ); wc_set_time_limit( 0 ); $response = array(); $product = wc_get_product( $product_id ); $default_values = isset( $request['default_values'] ) ? $request['default_values'] : array(); $meta_data = isset( $request['meta_data'] ) ? $request['meta_data'] : array(); $data_store = $product->get_data_store(); $response['count'] = $data_store->create_all_product_variations( $product, Constants::get_constant( 'WC_MAX_LINKED_VARIATIONS' ), $default_values, $meta_data ); if ( isset( $request['delete'] ) && $request['delete'] ) { $deleted_count = $this->delete_unmatched_product_variations( $product ); $response['deleted_count'] = $deleted_count; } $data_store->sort_all_product_variations( $product->get_id() ); return rest_ensure_response( $response ); } }