• Resolved joosemi1993

    (@joosemi1993)


    Well, I’m trying to create a module with the related products I have in my shop. My template has a file in the route /theme-name/woocommerce/single-product/related.php. In this we can see some code of how it call to the related product. It use the template as the listing product page (same template), but when I tried to shpw the thumbnail or the price it doesn’t work. It only show me the title of the product, nothing else.

    This is the code I have in my template for the file /related.php

    <?php 
    if ( $related_products ) : ?>
        <section id="nm-related" class="related products">
            <div class="nm-row">
                <div class="col-xs-12">
                    <h2><?php esc_html_e( 'Related products', 'woocommerce' ); ?></h2>
                    <?php woocommerce_product_loop_start(); ?>
                        <?php foreach ( $related_products as $related_product ) : ?>
                            <?php
                                $post_object = get_post( $related_product->get_id() );
                                setup_postdata( $GLOBALS['post'] =& $post_object );
                                wc_get_template_part( 'content', 'product' ); ?>
                        <?php endforeach; ?>
                    <?php woocommerce_product_loop_end(); ?>
                </div>
            </div>
        </section>
    <?php endif;

    After that I use the “content-product” template like the listing page, where I don’t have any problem.

    For example, to show the product thumbnail I tried this:

    <div class="nm-shop-loop-thumbnail nm-loader">
        <a href="<?php echo esc_url( get_permalink() ); ?>" class="nm-shop-loop-thumbnail-link woocommerce-LoopProduct-link">
            <?php
            $id_pro = get_the_ID();
            $pro_2 = get_post_thumbnail_id($id_pro);
            $featured_image_url = wp_get_attachment_url( $pro_2 );
            if(! empty( $featured_image_url )) { ?>
                <?php
                /**
                * Hook: woocommerce_before_shop_loop_item_title.
                *
                * @hooked woocommerce_show_product_loop_sale_flash - 10
                * @hooked woocommerce_template_loop_product_thumbnail - 10
                */
                do_action( 'woocommerce_before_shop_loop_item_title' );
            } else { ?>
                <img src="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg" data-src="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg" data-srcset="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 350w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 250w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 400w" alt="" sizes="(max-width: 350px) 100vw, 350px" width="350" height="420" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail wp-post-image  lazyloaded" srcset="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 350w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 250w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 400w">
              <?php
              }
              ?>
        </a>
    </div>

    And as I said, it works for the listing page but not inside single page product in related products section.

    The $id_pro variable work in both pages, it shows the id of the product, but when I try to get the thumbnail URL with this id it return me “”.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Thumbnail and price not showing on related products section’ is closed to new replies.