WC_Product_Data_Store_CPT::update_version_and_type │ protected │ WC 3.0.0

Make sure we store the product type and version (to track data changes).

Method of the class: WC_Product_Data_Store_CPT{}

Hooks from the method

Returns

void. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->update_version_and_type( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

Changelog

Since 3.0.0 Introduced.
Since 11.2.0 Skips wp_set_object_terms() when the stored product type term already matches to avoid unnecessary term cache invalidation.

WC_Product_Data_Store_CPT::update_version_and_type() code WC 11.2.2

protected function update_version_and_type( &$product ) {
	$product_id        = $product->get_id();
	$old_type          = \WC_Product_Factory::get_product_type( $product_id );
	$new_type          = $product->get_type();
	$stored_type_terms = get_the_terms( $product_id, 'product_type' );
	$stored_type_slug  = ! empty( $stored_type_terms ) && is_array( $stored_type_terms ) ? $stored_type_terms[0]->slug : null;

	// Skip wp_set_object_terms() when the stored term already matches — it always clears the term cache even on no-op writes.
	if ( $stored_type_slug !== $new_type ) {
		wp_set_object_terms( $product_id, $new_type, 'product_type' );
	}

	update_post_meta( $product_id, '_product_version', Constants::get_constant( 'WC_VERSION' ) );

	// Action for the transition.
	if ( $old_type !== $new_type ) {
		$this->updated_props[] = 'product_type';
		do_action( 'woocommerce_product_type_changed', $product, $old_type, $new_type );
	}
}