1

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?

user2521295
  • 823
  • 2
  • 12
  • 23

2 Answers2

1

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!

// Get all variations of product
$variations1 = $product->get_children();

// Loop through variations to find description
foreach ($variations1 as $value):

    $single_variation = new WC_Product_Variation($value);

    $variation_full_name = $single_variation->name;
    $variation_name = str_replace($product->name . " - ", "", $variation_full_name);

    // Set the variable description
    if ( $option === $variation_name ):
        $variable_description = $single_variation->description;
    endif;

endforeach;
user2521295
  • 823
  • 2
  • 12
  • 23
1

UPDATE 30/01/2020 It seems we were using deprecated code

I have replaced:

$variation_data[$single_variation->attributes[$attribute]] = $single_variation->description;
}

with

$variation_data[$single_variation->get_attributes()[$attribute]] = $single_variation->get_description();

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:

function mc_return_variation_data($product, $attribute){
    $variation_data = array();
    $variations = $product->get_children();
    foreach ($variations as $value){
        $single_variation = new WC_Product_Variation($value);
        $variation_data[$single_variation->get_attributes()[$attribute]] = $single_variation->get_description();
    }
     return $variation_data;
}

Then call the function once, after you have values for $product and $attribute, so:

$variation_data = mc_return_variation_data($product, $attribute);

Then to get the actual description when you're in the foreach($terms as $term) loop use:

$variable_description = $variation_data[$term->slug];

The only thing I"m not sure of is how much error checking I need to do to make sure that

$variation_data[$term->slug]

is defined

Nat
  • 621
  • 1
  • 6
  • 17
  • 1
    GREAT CALL. That should save a lot of calls. And yes, just in case of any weirdness it might make sense/be safest to do an if/then if $variation_data[$term->slug] isn't defined, to just set an empty string for $variable_description – user2521295 Jan 29 '20 at 19:57
  • Note that my code assumes you only have one variations attribute on your product page. – Nat Jan 30 '20 at 13:00