Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yaovicoder/f4e87f795a1d368aa9dd4d8761028797 to your computer and use it in GitHub Desktop.
Save yaovicoder/f4e87f795a1d368aa9dd4d8761028797 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WooCommerce Variable Product Variation Descriptions
* Description: Replaces the product short description with the descriptions of all visible and priced variations.
* Version: 1.0
* Author: Yaovi Ametepe
* Released: July 4th, 2023
*/
/**
* Replaces the product short description with the descriptions of all visible and priced variations.
*
* @param string $short_description The original product short description.
* @return string The modified short description.
*/
function replace_product_short_description_with_variation( $short_description ) {
global $product;
if ( is_product() && $product->is_type( 'variable' ) && $product->has_child() ) {
$variation_descriptions = array();
foreach ( $product->get_available_variations() as $variation ) {
if ( $variation['variation_is_visible'] && $variation['display_price'] !== '' ) {
$variation_description = get_post_field( 'post_content', $variation['variation_id'] );
$variation_descriptions[] = $variation_description;
}
}
if ( ! empty( $variation_descriptions ) ) {
$short_description = implode( '', $variation_descriptions );
}
}
return $short_description;
}
add_filter( 'woocommerce_short_description', 'replace_product_short_description_with_variation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment