Support

Account

Home Forums General Issues ACF Group does not work on shop page (Woocommerce)

Solved

ACF Group does not work on shop page (Woocommerce)

  • Hi!

    Currently I am adding a banner to the shop page for Woocommerce. I do that with the use of action hooks:

    //add banner to shop
    	  add_action( 'woocommerce_before_main_content', 'cp_add_banner', 3 );
    	  function cp_add_banner(){
    	    get_template_part('template-parts/banner');
    	  }

    Yet for some reason, the group doesn’t work on the shop page. It simply isn’t loading when using sub fields in a group.

    The banner.php:

    <section class="main-banner container-full">
    
      <img class="logo" src="<?php bloginfo('template_url'); ?>/assets/images/logo.svg" alt="logo">
    
      <?php if( have_rows('main_banner') ):
        while( have_rows('main_banner') ): the_row(); ?>
    
        <img src="<?php the_sub_field('image'); ?>">
    
        <?php endwhile;
      endif; ?>
    
    </section>

    When I remove the if statement of the group, it load the banner.php. But when I start using the group again, it stops working completely.

    Is there a way to solve this, or what is my oversight?.

    Thank you in advance.

  • If you want to get fields from the page set as the “Shop” page in WC, then you need to supply the correct ID for ACF to know where to get the fields from.

    The issue here is that you the page is an archive page and the query is altered when loading the “Shop” page to point to the archive instead of the page.

    Anyway, I discovered this while working on a bug on a site. WC will show the “Content” of the “Shop” page as description for the archive page. Here is the WC function that does this.

    If you’re interested the function that does this is woocommerce_product_archive_description() in the file ...plugins/woocommerce/includes/wc-template-functions.php

    Anyway, to get the ID of the “Shop” page do

    
    $shop_page_id = wc_get_page_id('shop');
    

    Then use that ID for ACF calls

    
    <section class="main-banner container-full">
    
      <img class="logo" src="<?php bloginfo('template_url'); ?>/assets/images/logo.svg" alt="logo">
    
      <?php if( have_rows('main_banner', $shop_page_id) ):
        while( have_rows('main_banner', $shop_page_id) ): the_row(); ?>
    
        <img src="<?php the_sub_field('image'); ?>">
    
        <?php endwhile;
      endif; ?>
    
    </section>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.