Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

woocommerce get products gets only first 100. #26

Open
yilmazchef opened this issue Jan 30, 2020 · 3 comments
Open

woocommerce get products gets only first 100. #26

yilmazchef opened this issue Jan 30, 2020 · 3 comments

Comments

@yilmazchef
Copy link

yilmazchef commented Jan 30, 2020

Dear Coder,

Firstly, I would like to thank you for providing such useful library. I have been developing a microservice-based application and using your library multiple times.

When I use the code below:

    Map<String, String> params = new HashMap<>();
    params.put("per_page", "100");
    params.put("offset", "0");
    List<Map<String, Object>> productsMap = wooCommerce.getAll(EndpointBaseType.PRODUCTS.getValue(), params);

Can you please help me to get all the products as Map<String, Object> because I will need to collect all the data from stores.

Thank you very much in advance.

Best regards,

Yilmaz.

@omandryk
Copy link
Collaborator

Hi Yilmaz,
if you are using WC v3 try with the following params:

params.put("page", "1");
params.put("per_page", String.valueOf(Integer.MAX_VALUE));

Read more on WC Rest API Docs: http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products

Best regards,
Oleksandr Mandryk

@yilmazchef
Copy link
Author

Hello Oleksandr,

I have tried
params.put("page", "1");
params.put("per_page", String.valueOf(Integer.MAX_VALUE));

however, I received an error..

Is there any other option.

I have coded another solution, I get data 100 products per page, and trying to cache them.

but it would be amazing if I could get all data..

@craftedsro
Copy link

To get all products you have to work with page...

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.icoderman.woocommerce.ApiVersionType;
import com.icoderman.woocommerce.EndpointBaseType;
import com.icoderman.woocommerce.WooCommerce;
import com.icoderman.woocommerce.WooCommerceAPI;
import com.icoderman.woocommerce.oauth.OAuthConfig;

public class Test {

public static void main(String[] args) {
	String url = "http://example.com";
	String ck = "customer_key_from_your_woocommerce_page";
	String cs = "customer_secret_from_your_woocommerce_page";
	
	Map<String, String> options = new HashMap<String, String>();
	OAuthConfig config = new OAuthConfig(url, ck, cs);
	WooCommerce woo = new WooCommerceAPI(config, ApiVersionType.V3);

	List<?> temp;
	List<HashMap<?, ?>> productsTmp = new ArrayList<HashMap<?, ?>>();

	int perPage = 100;
	int page = 1;
	boolean end = false;

	options.put("page", String.valueOf(page));
	options.put("per_page", String.valueOf(perPage));

	while(!end) {			
		temp = woo.getAll(EndpointBaseType.PRODUCTS.getValue(), options);
			
		if(!temp.isEmpty()) {
			for(int i = 0; i < temp.size(); i++) {
				productsTmp.add( (HashMap<?, ?>) temp.get(i) );
			}
			options.put("page", String.valueOf(++page));
		}
		else {
			end = true;
		}
	}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants