Support

Account

Home Forums General Issues Displaying Array Data in Product Pages

Solving

Displaying Array Data in Product Pages

  • Hello,
    I am here to ask if anyone could help me to display the data in an array in my product pages.

    I have a field group with 29 custom fields and ACF has applied the relevant data in the products section just fine. I have imported all of the data on my csv (including the custom fields) and the only thing I can’t find out is how to now ensure the custom fields data is displayed on the product webpages. I have tried several functions and stuff I have found by trawling around but I can’t find out out how to do it.

    I am not a developer nor do I know anything about php, but if I found the “formula” I can amend my functions file no problem so the data can be imported to my web pages.

    Can anyone help, or point me in the right direction please? Thanks in advance

    Robert

  • Hi @rslaing

    Thanks for reaching out.

    In order to show the field on the frontend you have to use the_field/get_field functions to show the post’s contents on your site.

    I would recommend that you check out the following article for the same.

    https://www.advancedcustomfields.com/resources/the_field/

  • Thanks James.

    I have over 3000 products, and 29 custom fields. Maybe I have misinterpreted your response but are you saying I have to use the_field/get_field functions and do it by hand for each product?

    So far, I have managed to automate the product import to Woocommerce, each of them showing on my product pages. I would have thought that there must be an easier way to retrieve the custom fields and display them, by some php coding that I insert into my template rather than insert each field by hand?

    Also, having looked at the link you supplied, I don’t have an ID number in the php created by ACF tool, I only have the following, and the only unique reference in the file is ‘key’ => ‘group_5715ebca23858’. Can this be used to pull in the array data to the product pages? If so, how do I do that?

    Here is the php output by ACF php tool that I need to be displayed in the product pages:
    if( function_exists(‘acf_add_local_field_group’) ):

    acf_add_local_field_group(array (
    ‘key’ => ‘group_5715ebca23858’,
    ‘title’ => ‘xxx’,
    ‘fields’ => array (
    array (
    ‘key’ => ‘field_5715ebd45f805’,
    ‘label’ => ‘Model’,
    ‘name’ => ‘model’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ed8d5f806’,
    ‘label’ => ‘Product Size’,
    ‘name’ => ‘product_size’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715eda85f807’,
    ‘label’ => ‘Product Power’,
    ‘name’ => ‘product_power’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),

    array (
    ‘key’ => ‘field_5715ede25f80a’,
    ‘label’ => ‘Liquid Volume’,
    ‘name’ => ‘liquid_volume’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),

    array (
    ‘key’ => ‘field_5715edfe5f80c’,
    ‘label’ => ‘Fastening’,
    ‘name’ => ‘fastening’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee115f80d’,
    ‘label’ => ‘Washable’,
    ‘name’ => ‘washable’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),

    array (
    ‘key’ => ‘field_5715ee225f80f’,
    ‘label’ => ‘Diameter’,
    ‘name’ => ‘diameter’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),

    array (
    ‘key’ => ‘field_5715ee3a5f811’,
    ‘label’ => ‘Original Circumference’,
    ‘name’ => ‘original_circumference’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee4f5f812’,
    ‘label’ => ‘Original Diameter’,
    ‘name’ => ‘original_diameter’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee615f813’,
    ‘label’ => ‘Product Width’,
    ‘name’ => ‘product_width’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee6c5f814’,
    ‘label’ => ‘Colour’,
    ‘name’ => ‘colour’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee7b5f815’,
    ‘label’ => ‘Flexibility’,
    ‘name’ => ‘flexibility’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee865f816’,
    ‘label’ => ‘Controller’,
    ‘name’ => ‘controller’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee8e5f817’,
    ‘label’ => ‘Waterproof’,
    ‘name’ => ‘waterproof’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ee985f818’,
    ‘label’ => ‘Designed For Who’,
    ‘name’ => ‘designed_for_who’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715eead5f819’,
    ‘label’ => ‘What Is It’,
    ‘name’ => ‘what_is_it’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715eec65f81a’,
    ‘label’ => ‘What Is It For’,
    ‘name’ => ‘what_is_it_for’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715eef65f81b’,
    ‘label’ => ‘Product Action’,
    ‘name’ => ‘product_action’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef045f81c’,
    ‘label’ => ‘Features’,
    ‘name’ => ‘features’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef0f5f81d’,
    ‘label’ => ‘Miscellaneous’,
    ‘name’ => ‘miscellaneous’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef195f81e’,
    ‘label’ => ‘Material Name’,
    ‘name’ => ‘material_name’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef2a5f81f’,
    ‘label’ => ‘Brand Name’,
    ‘name’ => ‘brand_name’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef335f820’,
    ‘label’ => ‘Style Name’,
    ‘name’ => ‘style_name’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    array (
    ‘key’ => ‘field_5715ef3d5f821’,
    ‘label’ => ‘EAN’,
    ‘name’ => ‘ean’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array (
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ‘readonly’ => 0,
    ‘disabled’ => 0,
    ),
    ),
    ‘location’ => array (
    array (
    array (
    ‘param’ => ‘post_type’,
    ‘operator’ => ‘==’,
    ‘value’ => ‘product’,
    ),
    ),
    ),
    ‘menu_order’ => 1,
    ‘position’ => ‘normal’,
    ‘style’ => ‘default’,
    ‘label_placement’ => ‘top’,
    ‘instruction_placement’ => ‘label’,
    ‘hide_on_screen’ => ”,
    ‘active’ => 1,
    ‘description’ => ”,
    ));

    endif;

  • Hi @rslaing

    Could you please clarify your intention? Did you want to insert the custom field values to the products you have or did you want to show the values on the front end?

    If you want to insert the value, I’m afraid there’s no easy way to automate it because ACF doesn’t know what you want to insert for a particular product.

    If you want to show the values, please take a look at this page: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/.

    The ID for the_field() function is not the group ID, but your post/product ID. To get it, please take a look at this page: http://stackoverflow.com/questions/27385920/woocommerce-get-current-product-id-or-title-within-sidebar.

    I hope this helps!

  • I imported all products from csv. They are visible in products admin section. The custom fields had been set up before import and the field values were imported too. The problem I had was displaying those labels (keys) and fields (values) on the products webpage, but only those labels and fields that were not empty. I have over 3000 extremely varied products and 29 fields and only a third of those fields were relative to any one product at any time. However, having spent a lot of time trying to find an answer I managed to sort it out using the following and placed the code in my template. Now all I have to do is complete a php course so it doesn’t take two weeks to find 10 lines of code! Thanks for your responses.

    <?php

    $fields = get_field_objects(); ?>

    <?php if( $fields ): ?>

      <?php foreach( $fields as $field ): ?>

      <?php if( $field[‘value’] ): ?>

    • <?php echo $field[‘label’]; ?>: <?php echo $field[‘value’]; ?>
    • <?php endif; ?>

      <?php endforeach; ?>

    <?php endif; ?>

  • Hi @rslaing

    Thanks for the explanation. I see what you wanted now. I’m sorry I couldn’t understand it before.

    Good luck with the project! 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Displaying Array Data in Product Pages’ is closed to new replies.