WC_Product_Importer::set_variation_data()protectedWC 1.0

Set variation data.

Метод класса: WC_Product_Importer{}

Хуков нет.

Возвращает

WC_Product|WP_Error.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->set_variation_data( $variation, $data );
$variation(WC_Product) (обязательный) (передается по ссылке — &)
Product instance.
$data(массив) (обязательный)
Item data.

Код WC_Product_Importer::set_variation_data() WC 8.7.0

protected function set_variation_data( &$variation, $data ) {
	$parent = false;

	// Check if parent exist.
	if ( isset( $data['parent_id'] ) ) {
		$parent = wc_get_product( $data['parent_id'] );

		if ( $parent ) {
			$variation->set_parent_id( $parent->get_id() );
		}
	}

	// Stop if parent does not exists.
	if ( ! $parent ) {
		return new WP_Error( 'woocommerce_product_importer_missing_variation_parent_id', __( 'Variation cannot be imported: Missing parent ID or parent does not exist yet.', 'woocommerce' ), array( 'status' => 401 ) );
	}

	// Stop if parent is a product variation.
	if ( $parent->is_type( 'variation' ) ) {
		return new WP_Error( 'woocommerce_product_importer_parent_set_as_variation', __( 'Variation cannot be imported: Parent product cannot be a product variation', 'woocommerce' ), array( 'status' => 401 ) );
	}

	if ( isset( $data['raw_attributes'] ) ) {
		$attributes        = array();
		$parent_attributes = $this->get_variation_parent_attributes( $data['raw_attributes'], $parent );

		foreach ( $data['raw_attributes'] as $attribute ) {
			$attribute_id = 0;

			// Get ID if is a global attribute.
			if ( ! empty( $attribute['taxonomy'] ) ) {
				$attribute_id = $this->get_attribute_taxonomy_id( $attribute['name'] );
			}

			if ( $attribute_id ) {
				$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
			} else {
				$attribute_name = sanitize_title( $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['value'] ) ? current( $attribute['value'] ) : '';

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