• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How randomize products display on a page using Divi theme and Woocommerce

erikamercier

New Coder
I am doing a shop page using Divi theme and I am trying to display randomly products from a certain category.

The problem I have is that the theme does not have options to randomize the way the articles are displaying on the shop page.

I also tried a woocommerce shortcode:

[products limit="30" columns="3" category="cardigans-sweaters" cat_operator="AND" orderby="rand" order="rand"]

It worked well on desktop, but the problem is that the visual I get on mobile is not the one I want (it is one column instead of two, plus it kinda lost the CSS I did myself).

So yeah if someone knows a PHP code, or any code I could use to randomize, I would be really happy!
 

LTomy

Active Coder
Staff Team
Guardian
Hi erikamercier,
Welcome to CodeForum!

I know nothing about woocommerce, but it should not be too hard to write a PHP code to randomize the items that are displayed, but the exact way to do it depends on how your website works.
Other members of the forum may be able to provide a more helpful reply?
 
Last edited:

erikamercier

New Coder
@LTomy Indeed, I think a PHP code could work! :)

Our website is www.orangefashionvillage.com

I am trying to randomize the display of the products of certain categories. Like on the page Bamboo, I want to randomize the display of products of Bamboo only, and do the same with the pages Spring and Summer, and Fall and Winter.

I am no pro in PHP, so if someone could help me with that, that would be really awesome :)

(I am new here so if my post is not to the right place, please tell me!)
 

LTomy

Active Coder
Staff Team
Guardian
I do no have that much experience in PHP and, again, I do not know how your website works, but here is a code 'template' that could be used to achieve what you want:
PHP:
// Number of products that can be displayed.
$nbItems = 500

// An array containing the Ids of the products that are displayed.
$productsDisplayed = array();

for($i = 0; $i < $nbItems; $i++)
{
    do
    {
        // We retrieve a random Id.
        $productId = rand(1, $nbItems);
    }
    while(in_array($productId, $productsDisplayed); // If the product is already displayed, we find an other.
 
    // We add the Id to the array of displayed products.
    $productsDisplayed[i] = $productId;
       
    /*
    Here, we display a different product in function
    of the value of 'productId'.
    */
}
 
Last edited:
Top