Support

Account

Home Forums General Issues Reverse relationship array on WooCommerce product page.

Solved

Reverse relationship array on WooCommerce product page.

  • Hi everyone,

    I’m having a few issues and I’ll try and explain in detail here.

    I have a relationship setup in ACF as shown here.
    https://drive.google.com/file/d/0ByronOU4qUW7N253VEttb2U2azA/view?usp=sharing

    and have its location set to WooCommerce post type products, as shown here.
    https://drive.google.com/file/d/0ByronOU4qUW7Y2RyNTdiWE1hOVU/view?usp=sharing

    In a product page in WooCommerce admin I select a number of pages that are related to that product. Now I’m trying to use WooCommerce hooks to bring in the images, permalink, title and thumbnail from each of the related pages and show them within the frontend of the product page in question.

    My test has 10 pages in a relationship with this product and all that gets printed to the product page is 10X image, permalink, title and thumbnail of the product page and not the relationship pages images, permalinks, titles and thumbnails.

    I’ve added the following in to functions.php

    I’m sure this is doable but as a non developer in training i’m stumped here and would very much appreciate your assistance/guidance.

    Many thanks.

    add_action( 'woocommerce_product_thumbnails', 'packagelogos', 20 );
    function packagelogos() { ?>
    <div class="channel-list">
    <?php
    $posts = get_field('select');
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<div class="channel-list-item">
    					<img class="image img-responsive lazyloaded" alt="<?php the_title(); ?>" src="<?php if ( has_post_thumbnail() ) { the_post_thumbnail_url(); } ?>">
    					<div class="title"><?php the_title(); ?></div>
    				</div>
    			</a>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>									
    </div>
    <?php
    };
  • you need to add the global $post inside of your function. Most of the code examples provided in the documents assume you’re working directly in a template file where WP has declared this global for you. Inside a function you need to do it yourself.

    
    add_action( 'woocommerce_product_thumbnails', 'packagelogos', 20 );
    function packagelogos() { 
    
    global $post;
    
    ?>
    <div class="channel-list">
    <?php
    $posts = get_field('select');
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<div class="channel-list-item">
    					<img class="image img-responsive lazyloaded" alt="<?php the_title(); ?>" src="<?php if ( has_post_thumbnail() ) { the_post_thumbnail_url(); } ?>">
    					<div class="title"><?php the_title(); ?></div>
    				</div>
    			</a>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>									
    </div>
    <?php
    };
    
  • I’m so grateful for your time. global $post; certainly did solve my issues.

    Many thanks

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

You must be logged in to reply to this topic.