WC_REST_Product_Variations_V2_Controller::prepare_object_for_database()protectedWC 1.0

Prepare a single variation for create or update.

Method of the class: WC_REST_Product_Variations_V2_Controller{}

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_V2_Controller::prepare_object_for_database() code WC 8.7.0

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();
	}

	// Update parent ID just once.
	if ( 0 === $variation->get_parent_id() ) {
		$variation->set_parent_id( absint( $request['product_id'] ) );
	}

	// Status.
	if ( isset( $request['visible'] ) ) {
		$variation->set_status( false === $request['visible'] ? 'private' : 'publish' );
	}

	// SKU.
	if ( isset( $request['sku'] ) ) {
		$variation->set_sku( wc_clean( $request['sku'] ) );
	}

	// Thumbnail.
	if ( isset( $request['image'] ) ) {
		if ( is_array( $request['image'] ) && ! empty( $request['image'] ) ) {
			$image = $request['image'];
			if ( is_array( $image ) ) {
				$image['position'] = 0;
			}

			$variation = $this->set_product_images( $variation, array( $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'] ) ) {
		if ( 'parent' === $request['manage_stock'] ) {
			$variation->set_manage_stock( false ); // This just indicates the variation does not manage stock, but the parent does.
		} else {
			$variation->set_manage_stock( wc_string_to_bool( $request['manage_stock'] ) );
		}
	}

	if ( isset( $request['in_stock'] ) ) {
		$variation->set_stock_status( true === $request['in_stock'] ? 'instock' : 'outofstock' );
	}

	if ( isset( $request['backorders'] ) ) {
		$variation->set_backorders( $request['backorders'] );
	}

	if ( $variation->get_manage_stock() ) {
		if ( isset( $request['stock_quantity'] ) ) {
			$variation->set_stock_quantity( $request['stock_quantity'] );
		} elseif ( isset( $request['inventory_delta'] ) ) {
			$stock_quantity  = wc_stock_amount( $variation->get_stock_quantity() );
			$stock_quantity += wc_stock_amount( $request['inventory_delta'] );
			$variation->set_stock_quantity( $stock_quantity );
		}
	} else {
		$variation->set_backorders( 'no' );
		$variation->set_stock_quantity( '' );
	}

	// Regular Price.
	if ( isset( $request['regular_price'] ) ) {
		$variation->set_regular_price( $request['regular_price'] );
	}

	// Sale Price.
	if ( isset( $request['sale_price'] ) ) {
		$variation->set_sale_price( $request['sale_price'] );
	}

	if ( isset( $request['date_on_sale_from'] ) ) {
		$variation->set_date_on_sale_from( $request['date_on_sale_from'] );
	}

	if ( isset( $request['date_on_sale_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() );
		$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'] );
				$raw_attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
			} elseif ( ! empty( $attribute['name'] ) ) {
				$raw_attribute_name = sanitize_title( $attribute['name'] );
			}

			if ( ! $attribute_id && ! $raw_attribute_name ) {
				continue;
			}

			$attribute_name = sanitize_title( $raw_attribute_name );

			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, $raw_attribute_name ); // @codingStandardsIgnoreLine

				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'] : '' );
		}
	}

	/**
	 * Filters an object before it is inserted via the REST API.
	 *
	 * The dynamic portion of the hook name, `$this->post_type`,
	 * refers to the object type slug.
	 *
	 * @param WC_Data         $variation Object object.
	 * @param WP_REST_Request $request   Request object.
	 * @param bool            $creating  If is creating a new object.
	 */
	return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $variation, $request, $creating );
}