Skip to content

Instantly share code, notes, and snippets.

@yaovicoder
Last active July 4, 2023 14:31
Show Gist options
  • Save yaovicoder/19dd6888e5834f69042c79bbef0901fa to your computer and use it in GitHub Desktop.
Save yaovicoder/19dd6888e5834f69042c79bbef0901fa to your computer and use it in GitHub Desktop.
This WordPress plugin replaces the main product description with the variation description if available for variable products.

Plugin Description:

  • Plugin Name: Variation Description Plugin
  • Description: Replaces main product description with variation description if available.

This plugin modifies the product description display behavior in WooCommerce for variable products. By default, WooCommerce displays the main product description on the product page. However, if a variable product has child variations with different descriptions, this plugin replaces the main product description with the description of the selected price variation.

Function Description:

  • Function Name: replace_product_description_with_variation

  • Function Description: Replaces the product description with the variation description if available for variable products.

  • Function Arguments:

    • $content (string): The current content of the product description.
  • Function Parameters:

    • $content (string): The current content of the product description. This parameter represents the main product description initially.
  • Function Return Value:

    • The modified product description. It will either be the variation description if available or the original product description if no variation description is found.

Function Implementation:

  1. The function starts by declaring the global $product variable, which represents the current product being displayed.
  2. The function then checks the following conditions:
    • is_product(): Checks if the current page being displayed is a single product page.
    • $product->is_type( 'variable' ): Checks if the product being displayed is of type "variable."
    • $product->has_child(): Checks if the variable product has child variations.
  3. If all these conditions are met, the function proceeds to iterate over the available variations of the product.
  4. Inside the loop, it checks the following conditions for each variation:
    • $variation['variation_is_visible']: Checks if the variation is visible to the customer.
    • $variation['display_price'] !== '': Checks if the variation has a non-empty display_price.
  5. If a variation is found that meets these conditions, the variation description is retrieved using get_post_field( 'post_content', $variation['variation_id'] ). This retrieves the description field of the specific variation post.
  6. If a valid variation description is found, the $content variable is updated to store the variation description using apply_filters( 'the_content', $variation_description ).
  7. Finally, the modified $content variable is returned.

Filter Hook Usage:

  • The function uses the WordPress filter hook 'the_content' to modify the product description. By applying the replace_product_description_with_variation function to this filter hook, it replaces the original product description with the variation description if available.

Usage Instructions:

  1. Install and activate the "Variation Description Plugin" on your WordPress site.
  2. Ensure you have variable products with child variations set up in your WooCommerce store.
  3. Customize the variation descriptions for each variation within the product edit screen.
  4. Visit the product page on the front-end of your website, and you should see the variation description displayed instead of the main product description, only if a variation with a visible price and description is selected.

Please note that this documentation assumes basic knowledge of WordPress, WooCommerce, and PHP. It's essential to test the plugin thoroughly and ensure compatibility with your specific setup before deploying it on a live site.

<?php
/*
Plugin Name: Variation Description Plugin
Description: Replaces main product description with variation description if available.
Version: 1.0
Author: Yaovi Ametepe
Released: July 4th, 2023
*/
/**
* Replaces the product description with the variation description if available for variable products.
*
* This function is hooked to the 'the_content' filter and modifies the product description display behavior in WooCommerce
* for variable products. If a variable product has child variations with different descriptions, this function replaces
* the main product description with the description of the selected price variation.
*
* @param string $content The current content of the product description.
* @return string The modified product description, either the variation description if available or the original product
* description if no variation description is found.
*/
function replace_product_description_with_variation( $content ) {
global $product;
if ( is_product() && $product->is_type( 'variable' ) && $product->has_child() ) {
$variation_description = '';
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'] );
break;
}
}
if ( ! empty( $variation_description ) ) {
$content = apply_filters( 'the_content', $variation_description );
}
}
return $content;
}
add_filter( 'the_content', 'replace_product_description_with_variation' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment