WebBox

Lista brendova pa proizvoda

Oct 27th, 2023
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | Source Code | 0 0
  1. <?php
  2.   $brand_list = array();
  3.  
  4.   $args = array(
  5.       'post_type' => 'product',
  6.       'posts_per_page' => -1,
  7.   );
  8.  
  9.   $products = new WP_Query($args);
  10.  
  11.   while ($products->have_posts()) {
  12.       $products->the_post();
  13.  
  14.       $product = wc_get_product(get_the_ID());
  15.       $brand = $product->get_attribute('pa_proizvodac');
  16.  
  17.       if (!empty($brand) && !in_array($brand, $brand_list)) {
  18.           $brand_list[] = $brand;
  19.       }
  20.   }
  21.  
  22.   var_dump($brand_list);
  23.  
  24.   wp_reset_postdata();
  25.  
  26. foreach ($brand_list as $brand) {
  27.     echo '<h2>' . $brand . '</h2>';
  28.    
  29.     $args = array(
  30.         'post_type' => 'product',
  31.         'posts_per_page' => -1,
  32.         'tax_query' => array(
  33.             array(
  34.                 'taxonomy' => 'pa_proizvodac',
  35.                 'field' => 'slug',
  36.                 'terms' => $brand,
  37.             ),
  38.         ),
  39.     );
  40.  
  41.     $products = new WP_Query($args);
  42.     print_r($args);
  43.  
  44.     if ($products->have_posts()) {
  45.         while ($products->have_posts()) {
  46.             $products->the_post();
  47.             echo '<br><strong>' . get_the_title() . '</strong>';
  48.         }
  49.     }
  50.  
  51.     wp_reset_postdata();
  52. }
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment