- 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 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.
- The function starts by declaring the global
$product
variable, which represents the current product being displayed. - 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.
- If all these conditions are met, the function proceeds to iterate over the available variations of the product.
- 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-emptydisplay_price
.
- 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. - If a valid variation description is found, the
$content
variable is updated to store the variation description usingapply_filters( 'the_content', $variation_description )
. - Finally, the modified
$content
variable is returned.
- The function uses the WordPress filter hook
'the_content'
to modify the product description. By applying thereplace_product_description_with_variation
function to this filter hook, it replaces the original product description with the variation description if available.
- Install and activate the "Variation Description Plugin" on your WordPress site.
- Ensure you have variable products with child variations set up in your WooCommerce store.
- Customize the variation descriptions for each variation within the product edit screen.
- 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.