1

I have created three new related product fields what I want to show under single product view.

Product has variations pa_color with terms red, black, grey

So I created into relations tab new three fields where I can select red products, black products and grey products what should be seen in single product under where was shown upselling products before. But I want it changes if customer selects varation black example then it will show black related products.

$test = get_post_meta( $post->ID, '_black_products_ids', true ); // this will take show black related products, but without parent details what I need to show.

//$product = new WC_Product(get_the_ID()); //these was taking related upselling products
//$related_products = $product->get_upsells();
//$related_products_count = count($related_products);
section class="beige pt-5 pb-3">
    <div class="container-fluid px-0">
        <div class="row">
            <div class="col-md-12">
                <h2 class="mb-4 all-title"><?php _e('Related products', 'wc'); ?></h2>
                <article>
                    <div class="row">
                        <?php 
                        foreach($test as $related_product):
                        //foreach($related_products as $related_product):
                        $post_object = get_post($related_product);
                        setup_postdata($GLOBALS['post'] =& $post_object);
                            wc_get_template_part('single-product-card');
                        endforeach; ?>
                    </div>
                </article>
            </div>
        </div>
    </div>
</section>

My related products card (single-product-card):

$product_thumbnail = (get_the_post_thumbnail_url()) ? get_the_post_thumbnail_url() : wc_placeholder_img_src();
$product = wc_get_product( get_the_ID() ); ?>


<div class="col-lg-3 col-md-6 mb-md-3 mb-3 <?php if(is_front_page()) { echo 'pr-0 pl-md-3 pl-0'; }?>">
    <article class="product-card h-100 p-3">
        <a class="h-100 d-flex flex-column" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <img src="<?php echo $product_thumbnail; ?>" class="card-img img-fluid position-relative" alt="<?php the_title(); ?>">
            <div class="card-price py-1 px-3">
                <p class="mb-0 <?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>
            </div>
            <div class="card-header-details pt-3">
                <div class="row">
                    <div class="col-md-12 mb-3">
                        <h3 class="card-title mb-0"><?php the_title(); ?></h3>   
                    </div>
                    <div class="col-md-12">
                    <?php if( have_rows('product_info') ): ?>
                        <div class="product-info">
                        <?php while( have_rows('product_info') ) : the_row(); ?>
                            <div class="row">
                                <div class="col-md-3">
                                    <p class="mb-0 heading"><?php the_sub_field('info_heading'); ?></p>
                                </div>
                                <div class="col-md-9">
                                    <p class="mb-0 value"><?php the_sub_field('info_value'); ?></p>
                                </div>
                            </div>
                        <?php endwhile; ?>
                        </div>
                    <?php endif; ?>
                    </div>
                </div>
            </div>
        </a>
    </article>
</div>

Missing details: product_info, info_heading, info_value


Maybe I do all this thing wrong and harder way but I think im close to it.

I used this topic to create these fields to relation tab under product: How to add more custom field in linked product of woocommerce

kmb67116
  • 161
  • 9

0 Answers0