Automattic\WooCommerce\Internal\CLI\Migrator\Core

WooCommerceProductImporter::update_seo_metaprivateWC 1.0

Updates SEO meta fields if Yoast SEO is active.

Method of the class: WooCommerceProductImporter{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->update_seo_meta( $product_id, $metafields, $product_data ): void;
$product_id(int) (required)
The product ID.
$metafields(array) (required)
Key-value array of metafields from standardized data.
$product_data(array) (required)
Full product data for fallbacks.

WooCommerceProductImporter::update_seo_meta() code WC 10.7.0

private function update_seo_meta( int $product_id, array $metafields, array $product_data ): void {
	if ( ! defined( 'WPSEO_VERSION' ) ) {
		return;
	}

	$seo_title       = $metafields['global_title_tag'] ?? null;
	$seo_description = $metafields['global_description_tag'] ?? null;

	$final_seo_title       = $seo_title ? $seo_title : ( $product_data['name'] ?? '' );
	$fallback_desc         = $product_data['description'] ? $product_data['description'] : ( $product_data['short_description'] ?? '' );
	$final_seo_description = $seo_description ? $seo_description : wp_strip_all_tags( $fallback_desc );

	$current_title = get_post_meta( $product_id, '_yoast_wpseo_title', true );
	if ( $current_title !== $final_seo_title && ! empty( $final_seo_title ) ) {
		update_post_meta( $product_id, '_yoast_wpseo_title', $final_seo_title );
	}

	$current_desc = get_post_meta( $product_id, '_yoast_wpseo_metadesc', true );
	if ( $current_desc !== $final_seo_description && ! empty( $final_seo_description ) ) {
		$truncated_desc = mb_substr( $final_seo_description, 0, 160 );
		update_post_meta( $product_id, '_yoast_wpseo_metadesc', $truncated_desc );
	}
}