Support

Account

Home Forums General Issues Display product price in Relationship field

Solving

Display product price in Relationship field

  • Hi, I am using ACF relationship field to pick some related products. I then display those related products on a single product page (I use Woocommerce) and I have problem with displaying proper price.
    I am using this code:

    <?php
       $product = new WC_Product(get_the_ID());
      echo wc_price($product->get_price_including_tax(1,$product->get_price()));
    ?>

    but it displays price of a main single product which is on the page, rather than showing price for each related product in a loop, So if the price of the product on single page is £9.00, every product in related products grid will display with £9.00 too rather than it’s own price..

    I followed the documentation and to display correct thumbnails for each related product I had to use this code:
    <?php echo get_the_post_thumbnail( $p->ID, ‘175×100’ ) ?>
    rather than just ‘the_thumbnail ‘ to show correct thumbnails.
    Is there similar way to display correct product price?

    This is my whole code:

    <?php 
    $posts = get_field('related_set_1');
    if( $posts ): ?>
    <?php foreach( $posts as $p): ?>
    <li>
        <a href="<?php echo get_permalink( $p->ID ); ?>">
            <?php 
              echo get_the_post_thumbnail( $p->ID, '175x100' )
                    ?>
                <div style="overflow:hidden">
                    <h4><?php echo $p->post_title; ?></h4>
                    <p class="price">
                        <?php 
                        global $post;
                        $product = new WC_Product($post->ID); 
                        echo     wc_price($product->get_price_including_tax(1,$product->get_price()));
                        ?>
                    </p>
                    <p class="link">View now</p>
                </div>
        </a>
    </li>
    <?php endforeach; ?>
        <?php endif; ?>
  • Hi @fanta00

    I think you need to use the $p variable instead of the $post variable. It should be something like this:

    $product = new WC_Product($p->ID);

    I hope this helps.

  • Thank you, indeed it should be $p like in the rest of the code!
    It works fine now for regular prices. However when I have sale price and regular price for one product it displays: 0.00 and I get error : “Trying to get property of non-object…” on this line:
    $product = new WC_Product($p->ID);
    Is there anything else I missed?

  • Hi @fanta00

    Could you please debug the returned object? You can do it by using var_dump() function like this:

    <?php foreach( $posts as $p): ?>
        <pre><?php var_dump($p); ?></pre>

    This page should give you more idea about it: http://www.advancedcustomfields.com/resources/debug/.

    You can also try to set the ID manually like this:

    $product = new WC_Product(99);

    Where 99 is the ID of your product which has a sale price and regular prices.

    Thanks!

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

The topic ‘Display product price in Relationship field’ is closed to new replies.