Ahmad Naser Turnkey Ecosystem Ahmad Naser
November 25, 2019

Rest Api Call for internal wordpress and woocommerce products

Published in the Ahmad Naser ecosystem. Estimated reading time: 8 minutes.

 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>', '&#8381;');
            $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:

<?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

{
"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": [
"&#36;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": [
"&#36;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": [
"&#36;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": [
"&#36;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": [
"&#36;45.00",
" &#36;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": [
"&#36;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": [
"&#36;45.00",
" &#36;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": [
"&#36;45.00",
" &#36;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": [
"&#36;90.00",
" &#36;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": [
"&#36;18.00",
" &#36;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": [
"&#36;65.00",
" &#36;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": [
"&#36;20.00",
" &#36;18.00"
]
}
]
}

Leave a Reply

Your email address will not be published. Required fields are marked *