1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
protected function getproducts(){ global $woocommerce; global $wp_query; global $wpdb; // Get the total product count. $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1 ); // $output['args'] = $args ; $products = new WP_Query( $args ); $count = $products->found_posts; //$output['count'] = $count ; global $post; // Get the products. $xgetproducts = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $count, ); $allproducts = new WP_Query( $xgetproducts ); $allproducts = $allproducts->get_posts(); // $output['products'] = $allproducts ; $mywebproducts = []; foreach ($allproducts as $post) { $productInstance = new WC_Product($post->ID); $product = (object)[]; $product->id = $post->ID; $product->slug = $post->post_name; $product->name = $post->post_title; $product->permalink = get_permalink($post->ID); $product->regular_price = $productInstance->get_regular_price(); $product->sale_price = $productInstance->get_sale_price(); $product->images = []; //product_tag $terms_product_tag = get_the_terms( $post->ID, 'product_tag' ); $term_array_product_tag = array(); if ( ! empty( $terms_product_tag ) && ! is_wp_error( $terms_product_tag ) ){ foreach ( $terms_product_tag as $term ) { $term_array_product_tag[] = $term->name; } } $product->product_tags = $term_array_product_tag; $sizes_attributes = $productInstance->get_attribute( 'sizes' ); $product->sizes_attributes = $sizes_attributes; $price_html = $productInstance->get_price_html(); $product->price_html = $this->price_array($price_html); global $_wp_additional_image_sizes; foreach ($_wp_additional_image_sizes as $size => $value) { $image_info = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $size); $product->images[0][$size] = $image_info[0]; } $mywebproducts[] = $product; } $output['products'] = $mywebproducts ; return $output; } function price_array($price){ $del = array('<span class="woocommerce-Price-amount amount">', '<span class="woocommerce-Price-currencySymbol">' ,'</span>','<del>','<ins>', '₽'); $price = str_replace($del, '', $price); $price = str_replace('</del>', '|', $price); $price = str_replace('</ins>', '|', $price); $price_arr = explode('|', $price); $price_arr = array_filter($price_arr); return $price_arr; } |
additionally you could apply filter as you want, e.g:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php if(isset ($_GET)){ $current_search = $_GET['search']; $current_product_cat = $_GET['product-cat']; $current_items_order_by = $_GET['order_by']; $current_paged = $_GET['paged']; $include = $_GET['include']; $current_colors = $_GET['colors'] ? explode( ',', $_GET['colors']) : []; $args = array( 'post_status' => 'publish', 'post_type' => array('product', 'product_variation'), 'post_type' => 'product', 'posts_per_page' => 16, 's' => $current_search, 'paged' => ( $current_paged ? $current_paged : 1 ), ); if($include){ $args['post__in'] = explode( ',', $include); } $args['tax_query'] = array('relation' => 'AND'); $args['meta_query'] = array('relation' => 'AND'); if (isset($current_product_cat) && !(empty($current_product_cat)) && !is_null($current_product_cat) && !($current_product_cat=="null")) { $request_params = array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $current_product_cat ); array_push($args['tax_query'], $request_params); } if (isset($current_colors) && !(empty($current_colors))) { $request_params = array( 'taxonomy' => 'pa_colors', 'field' => 'slug', 'terms' => $current_colors, ); array_push($args['tax_query'], $request_params); } //make the query //$result = new WP_Query($args); } |
Json output of the call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
{ "products": [ { "id": 40, "slug": "vneck-tee", "name": "Vneck Tshirt", "permalink": "https:\/\/myweb-app.com\/product\/vneck-tee\/", "regular_price": "18", "sale_price": "", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/vneck-tee.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$18.00" ] }, { "id": 39, "slug": "tshirt", "name": "Tshirt", "permalink": "https:\/\/myweb-app.com\/product\/tshirt\/", "regular_price": "18", "sale_price": "", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/tshirt.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$18.00" ] }, { "id": 38, "slug": "polo", "name": "Polo", "permalink": "https:\/\/myweb-app.com\/product\/polo\/", "regular_price": "20", "sale_price": "", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/polo.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$20.00" ] }, { "id": 37, "slug": "long-sleeve-tee", "name": "Long Sleeve Tee", "permalink": "https:\/\/myweb-app.com\/product\/long-sleeve-tee\/", "regular_price": "25", "sale_price": "", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/long-sleeve-tee.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$25.00" ] }, { "id": 36, "slug": "hoodie", "name": "Hoodie", "permalink": "https:\/\/myweb-app.com\/product\/hoodie\/", "regular_price": "45", "sale_price": "42", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$45.00", " $42.00" ] }, { "id": 35, "slug": "hoodie-with-zipper", "name": "Hoodie with Zipper", "permalink": "https:\/\/myweb-app.com\/product\/hoodie-with-zipper\/", "regular_price": "45", "sale_price": "", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-zipper.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$45.00" ] }, { "id": 34, "slug": "hoodie-with-pocket", "name": "Hoodie with Pocket", "permalink": "https:\/\/myweb-app.com\/product\/hoodie-with-pocket\/", "regular_price": "45", "sale_price": "35", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-pocket.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$45.00", " $35.00" ] }, { "id": 33, "slug": "hoodie-with-logo", "name": "Hoodie with Logo", "permalink": "https:\/\/myweb-app.com\/product\/hoodie-with-logo\/", "regular_price": "45", "sale_price": "20", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/hoodie-with-logo.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$45.00", " $20.00" ] }, { "id": 32, "slug": "sunglasses", "name": "Sunglasses", "permalink": "https:\/\/myweb-app.com\/product\/sunglasses\/", "regular_price": "90", "sale_price": "60", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/sunglasses.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$90.00", " $60.00" ] }, { "id": 31, "slug": "cap", "name": "Cap", "permalink": "https:\/\/myweb-app.com\/product\/cap\/", "regular_price": "18", "sale_price": "16", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/cap.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$18.00", " $16.00" ] }, { "id": 30, "slug": "belt", "name": "Belt", "permalink": "https:\/\/myweb-app.com\/product\/belt\/", "regular_price": "65", "sale_price": "55", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/belt.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$65.00", " $55.00" ] }, { "id": 29, "slug": "beanie", "name": "Beanie", "permalink": "https:\/\/myweb-app.com\/product\/beanie\/", "regular_price": "20", "sale_price": "18", "images": [ { "gbe_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "gbe_image": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "woocommerce_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "woocommerce_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "woocommerce_gallery_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "shop_catalog": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "shop_single": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg", "shop_thumbnail": "https:\/\/myweb-app.com\/wp-content\/uploads\/2019\/04\/beanie.jpg" } ], "product_tags": [ ], "sizes_attributes": "", "price_html": [ "$20.00", " $18.00" ] } ] } |