Support » Plugin: WooCommerce » Display Custom Fields Variations

  • I am new to WooCommerce and I am currently developing a new site using the plugin to display Courses that can be purchased. I have added some products (courses) and configured them to have variations based on an attribute of Date.

    What I would like WooCommerce to do is use custom field(s) such as Duration and display this information somewhere on the page (preferably in description or additional information tab. I would like the information to change based on the product variation that has been selected (e.g. A course with a date of 01.04.2014 will have a different duration than one that starts on 01.05.2014)

    I have so far managed to successfully add custom fields by putting code in functions.php to display and save the information in the admin section for each variation.

    When attempting to display this information by editing either additional-information.php or variable.php, nothing is being returned/displayed.

    I have tried the following code:
    echo get_post_meta( $post->ID, '_variable_duration', true );
    echo get_post_meta( $product->ID, '_variable_duration', true );

    I know that the information is being stored correctly in the field _variable_duration as I have checked the meta data in the database.

    I believe it is struggling to find the correct product variation id as when I echo $post->id in additional-information.php, I am given the Parent Product ID and not the currently selected variation ID.
    I have tried the following code too but nothing is being returned:
    echo $variation->ID;

    $post_id = $variation->ID;
    echo $post_id;

    I want the information to be displayed dynamically, in the way that the price is displayed dynamically (changing based on the variation selected). This must therefore be possible because you can have variable prices, weights, heights etc. displayed out of the box but how can this be achieved with custom fields?

Viewing 1 replies (of 1 total)
  • Update:

    I have managed to successfully retrieve the variation ID and custom field _variable_duration using the following code:

    $duration = get_post_meta( $variation_id, '_variable_duration', true);

    However, this is only retrieving the first variations data due to using the [0] in the $available_variations array.

    As previously stated, I need this information to be updated dynamically based on the variation date that has been selected.
    I know I could cycle through these with a foreach loop, but I don’t want to display them all, just the one currently selected. Maybe an if statement can check if the current selected variation ID is equal to what is being retrieved in the foreach???

    Any help into figuring this out is very much appreciated.

Viewing 1 replies (of 1 total)
  • The topic ‘Display Custom Fields Variations’ is closed to new replies.