Use the wpseo_metadesc
filter:
function wpse_406559_product_meta_description_from_category( $description ) {
if ( ! is_singular( 'product' ) || $description ) {
// Bail early if not single product, or custom meta description is already set
return $description;
}
$product_cats = get_the_terms( get_queried_object_id(), 'product_cat' );
// Get first product category
$product_cat = is_array( $product_cats ) ? current( $product_cats ) : null;
if ( $product_cat ) {
// Get Yoast SEO term meta
$meta = get_option( 'wpseo_taxonomy_meta' );
// Try to get Yoast SEO meta description for this product category
$description = $meta['product_cat'][ $product_cat->term_id ]['wpseo_desc'] ?? null;
if ( ! $description ) {
// Fallback to default product category description field
$description = wp_strip_all_tags( $product_cat->description );
}
}
return $description;
}
add_filter( 'wpseo_metadesc', 'wpse_406559_product_meta_description_from_category' );