• Resolved shenalorlof

    (@shenalorlof)


    Hi dear supporters i really need this to be done, it’s an urgent

    i want to Display latest 6 products in a row in a random order from the latest 20 product posts.

    My points should be in the algorithm is…..
    1. display 6 products in row, 2. from last 20 latest products, 3. in a random order. All these should be in same

    i want it from the last 20 latest products..

    I got following code as a solution from a supporter but it shows very old products as well

    function ijab_random_products() {
    	?>
    
    	<ul class="products columns-3">
    		<?php
    		$args = array(
    			'post_type' => 'product',
    			'posts_per_page' => 20,
    			'orderby' => 'date',
    			'no_found_rows' => true,
    		);
    		$counter = 1;
    		$max = 6;
    		$loop = new WP_Query( $args );
    		shuffle($loop->posts);
    		if ( $loop->have_posts() ) {
    			while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
    				$counter++;
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    		?>
    	</ul>
    
    	<?php 
    }
    add_shortcode( 'randomprod', 'ijab_random_products' );

    Can we do like this, the logic..
    First we sort products by the date,
    Second take 20 products starting from the first row
    Third shuffle that 20 products we got and display 6 in a row

    Can we do this and get this done

    Can we change the above code something like this code please? i want to display 6 products from the latest 20 products in a random order (shuffle).. like this https://ibb.co/3cxn43C

    Hope this code might give an idea..

    $recent_posts = wp_get_recent_posts( array(
    ‘numberposts’ => 20, // Number of recent posts
    ‘post_status’ => ‘publish’, // Show only the published posts
    ‘post_type’ => ‘product’
    ));
    
    $sub = array_splice( $recent_posts, 10, 10 );// array_splice ( array, offset, length )
    
    shuffle( $sub ); // Random
    
    array_splice( $recent_posts, 10, 0, $sub );
    
    foreach( $recent_posts as $post ) {
    echo $post[‘ID’] . ‘<br>’;
    //echo ‘
    
    ', print_r( $post, 1), '
    ‘;
    } // Loop

    >> Show 6 products in a row from the last 20 products in a random order << i want it to be show like this as in the image https://ibb.co/3cxn43C

    Thank you in advance dear supporters

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @shenalorlof ,

    So, from your description, you want to show 20 latest products in 6 columns and in random order. Is that right?

    WooCommerce comes with a product shortcode that can be used to show products in this way. So, using your requirement you can set the shortcode like this –

    [products limit="20" columns="6" orderby="rand" order="DESC"]

    You can find full details of available products shortcode parameters here – https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-10

    You can also do these in code format. You can take a look at these examples to figure the coding solution – https://sarathlal.com/custom-product-query-wp-query-woocommerce/

    I hope the information helps.

    Thank you ??

    Thread Starter shenalorlof

    (@shenalorlof)

    Hi @rur165 thank you for ur reply. But what I want is to show 6 products from the latest 20 products. Which is 1 row 6 columns. 20 latest products in shuffle (random order)

    Thank you

    Hello @shenalorlof ,

    Okay, thanks a lot for explaining the exact goal. Unfortunately, the shortcode method does not work in that way. This needs to be solved using custom code.

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you ??

    laceyrod

    (@laceyrod)

    Automattic Happiness Engineer

    Hi there,

    This thread has been inactive for a bit, so I’m going to mark it as Resolved now for the overall health of the forums. Please feel free to check out the resources listed above for further assistance.

    Cheers!

    Thread Starter shenalorlof

    (@shenalorlof)

    This code works.

    function ijab_random_products() {
    	?>
    
    	<ul class="products columns-3">
    		<?php
    		$args = array(
    			'post_type' => 'product',
    			'posts_per_page' => 20,
    			'orderby' => 'date',
    			'no_found_rows' => true,
    		);
    		$counter = 1;
    		$max = 6;
    		$loop = new WP_Query( $args );
    		shuffle($loop->posts);
    		if ( $loop->have_posts() ) {
    			while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
    				$counter++;
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    		?>
    	</ul>
    
    	<?php 
    }
    add_shortcode( 'randomprod', 'ijab_random_products' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display 6 products in a random order from the latest 20 product posts’ is closed to new replies.