skip to Main Content

I am customizing an old theme called AmazeTheme which was not specifically made with WooCommerce support. And an woocommerce.php was at theme’s root. However with help of plugins like Simply Show Hooks and Show current template I am able to see where to put which info.

But I am having an weird problem. The ACF fields I have added are not visible at all. I even tried to assign the field group inside product loop section.

I also tried the following method.

add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );

function view_acf_field_for_single_product(){  
  if (function_exists('the_field')){
    the_field('shop_support');
  }

}

And inside the loop of single-product.php

        <?php while ( have_posts() ) : the_post(); ?>

            <?php wc_get_template_part( 'content', 'single-product' ); 
                   $dump = get_fields();
            ?>
        <?php endwhile; // end of the loop. ?>

And then did var_dump($dump) at desired place of the file.

This site’s php version is 5.6.40 and WooCommerce version is 3.4.8
WP version is 4.9.18

I have looked up many solutions. Also tried the WooCommerce Hooks but still no clue why ACF not showing.

2

Answers


  1. Chosen as BEST ANSWER

    For some weird reasons, this approach worked for me (path/to/theme/woocommerce/single-product.php):

    <?php if (get_field_objects()): 
         foreach ( get_field_objects() as $field_id => $field ) : 
          $value = trim($field['value']); 
             if (!empty($value)) : 
    ?>
                <div class="product_field" id="field-<?php echo $field_id; ?>">
                    <?php the_field($field_id); ?>
                </div>
            <?php endif; ?>
        <?php endforeach; // end of the loop. ?>
    <?php endif; ?>
    

    Also the following one for Category (path/to/theme/woocommerce/archive-product.php) pages:

    <?php
    $extra_info = get_field_object( 'category_details', get_queried_object() );
    if ( !empty($extra_info) ) :
    ?>
        <div class="category_field" id="field-<?php echo $extra_info['key']; ?>">
            <?php echo $extra_info['value']; ?>
        </div>
    
    <?php endif; ?>
    

    Even at these locations, get_fields() did not work.


  2. Can you try this code:

    add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
    
    function view_acf_field_for_single_product(){  
      global $post;
      if (function_exists('the_field')){
        the_field('shop_support', $post->ID);
      }
    
    }
    

    Not tested.

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