I'm attempting to add repeater data from Advanced Custom Fields Pro onto a Woocommerce product page. I have 3 custom fields, all repeaters. All 3 fields have subfields, such as a Title, and a Value.
For example, Technical Specifics (technical_specifics) has 2 subfields, Titles (titles) and Values (values). This data is being displayed as a table.
SDS and TDS both have File Name (file_name) and PDF link (pdf_file). This data is being displayed as a file name and a link to a PDF on the server.
I have the below code, which adds the tabs correctly, but no data is coming into the page, so the tabs have blank data. What am I doing wrong to retrieve the array data?
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
/*
* Add 3 custom product data tabs
*/
function woo_new_product_tab( $tabs ) {
// Adds the new tab
if(get_field('technical_specifics'))
$tabs['technical_specifics'] = array(
'title' => __( 'Technical Specifics', 'woocommerce' ),
'priority' => 15,
'callback' => 'technical_specifics_callback'
);
if(get_field('sds'))
$tabs['sds'] = array(
'title' => __( 'SDS', 'woocommerce' ),
'priority' => 20,
'callback' => 'sds_callback'
);
if(get_field('tds'))
$tabs['tds'] = array(
'title' => __( 'TDS', 'woocommerce' ),
'priority' => 25,
'callback' => 'tds_callback'
);
return $tabs;
}
function technical_specifics_callback() {
// The new tab content
$per_unit_or_square = get_field( 'pricem2_or_priceunit_', $item->get_product_id() );
$technical_specifics = get_field('technical_specifics');
$technical_spec = $technical_specifics[0];
?>
<div>
<table style="width: 100%;">
<tr>
<?php
foreach ($technical_spec['titles'] as $key => $values)
{
?>
<th><?php echo $values['title1']; ?></th>
<th><?php echo $values['title2']; ?></th>
<th><?php echo $values['title3']; ?></th>
<th><?php echo $values['title4']; ?></th>
<th><?php echo $values['title5']; ?></th>
<th><?php echo $values['title6']; ?></th>
<?php
}
?>
</tr>
<?php
foreach ($technical_spec['values'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['value1']; ?></td>
<td><?php echo $value['value2']; ?></td>
<td><?php echo $value['value3']; ?></td>
<td><?php echo $value['value4']; ?></td>
<td><?php echo $value['value5']; ?></td>
<td><?php echo $value['value6']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
<?php
}
function sds_callback(){
// The new tab content
$sds = get_field('sds');
if(isset($sds[0]))
{
?>
<div class="sds-spec">
<ul class="list">
<?php
foreach ($sds as $key => $value)
{
?>
<li>
Download <?php echo $value['file_name']; ?>
</li>
<?php
}
?>
</ul>
</div>
<?php
}
}
function tds_callback(){
// The new tab content
$tds = get_field('tds');
if(isset($tds[0]))
{
?>
<div class="sds-spec">
<ul class="list">
<?php
foreach ($tds as $key => $value)
{
?>
<li>
Download <?php echo $value['file_name']; ?>
</li>
<?php
}
?>
</ul>
</div>
<?php
}
}
This is the PHP code generated by my ACF fields.
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5e326b8892b0f',
'title' => 'Product Technical Specifics',
'fields' => array(
array(
'key' => 'field_5e326be12fa37',
'label' => 'Technical Specifics',
'name' => 'technical_specifics',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 1,
'max' => 1,
'layout' => 'block',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5e326c132fa38',
'label' => 'Titles',
'name' => 'titles',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 1,
'max' => 1,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5e4a7d6ed08c1',
'label' => 'Title1',
'name' => 'title1',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a81283bc92',
'label' => 'Title2',
'name' => 'title2',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a81323bc93',
'label' => 'Title3',
'name' => 'title3',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a813c3bc94',
'label' => 'Title4',
'name' => 'title4',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a81463bc95',
'label' => 'Title5',
'name' => 'title5',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a81533bc96',
'label' => 'Title6',
'name' => 'title6',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
),
array(
'key' => 'field_5e326c642fa39',
'label' => 'Values',
'name' => 'values',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 1,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5e4a80478bb97',
'label' => 'Value1',
'name' => 'value1',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a80598bb98',
'label' => 'Value2',
'name' => 'value2',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a80658bb99',
'label' => 'Value3',
'name' => 'value3',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a80718bb9a',
'label' => 'Value4',
'name' => 'value4',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a807c8bb9b',
'label' => 'Value5',
'name' => 'value5',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5e4a808b8bb9c',
'label' => 'Value6',
'name' => 'value6',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
),
),
),
array(
'key' => 'field_5f2a9e5a6ec81',
'label' => 'SDS',
'name' => 'sds',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5f2a9e726ec82',
'label' => 'File Name',
'name' => 'file_name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5f2a9e776ec83',
'label' => 'Pdf File',
'name' => 'pdf_file',
'type' => 'file',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'url',
'library' => 'all',
'min_size' => '',
'max_size' => '',
'mime_types' => '',
),
),
),
array(
'key' => 'field_5f2a9e906ec84',
'label' => 'TDS',
'name' => 'tds',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5f2a9e906ec85',
'label' => 'File Name',
'name' => 'file_name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5f2a9e906ec86',
'label' => 'Pdf File',
'name' => 'pdf_file',
'type' => 'file',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'url',
'library' => 'all',
'min_size' => '',
'max_size' => '',
'mime_types' => '',
),
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'product',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => false,
));
endif;
Based on how you describe your repeater fields & the code you've given, it sounds like you might be accessing them incorrectly.
I'll put what I believe the code needs to be to access the values correctly below, however, for debugging sake, in the "technical_specifics_callback" function, could you try var_dump the following variables (per_unit_or_square, technical_specifics & technical_spec) and see if they return values?
From my understanding, I suspect that you either need to add the post id to the get_field('technical_specifics') call. Or the foreach call needs to be updated to work with how repeaters work (code example below).
<?php
<?php
function technical_specifics_callback() {
$per_unit_or_square = get_field( 'pricem2_or_priceunit_', $item->get_product_id() );
$technical_specifics = get_field('technical_specifics');
$title_row = "";
$values_row = "";
foreach( $technical_specifics as $item ) {
$title_row .= "<th>" . $item["title"] . "</th>";
$values_row .= "<td>" . $item["values"] . "</td>";
}
?>
<div>
<table style="width: 100%;">
<tr><?php echo $title_row;?></tr>
<tr><?php echo $values_row;?></tr>
</table>
</div>
<?php
}
After I included ACF into my Plugin fields of type 'user' and 'post_object' don't work any more. They worked perfectly fine before I included ACF. They are displayed but they don't show me any options any more. Any clue what this might have caused? I guess that some link might have changed but I can't find any hint to that in the code:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5f65057cd8301',
'title' => 'Employees Details',
'fields' => array(
array(
'key' => 'field_5f8a0c6792e45',
'label' => 'Display Name',
'name' => 'display_name',
'type' => 'user',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'role' => array(
0 => 'employee',
1 => 'administrator',
),
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'array',
),
array(
'key' => 'field_5f65089dbbe1d',
'label' => 'Occupation',
'name' => 'occupation',
'type' => 'post_object',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'occupations',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'object',
'ui' => 1,
),
array(
'key' => 'field_5f8b42ca6c4c6',
'label' => 'Costs per Hour',
'name' => 'costs_per_hour',
'type' => 'number',
'instructions' => 'Enter a number with max 2 decimals, separated by ".", e.g. "35.46"',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => '',
'max' => '',
'step' => '0.01',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'employees',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
In the meantime I found this:
In the console there are errors as these:
Failed to load resource: the server responded .../advanced-custom-fields/assets/css/acf-global.css?ver=5.9.1
ReferenceError:Can't find variable: acf Global Code post-new.php 1972
(actually my post-new.php doesn't even have so many lines)
When I click on the last one the debugger gives me this:
acf.data = {"postboxes":[{...
acf.doAction( 'prepare' )
And when I finally look into the apache_error.log there are errors like:
File does not exist: /Applications/MAMP/htdocs/myPlugin/Applications, referrer: http://localhost:8888/myPlugin/wp-admin/post-new.php?post_type=jobs
I want to separate some part of my arrays so I can make the details dynamic and different in my WordPress website.
I'm using ACF to get my custom post type taxonomy and trying to generate some attributes and make them tabs using the acf_add_local_field_group function but unfortunately am not able to make it work.
The reason why I want to separate the array is because am not able to call global $post; and I have a function which is a meta box for that to retrieve the taxonomy terms.
Here is my code:
function my_acf_add_local_field_groups()
{
acf_add_local_field_group(array(
'key' => 'tab_group_1',
'title' => 'Product Sizes and Prices',
'name' => 'group_sizes_tab',
'fields' => array(
array(
'key' => 'field_tab_size_1',
'label' => 'First Size',
'name' => 'store_sizes_',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
) ,
array(
'key' => 'field_unique_key',
'label' => 'Simple Repeater',
'name' => 'simple_repeater',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => 0,
'max' => 10,
'layout' => 'table',
'button_label' => 'Add row',
'sub_fields' => array(
array(
'key' => 'field_unique_key_1',
'label' => 'Total Products',
'name' => 'total_products',
'type' => 'text',
) ,
array(
'key' => 'field_unique_key_2',
'label' => 'Total Prices',
'name' => 'total_prices',
'type' => 'text',
) ,
) ,
) ,
array(
'key' => 'field_tab_size_2',
'label' => 'Second Size',
'name' => 'store_sizes_2',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
) ,
) ,
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'products',
) ,
) ,
) ,
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
As you can see in the group_sizes_tab the fields value is the one I want to separate. Am thinking storing it in a variable but when I tested it it doesn't work.
Just to make it short, basically outside the function it'll be like this:
$fields = array(
'key' => 'field_tab_size_1',
'label' => 'First Size',
'name' => 'store_sizes_',
'type' => 'tab',
'parent' => 'tab_group_1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
) ,
'collapsed' => '',
'min' => '',
'max' => '',
);
Then I replace this value inside the function but it is not working.
How can I wrap the array and replace it with a variable in the function?
I'm using a wordpress real estate plugin for my project which is wpcasa, I already do some modification, But this thing takes my time to figure out.
I think they use array to get set values/labels on search form.
Anyone can Help me to how to add default value into a search form?
Here's the code below
$defaults = array(
'keyword' => array(
'label' => __( 'Keyword or Listing ID', 'wpcasa' ) . '…',
'type' => 'text',
'class' => 'width-3-4',
'priority' => 10
),
'submit' => array(
'label' => __( 'Search', 'wpcasa' ),
'type' => 'submit',
'class' => 'width-1-4',
'priority' => 20
),
'offer' => array(
'label' => __( 'Offer', 'wpcasa' ),
'key' => '_price_offer',
'data' => wpsight_offers(),
'type' => 'select',
'data_compare' => '=',
'class' => 'width-1-5',
'priority' => 30
),
'location' => array(
'data' => array(
// wp_dropdown_categories() options
'taxonomy' => 'location',
'show_option_none' => __( 'Location', 'wpcasa' ),
'option_none_value' => '',
'hierarchical' => 1,
'orderby' => 'ID',
'order' => 'ASC'
),
'type' => 'taxonomy_select',
'class' => 'width-1-5',
'priority' => 40
),
'listing-type' => array(
'data' => array(
// wp_dropdown_categories() options
'taxonomy' => 'listing-type',
'show_option_none' => __( 'Type', 'wpcasa' ),
'option_none_value' => '',
'hierarchical' => 1,
'orderby' => 'ID',
'order' => 'ASC'
),
'type' => 'taxonomy_select',
'class' => 'width-1-5',
'priority' => 50
),
$details['details_1']['id'] => array(
'label' => $details['details_1']['label'],
'key' => '_details_1',
'data' => $details['details_1']['data'],
'type' => 'select',
'data_compare' => '>=',
'class' => 'width-1-5',
'priority' => 60
),
$details['details_2']['id'] => array(
'label' => $details['details_2']['label'],
'key' => '_details_2',
'data' => $details['details_2']['data'],
'type' => 'select',
'data_compare' => '>=',
'class' => 'width-1-5',
'priority' => 70
)
);
`
Example on keyword I will set a value HOME Instead showing its label.
Thank you!
I got it now I should add 'default' => 'Myvalue',
example
'keyword' => array(
'label' => __( 'Keyword or Listing ID', 'wpcasa' ) . '…',
'type' => 'text',
'class' => 'width-3-4',
'default' => 'HELLO',
'priority' => 10
),
Thanks for the help/advice!
I am using wordpress for a News site.
There is place called Headlines with following ACF code. Which adds posts to headline of the website.
array (
'key' => 'field_53e3e2fc67dc4',
'label' => 'Headlines',
'name' => 'hp_headlines',
'prefix' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'layout' => 'row',
'button_label' => 'Add Headline',
'sub_fields' => array (
array (
'key' => 'field_54621f720bfdc',
'label' => 'Headline Type',
'name' => 'hp_headline_type',
'prefix' => '',
'type' => 'radio',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
'url' => 'URL',
'article' => 'Article',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => 'url',
'layout' => 'horizontal',
),
array (
'key' => 'field_54621fa20bfdd',
'label' => 'URL',
'name' => 'hp_headline_url',
'prefix' => '',
'type' => 'url',
'instructions' => '',
'required' => 1,
'conditional_logic' => array (
array (
array (
'field' => 'field_54621f720bfdc',
'operator' => '==',
'value' => 'url',
),
),
),
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => 'http://',
),
array (
'key' => 'field_53e3e34067dc5',
'label' => 'Article',
'name' => 'hp_headline_article',
'prefix' => '',
'type' => 'post_object',
'instructions' => '',
'required' => 1,
'conditional_logic' => array (
array (
array (
'field' => 'field_54621f720bfdc',
'operator' => '==',
'value' => 'article',
),
),
),
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array (
0 => 'post',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'id',
'ui' => 1,
),
),
Which creates a field to add already added posts to a list of headlines.
Now I want these fields to be added to category "xyz" automatically as I press the update button. And I have no Idea which file to edit.
You need use save_post action, add this code in your functions.php change cat ID wit your cat id's
function set_my_categories($post_ID){
if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
return $post_ID;
}
wp_set_post_categories( $post_ID, array(49,13) );
}
add_action('save_post', 'set_my_categories');