WooCommerce Rest API get products by slug - wordpress

New to WooCommerce, and using the REST API to get products which I have successfully done.
Not sure on how to get a single product using the slug as permalink gives full url, where as i only need the end slug so I can have clean urls and not use the id to find a record. I know with the Wordpress rest api plugin you can do this posts-api?filter[name]== and get the post by product slug, rather than use:
/wc-api/v3/products/id
I want to be able to do:
/wc-api/v3/products/slug

You just have to adjust the query slightly /wc-api/v3/products/slug should be /wc-api/v3/products/?slug=your-product-slug.
One important difference is that this will return the slug inside an array, so you will have to select the first object in the array once you have parsed it.

Related

Is there any way to search products by name in WooCommerce API

I am trying to retrieve a product by name using WooCommerce API but I failed to do so.
Is there a way to find a product by its name?
Thank you.
You can retrieve a product by name using the WooCommerce REST API by using the list all products endpoint with the parameter ?search. List of the parameters here. With the list of parameters, there's quite a bit you can do with the list all products endpoint.
Your whole request url might look something like this:
.../wp-json/wc/v3/products/?search="Product Name"
The search parameter will look for a matching string in the name of the product. So if you have multiple products with the same words in them, they will also be returned.
I retrieve a product by name using the WooCommerce REST API with parameters.
I used python for it. You can use a similar approach:
def get_product_by_name():
product_list = wcapi.get("products", params={'search': {searchQuery}}).json()
count_of_items = len(product_list)
... do something else
{searchQuery} - product name(or part of name)

WPGraphQL: Custom Post Types query using SLUG as unique identifier returns no data

I've created a Custom Post Type in WordPress named "projects" and now I'm trying to query a single project using the slug as an identifier. In other words, I want the idType to be the SLUG, not ID. I tried the following query but it does not work.
However, this query works (it's a common query that is using the ID) :
The query above is not what i'm looking for, but at least i know that my CPT is registered correctly and that my data can be fetched somehow.
Why I think this is a problem?
Because I used the same type of query for querying WordPress posts (not custom posts types, just posts) and it works fine. I'm being returned the correct data.
Any help is appreciated ;) I don't know if i'm missing something with my query, or if custom post types don't work the same way that posts do.
WPGraphQL version: 1.3.10 Other WordPress plugins installed: Advanced
Custom Fields, Custom Post Type UI
Set the idType to URI. Here's an example, querying a page (it's the same for other post types as well):
NOTE: the GraphiQL IDE has some nice auto-completions and drop-downs to show the available options:
My understanding is the idType:SLUG option is not available when your custom post type is hierarchical since the slug would not be a unique identifier at that point.

Get all categories, custom field values in Wordpress query?

I've setup an API endpoint on a Wordpress site I'm working on, but the posts I'm retrieving have quite a bit of associated data, I think doing a query to get the posts then running the posts through a loop and getting this data will be too intensive, is there a catch all that can get all meta data at the same time as getting the posts?
If you're using REST API, you can use the function register_rest_field to add fields to responses. And then you can query all custom fields to put them in the responses.
If you're using a plugin like Meta Box to create custom fields, then you can use its free extension MB REST API to pull all the custom fields into REST API responses for you.

How to sort products in WooCommerce WordPress JSON API

I am using WooCommerce JSON API on my mobile app but I am having problems sorting the the product list.
This is my url https://www.storeurl.com/wp-json/wc/v1/products but I don't know which parameters to add to the url to sort the products according to price, reviews, rating and popularity.
I have read the docs but I couldn't find it. Please do you know I could do this?
You just need to append the parameters as part of the query string like so...
https://www.storeurl.com/wc-api/v3/products?orderby=title&order=asc
To take that further, you can use the "Filter" parameter, which allows you to use any WP_Query style arguments you may want to add to your request. So for example, if you wish to sort by "Price", you would do something like...
https://www.storeurl.com/wc-api/v3/products?filter[order]=asc&filter[orderby]=meta_value_num&filter[orderby_meta_key]=_regular_price
Filter - Use WP Query arguments to modify the response; private query
vars require appropriate authorization.
Ref: https://woothemes.github.io/woocommerce-rest-api-docs/#list-all-products
After a long search I found solution.
you need to pass query Param like below
https://v1t.a8c.myftpupload.com/wp-json/wc/v3/products? consumer_key=ck_4fxx69xxx195565b4ffca55aca56d2f4d6e&consumer_secret=cs_3928b95d5xxxxxe1e82xx210748956&page=1&status=publish&category=130&per_page=1&orderby=price&order=asc.
Also you can sort by below param.
date, id, include, title, slug, modified, menu_order, price, popularity, rating
Ex: &orderby=popularity, &orderby=rating
if you still facing issue comment here i will help you.
Thanks

Wordpress - Passing Variable to Custom Post Type Archive

I am using /%postname%/ permalinks and want to be able to pass a variable to my custom post type archive. My goal is to have the archive receive a value which I use to sort the individual posts. My overall goal is essentially fake 'categories' by assigning the custom posts a meta value and sorting using the value I pass through the url.
A more literal Example:
example.com/vegetables/
is the path to my custom post type archive. I want to be able to direct users to this path:
example.com/vegetables/potatoes
and in the template be able to pull "potatoes" from the query string.
Any ideas?

Resources