• Version WP: 4.8.1
    Version Woo: 3.1.2

    In simple product page I want to show products list from the same groups.

    Example:
    Group1(grouped product)
    – product1
    – product2
    – product3

    Group2(grouped product)
    – product1
    – product4
    – product5

    In product page “product1” I want to show list products consist of product2, product3, product4, product5

    In product page “product2” I want to show list products consist of product1 and product3

    Before Woocommerce 3.0.0 I used this function in file content-single-product.php:

    if( $post->post_parent != 0 || $product->is_type( 'grouped' ) ) {
    
    	if( $post->post_parent != 0 ) {
    		$parent_id = $post->post_parent;
    	} else {
    		$parent_id = $post->ID;
    	}
    	$parent_product = get_product( $parent_id );
    	$childrens = $parent_product->get_children();
    	if (!empty($childrens)) {
    
    		global $product, $woocommerce_loop;
    
    		$args = array(
    			'post_type'				=> 'product',
    			'ignore_sticky_posts'	=> 1,
    			//'no_found_rows' 		=> 1,
    			'posts_per_page' 		=> -1,
    			'orderby' 				=> $orderby,
    			'post__in' 				=> $childrens
    			);
    
    		$parent_products = new WP_Query( $args );
    
    		$woocommerce_loop['columns'] 	= $columns;
    
    		if ( $parent_products->have_posts() ) { ?>
    
    			<div class="parent-products products compact-layout grid--float">
    				<div class="title">
    					<h4><?php printf( __( 'Collection Products &ldquo;%s&rdquo;', 'mk_framework' ), get_the_title($parent_id) ); ?></h4>
    					<img alt="" title="" width="150" height="24" src="<?php echo get_site_url(); ?>/wp-content/uploads/2016/12/div.png">
    				</div>
    				<section class=" mk--row">
    					<?php woocommerce_product_loop_start(); ?>
    
    					<?php while ( $parent_products->have_posts() ) : $parent_products->the_post(); ?>
    
    					<?php wc_get_template_part( 'content', 'product' ); ?>
    
    					<?php endwhile; // end of the loop. ?>
    
    					<?php woocommerce_product_loop_end(); ?>
    				</section>
    			</div>
    		<?php }
    	}
    }

    But $post->post_parent not working, and return 0. What alternative? How do I get a list ID’s grouped products for sample product?

    Thank!

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

  • The topic ‘How to get products list from the same groups’ is closed to new replies.