Automattic\WooCommerce\Internal\CLI\Migrator\Core

WooCommerceProductImporter::get_or_create_product_object │ private │ WC 1.0

Get or create product object with proper type conversion handling.

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

Хуков нет.

Возвращает

WC_Product|null. Product object or null on failure.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_or_create_product_object( ?int $existing_product_id, $required_type ): ?WC_Product;
?int $existing_product_id(обязательный)
.
$required_type(строка) (обязательный)
Required product type.

Код WooCommerceProductImporter::get_or_create_product_object() WC 11.2.1

private function get_or_create_product_object( ?int $existing_product_id, string $required_type ): ?WC_Product {
	if ( ! $existing_product_id ) {
		return $this->create_product_object( $required_type );
	}

	$existing_product = wc_get_product( $existing_product_id );
	if ( ! $existing_product ) {
		return $this->create_product_object( $required_type );
	}

	$current_type = $existing_product->get_type();
	if ( $current_type === $required_type ) {
		return $existing_product;
	}

	wc_get_logger()->info(
		"Converting product ID {$existing_product_id} from {$current_type} to {$required_type}",
		array( 'source' => 'wc-migrator' )
	);

	switch ( $required_type ) {
		case 'variable':
			return new WC_Product_Variable( $existing_product_id );
		case 'simple':
		default:
			return new WC_Product_Simple( $existing_product_id );
	}
}