Support

Account

Home Forums General Issues Querying Specific WooCommerce Products via ACF Fields via REST API?

Unread

Querying Specific WooCommerce Products via ACF Fields via REST API?

  • Hello,

    I’m curious if there’s a way to use the built in REST API to query a WooCommerce product based on it’s ACF custom fields, and (hopefully) only respond with those custom fields.

    Either the WC API, WC Legacy API, or the WP API is fine in this case.

    The reason I mention that is because if I hit: /wp-json/wc/v3/products it doesn’t display the custom fields, but if I hit /wp-json/wp/v2/product it does, although it excludes some information in the response that’s specific to WooCommerce, but hopefully that won’t matter because I can pass in _fields=acf with the WordPress route.

    I can also hit the WC legacy API like /wc-api/v3/products/?filter[meta]=true to return a product_meta object that contains the acf fields but can’t structure a query based on what’s in there.

    Basically, I have custom fields set up so that if I hit /wp-json/wp/v2/product?_fields=acf, I receive a response object that looks like:

    
    acf: {
      id: "367fh9378ahr89h2r",
      name: "Chair (Single Leg)"
      entity_type: "Furniture",
      category: "Chairs",
      price: 543.21,
      manufacturer: "Home Depot",
    },
    acf: {
      id: "234898172983712dj",
      name: "Dishwasher (Steel)"
      entity_type: "Appliance",
      category: "Dishwasher",
      price: 123.45,
      manufacturer: "Wayfair",
    }
    

    So my question is, is there someway that I can make a GET request to any of these APIs so that I can structure the query with parameters or filters to retrieve products, as well as retrieve a specific product, using a queries like this:

    All products marked as appliances:
    /wp-json/wp/v2/product/?_fields=acf&entity_type=Appliance

    A specific product:
    /wp-json/wp-v2/product/?_fields=acf&id=2hi3kuah82h2

    All products marked as either furniture or appliances
    /wp-json/wp/v2/product/?_fields=acf&filter[entity_type]=Appliance,Furniture
    or (since I know filters were deprecated, but you can use the rest filter plugin to bring em back)
    /wp-json/wp/v2/product/?_fields=acf&entity_type=Appliance,Furniture
    doesn’t matter which way obviously.

    All products made by Wayfair marked appliances:
    /wp-json/wp/v2/product/?_fields=acf&entity_type=Appliance&manufacturer=Wayfair

    I think that’s enough for you all to get the point. I haven’t even found a way to filter by the ACF fields specifically, let alone only return the acf object. I’ve seen a few posts about adding some functions or extending the API, but after experimenting with it (for someone like myself who is mostly a Javascript developer and doesn’t deal with too much PHP), I can’t seem to get a solution to work that fits another app developer’s specifications.

    Let me add that querying based on any type of WordPress/WooCommerce id is not possible in this circumstance.

    I have tried using the REST filter plugin. I’ve tried using the ACF to REST API plugin. I’ve tried using the WooCommerce legacy API to with ?filter[meta]=true, but can’t filter through the returned product_meta object.

    Any input on this or a point in the right direction would be much appreciated.

    Thanks,
    Nathan.

    Just after typing out this post I found a lovely and very small plugin that appears to be just two functions called ACF-REST-Query (by sts24 here: https://github.com/sts24/ACF-REST-Query) which is almost exactly what I’m looking to do, but it doesn’t have support for multiple arguments, or querying multiple acf parameters, so making requests like these don’t work (and it just accepts the last parameter):
    ?_fields=acf&acf_entity_type=Furniture,Appliance
    or
    ?_fields=acf&acf_category=Dishwasher&acf_manufacturer=Wayfair

    So yeah, almost there.

    Again, if anyone could help me out, it’d be much appreciated.

    Thanks,
    Nathan.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.