简体   繁体   中英

Retrieving repeater data from ACF Pro and adding it to Woocommerce Tab

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>
                        <a href="<?php echo $value['pdf_file']; ?>" class="open-popup">Download <?php echo $value['file_name']; ?></a>
                    </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>
                        <a href="<?php echo $value['pdf_file']; ?>" class="open-popup1">Download <?php echo $value['file_name']; ?></a>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </div>
        <?php
    }
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM