WordPress at a glance
function is not described

WC_REST_Legacy_Products_Controller::save_variations_data() protected WC 1.0

Deprecated from version 3.0.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Save variations.

{} It's a method of the class: WC_REST_Legacy_Products_Controller{}

Return

true/false.

Usage

// protected - for code of main (parent) or child class
$result = $this->save_variations_data( $product, $request );
$product(WC_Product) (required)
Product instance.
$request(WP_REST_Request) (required)
Request data.

Changelog

Deprecated 3.0.0

Code of WC_REST_Legacy_Products_Controller::save_variations_data() WC 5.0.0

<?php
protected function save_variations_data( $product, $request ) {
	foreach ( $request['variations'] as $menu_order => $data ) {
		$variation = new WC_Product_Variation( isset( $data['id'] ) ? absint( $data['id'] ) : 0 );

		// Create initial name and status.
		if ( ! $variation->get_slug() ) {
			/* translators: 1: variation id 2: product name */
			$variation->set_name( sprintf( __( 'Variation #%1$s of %2$s', 'woocommerce' ), $variation->get_id(), $product->get_name() ) );
			$variation->set_status( isset( $data['visible'] ) && false === $data['visible'] ? 'private' : 'publish' );
		}

		// Parent ID.
		$variation->set_parent_id( $product->get_id() );

		// Menu order.
		$variation->set_menu_order( $menu_order );

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

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

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

			$variation = $this->set_product_images( $variation, array( $image ) );
		}

		// Virtual variation.
		if ( isset( $data['virtual'] ) ) {
			$variation->set_virtual( $data['virtual'] );
		}

		// Downloadable variation.
		if ( isset( $data['downloadable'] ) ) {
			$variation->set_downloadable( $data['downloadable'] );
		}

		// Downloads.
		if ( $variation->get_downloadable() ) {
			// Downloadable files.
			if ( isset( $data['downloads'] ) && is_array( $data['downloads'] ) ) {
				$variation = $this->save_downloadable_files( $variation, $data['downloads'] );
			}

			// Download limit.
			if ( isset( $data['download_limit'] ) ) {
				$variation->set_download_limit( $data['download_limit'] );
			}

			// Download expiry.
			if ( isset( $data['download_expiry'] ) ) {
				$variation->set_download_expiry( $data['download_expiry'] );
			}
		}

		// Shipping data.
		$variation = $this->save_product_shipping_data( $variation, $data );

		// Stock handling.
		if ( isset( $data['manage_stock'] ) ) {
			$variation->set_manage_stock( $data['manage_stock'] );
		}

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

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

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

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

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

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

		if ( isset( $data['date_on_sale_to'] ) ) {
			$variation->set_date_on_sale_to( $data['date_on_sale_to'] );
		}

		// Tax class.
		if ( isset( $data['tax_class'] ) ) {
			$variation->set_tax_class( $data['tax_class'] );
		}

		// Description.
		if ( isset( $data['description'] ) ) {
			$variation->set_description( wp_kses_post( $data['description'] ) );
		}

		// Update taxonomies.
		if ( isset( $data['attributes'] ) ) {
			$attributes = array();
			$parent_attributes = $product->get_attributes();

			foreach ( $data['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 );
		}

		$variation->save();

		do_action( 'woocommerce_rest_save_product_variation', $variation->get_id(), $menu_order, $data );
	}

	return true;
}