WC_Product_CSV_Importer::can_create_variation │ protected │ WC 11.1.0

Whether a variation row that does not exist yet can be created under its parent product.

Method of the class: WC_Product_CSV_Importer{}

No Hooks.

Returns

true|WP_Error. True when the variation can be created, a WP_Error describing the refusal otherwise.

Usage

// protected - for code of main (parent) or child class
$result = $this->can_create_variation( $parsed_data );
$parsed_data(array) (required)
Parsed row data.

Changelog

Since 11.1.0 Introduced.

WC_Product_CSV_Importer::can_create_variation() code WC 11.1.2

protected function can_create_variation( $parsed_data ) {
	// A row ID cannot be honored when creating a new variation: reusing an existing
	// post's ID would corrupt that post, and a nonexistent ID cannot be assigned.
	if ( ! empty( $parsed_data['id'] ) ) {
		return new WP_Error(
			'woocommerce_product_importer_variation_has_id',
			esc_html__( 'A new variation cannot be created for a row that specifies an ID.', 'woocommerce' )
		);
	}

	// A CSV ID cannot be assigned to a new variation, so without a SKU the created variation
	// could never be matched again and every re-import would duplicate it.
	if ( empty( $parsed_data['sku'] ) ) {
		return new WP_Error(
			'woocommerce_product_importer_variation_missing_sku',
			esc_html__( 'A new variation cannot be created without a SKU.', 'woocommerce' )
		);
	}

	if ( empty( $parsed_data['parent_id'] ) ) {
		return new WP_Error(
			'woocommerce_product_importer_variation_missing_parent',
			esc_html__( 'A new variation cannot be created without a parent product.', 'woocommerce' )
		);
	}

	$parent = wc_get_product( $parsed_data['parent_id'] );

	if ( ! $parent || ! $parent->is_type( ProductType::VARIABLE ) ) {
		return new WP_Error(
			'woocommerce_product_importer_variation_parent_not_variable',
			esc_html__( 'A new variation can only be created for a variable parent product.', 'woocommerce' )
		);
	}

	// A parent with the 'importing' status is a placeholder, meaning the parent does not exist either.
	if ( in_array( $parent->get_status(), array( 'importing', ProductStatus::TRASH ), true ) ) {
		return new WP_Error(
			'woocommerce_product_importer_variation_parent_missing',
			esc_html__( 'A new variation cannot be created for a parent product that does not exist.', 'woocommerce' )
		);
	}

	return $this->validate_new_variation_attributes( $parsed_data, $parent );
}