WC_REST_Product_Variations_Controller::prepare_object_for_database() │ protected │ WC 1.0
Prepare a single variation for create or update.
Method of the class: WC_REST_Product_Variations_Controller{}
Hooks from the method
Return
WP_Error|WC_Data
.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_object_for_database( $request, $creating );
- $request(WP_REST_Request) (required)
- Request object.
- $creating(true|false)
- If is creating a new object.
Default: false
WC_REST_Product_Variations_Controller::prepare_object_for_database() WC REST Product Variations Controller::prepare object for database code WC 9.5.1
protected function prepare_object_for_database( $request, $creating = false ) { if ( isset( $request['id'] ) ) { $variation = wc_get_product( absint( $request['id'] ) ); } else { $variation = new WC_Product_Variation(); } $variation->set_parent_id( absint( $request['product_id'] ) ); // Status. if ( isset( $request['status'] ) ) { $variation->set_status( get_post_status_object( $request['status'] ) ? $request['status'] : 'draft' ); } // SKU. if ( isset( $request['sku'] ) ) { $variation->set_sku( wc_clean( $request['sku'] ) ); } // Unique ID. if ( isset( $request['global_unique_id'] ) ) { $variation->set_global_unique_id( wc_clean( $request['global_unique_id'] ) ); } // Thumbnail. if ( isset( $request['image'] ) ) { if ( is_array( $request['image'] ) ) { $variation = $this->set_variation_image( $variation, $request['image'] ); } else { $variation->set_image_id( '' ); } } // Virtual variation. if ( isset( $request['virtual'] ) ) { $variation->set_virtual( $request['virtual'] ); } // Downloadable variation. if ( isset( $request['downloadable'] ) ) { $variation->set_downloadable( $request['downloadable'] ); } // Downloads. if ( $variation->get_downloadable() ) { // Downloadable files. if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) { $variation = $this->save_downloadable_files( $variation, $request['downloads'] ); } // Download limit. if ( isset( $request['download_limit'] ) ) { $variation->set_download_limit( $request['download_limit'] ); } // Download expiry. if ( isset( $request['download_expiry'] ) ) { $variation->set_download_expiry( $request['download_expiry'] ); } } // Shipping data. $variation = $this->save_product_shipping_data( $variation, $request ); // Stock handling. if ( isset( $request['manage_stock'] ) ) { $variation->set_manage_stock( $request['manage_stock'] ); } if ( isset( $request['stock_status'] ) ) { $variation->set_stock_status( $request['stock_status'] ); } if ( isset( $request['backorders'] ) ) { $variation->set_backorders( $request['backorders'] ); } if ( $variation->get_manage_stock() ) { if ( isset( $request['stock_quantity'] ) ) { $variation->set_stock_quantity( $request['stock_quantity'] ); } elseif ( isset( $request['inventory_delta'] ) ) { $stock_quantity = wc_stock_amount( $variation->get_stock_quantity() ); $stock_quantity += wc_stock_amount( $request['inventory_delta'] ); $variation->set_stock_quantity( $stock_quantity ); } // 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'] ) { $variation->set_low_stock_amount( '' ); } else { $variation->set_low_stock_amount( wc_stock_amount( $request['low_stock_amount'] ) ); } } } else { $variation->set_backorders( 'no' ); $variation->set_stock_quantity( '' ); $variation->set_low_stock_amount( '' ); } // Regular Price. if ( isset( $request['regular_price'] ) ) { $variation->set_regular_price( $request['regular_price'] ); } // Sale Price. if ( isset( $request['sale_price'] ) ) { $variation->set_sale_price( $request['sale_price'] ); } if ( isset( $request['date_on_sale_from'] ) ) { $variation->set_date_on_sale_from( $request['date_on_sale_from'] ); } if ( isset( $request['date_on_sale_from_gmt'] ) ) { $variation->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'] ) ) { $variation->set_date_on_sale_to( $request['date_on_sale_to'] ); } if ( isset( $request['date_on_sale_to_gmt'] ) ) { $variation->set_date_on_sale_to( $request['date_on_sale_to_gmt'] ? strtotime( $request['date_on_sale_to_gmt'] ) : null ); } // Tax class. if ( isset( $request['tax_class'] ) ) { $variation->set_tax_class( $request['tax_class'] ); } // Description. if ( isset( $request['description'] ) ) { $variation->set_description( wp_kses_post( $request['description'] ) ); } // Update taxonomies. if ( isset( $request['attributes'] ) ) { $attributes = array(); $parent = wc_get_product( $variation->get_parent_id() ); if ( ! $parent ) { return new WP_Error( // Translators: %d parent ID. "woocommerce_rest_{$this->post_type}_invalid_parent", __( 'Cannot set attributes due to invalid parent product.', 'woocommerce' ), array( 'status' => 404 ) ); } $parent_attributes = $parent->get_attributes(); foreach ( $request['attributes'] as $attribute ) { $attribute_id = 0; $attribute_name = ''; // Check ID for global attributes or name for product attributes. if ( ! empty( $attribute['id'] ) ) { $attribute_id = absint( $attribute['id'] ); $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); } elseif ( ! empty( $attribute['name'] ) ) { $attribute_name = sanitize_title( $attribute['name'] ); } if ( ! $attribute_id && ! $attribute_name ) { continue; } if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) { continue; } $attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() ); $attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) { // If dealing with a taxonomy, we need to get the slug from the name posted to the API. $term = get_term_by( 'name', $attribute_value, $attribute_name ); if ( $term && ! is_wp_error( $term ) ) { $attribute_value = $term->slug; } else { $attribute_value = sanitize_title( $attribute_value ); } } $attributes[ $attribute_key ] = $attribute_value; } $variation->set_attributes( $attributes ); } // Menu order. if ( $request['menu_order'] ) { $variation->set_menu_order( $request['menu_order'] ); } // Meta data. if ( is_array( $request['meta_data'] ) ) { foreach ( $request['meta_data'] as $meta ) { $variation->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); } } if ( $this->cogs_is_enabled() ) { $this->set_cogs_info_in_product_object( $request, $variation ); } /** * 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. * * @since 4.5.0 * @param WC_Data $variation Object object. * @param WP_REST_Request $request Request object. * @param bool $creating If is creating a new object. */ return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $variation, $request, $creating ); }