Support

Account

Home Forums Backend Issues (wp-admin) Show parent product ACF fields on variation product

Unread

Show parent product ACF fields on variation product

  • I have products what have variations and I want show related products based which variation is selected and show product ACF fields on that.

    My product view and under that is related products:

    <?php
    
    global $product;
    
    if (!defined('ABSPATH')){
        exit;
    }
    
    $test = get_post_meta( $post->ID, '_black_products_ids', true );
    
    //$product = new WC_Product(get_the_ID());
    //$related_products = $product->get_upsells();
    $related_products_count = count($test);
    
    get_header('shop');
    
    ?>
    
    <?php while ( have_posts() ) :
    the_post(); ?>
    
    <article class="product">
        <section class="mt-5 pt-5">
            <div class="container-fluid single-box p-4">
            <?php do_action( 'woocommerce_before_single_product' ); ?>
                <div class="row">
                    <div class="col-md-4 mb-md-0 mb-3">
                        <?php wc_get_template( 'single-product/product-image.php' ); ?>
                    </div>
                    <div class="col-md-8">
                        <div class="single-inner d-flex flex-column h-100 p-md-4 p-0">
                            <div class="row">
                                <div class="col-md-6">
                                    <h1><?php the_title(); ?></h1>
                                </div>
                                <div class="col-md-6">
                                    <h2 class="mt-md-0 mt-3"><?php _e('About product', 'rv'); ?></h2>
                                </div>
                            </div>
                            <div class="row pt-3">
                                <div class="col-md-6">
                                <?php if( have_rows('product_info') ): ?>
                                    <div class="single 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 class="cart-box pt-4">
                                        <?php do_action( 'woocommerce_single_product_summary' ); ?>
                                    </div>
                                </div>
                                <div class="col-md-5">
                                    <?php the_content(); ?>  
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    
        <?php if($related_products_count > 0): ?>
        <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', 'rv'); ?></h2>
                        <article>
                            <div class="row">
                                <?php foreach($test 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>
        <?php endif; ?>
    </article>
    
    <?php endwhile;?>
    
    <?php get_footer('shop');

    My related product card view where I should get parent product data:

    <?php
    // Get product thumbnail
    $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-0 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>
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.