0

I'm looking for how to show products from their brand category.

My ULR is in the format www.mywebsite/brand1 or www.mywebsite/brand2 ...

My JS script with the Regex allows me to get brand1 or brand2, or whatever brand name.

I would like to display on each page the products corresponding to these brands via the product category of Woocommerce.

When I do an echo $brand I've got exactly what I need => brand2 so I 'm thinking "that's it !"

But with my code, when I try to put the $brand into PHP it seems incorrect. Maybe my PHP variable can't be inserted like this ... I don't know ...

I've searched a lot on the web but maybe didn't google it the right way.

Any helps will be great

 <script type="text/javascript">

   let url = document.location.pathname;
   let regex = /.*\/(.*?)\//;
   let brand = url.match(regex);
   // console.log(brand[1]) ---> brand2
   // that's good but not enough    
               
  <?php $brand = "<script>document.write(brand[1])</script>" ?>

</script>

<?php

   echo do_shortcode('[products category= "' . $brand . '" ]');

   // I need to find exactly this line below 
   // echo do_shortcode('[products category=brand2]');

?>
Lo1176
  • 29
  • 6
  • What is the file name of this file? – MaartenDev Jul 19 '21 at 18:32
  • Does this answer your question? [Get WooCommerce product categories from WordPress](https://stackoverflow.com/questions/21009516/get-woocommerce-product-categories-from-wordpress) – AndrewL64 Jul 19 '21 at 18:32
  • Maybe my question was badly formulated. My ULR is in the format www.mywebsite/ligne-w or www.mywebsite/brand1 or www.mywebsite/brand2 ... My JS script with the Regex allows me to get line-w or brand1 or brand2, or whatever brand name ... I would like to display on each page the products corresponding to these brands via the product category of Woocommerce. When I do a `echo $brand`have got excatly what I need => ligne-w But whit my code, when I put it into PHP have got an empty string I guess – Lo1176 Jul 19 '21 at 19:25
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CBroe Jul 20 '21 at 06:53

1 Answers1

0

Using JS complicates this scenario when you can just use PHP.

If you are attempting to get querystring params then use $_GET which will be an array of your name/values for querystring (NOT SANITIZED).

Example: /abc?a=121&b=212&c=313&d=random_string

<?php
    var_dump($_GET);

    /*
    * This will dump...
    * array(
    *     'a' => 121,
    *     'b' => 212,
    *     'c' => 313,
    *     'd' => 'random_string'
    */
?>

So knowing that your solution could be something like:

<?php
   // Assuming a URL like /store?brand=abc123
   // Attempt to sanitize input

   $brand = htmlspecialchars($_GET['brand'], ENT_QUOTES)
   echo do_shortcode('[products category= "' . $brand . '" ]');

?>
Ray
  • 773
  • 9
  • 21
  • Thanks Ray, I'm sure this answer will help later but it is not what I was looking for. I edited my first message since, try to be more precise on what I'm looking for. My bad ! – Lo1176 Jul 19 '21 at 22:55