skip to Main Content

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


  1. Chosen as BEST ANSWER

    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;
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search