Automattic\WooCommerce\Internal\CLI\Migrator\Core
WooCommerceProductImporter::get_or_create_product_object
Get or create product object with proper type conversion handling.
Method of the class: WooCommerceProductImporter{}
No Hooks.
Returns
WC_Product|null. Product object or null on failure.
Usage
// private - for code of main (parent) class only $result = $this->get_or_create_product_object( ?int $existing_product_id, $required_type ): ?WC_Product;
- ?int $existing_product_id(required)
- .
- $required_type(string) (required)
- Required product type.
WooCommerceProductImporter::get_or_create_product_object() WooCommerceProductImporter::get or create product object code WC 10.7.0
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 );
}
}