I’d like to always display the variation descriptions for each variation on the product page (currently it will only show the description for whatever variation is selected via the dropdown). I can’t seem to access the
In plugins/woocommerce/templates/single-product/add-to-cart/variation.php
, it seems to be accessed via:
{{{ data.variation.variation_description }}}
When inspecting the desired field in the edit product page, its name=
is :
variable_description0
I tried accessing via both of these, as well as:
if($product && taxonomy_exists($attribute)) {
$terms = wc_get_product_terms($product->get_id(), $attribute, array(
'fields' => 'all',
));
foreach($terms as $term) {
$variable_description = get_post_meta( $id, '_variation_description', true );
if(in_array($term->slug, $options, true)) {
$radios .= '<div><input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($term->slug).'" '.checked(sanitize_title($args['selected']), $term->slug, false).'><label for="'.esc_attr($term->slug).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $term->name)).' <span class="description">' . 'DESCRIPTION' . '</span></label></div>';
}
}
} else {
foreach($options as $option) {
// var_dump($options);
$variable_description = get_post_meta( $id, '_variation_description', true );
$checked = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false);
$radios .= '<div><input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($option).'" id="'.sanitize_title($option).'" '.$checked.'><label for="'.sanitize_title($option).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $option)).' <span class="description">' . $variable_description . '</span></label></div>';
}
}
However everything returns as NULL.
What should I be using for $id/how do I print this out for all variables?
2
Answers
I ended up being able to accomplish this by getting all variations of a product, looping through them, finding the match, then setting that description as the variable. This seems a little clunky and could cause issues for larger catalogs, so any improvements are welcome!
I have replaced:
with
Same issue here. I can’t believe there isn’t a simple solution.
Thanks for your code – very useful
One improvement (I hope) is to only do the loop over the variations once and save the values to an array.
so:
Then call the function once, after you have values for $product and $attribute, so:
Then to get the actual description when you’re in the foreach($terms as $term) loop use:
The only thing I”m not sure of is how much error checking I need to do to make sure that
is defined