Git Product home page Git Product logo

Comments (4)

mikejolley avatar mikejolley commented on September 25, 2024 1

I don't think we necessarily need to duplicate functionality available in other WP APIs. Categories for example can be looked up via /wp-json/wp/v2/product_cat. From that you can derive IDs from slugs—perhaps you can utilise this in your use case?

Example response
[
  {
    "id": 19,
    "count": 5,
    "description": "",
    "link": "https://store.local/product-category/clothing/accessories/",
    "name": "Accessories",
    "slug": "accessories",
    "taxonomy": "product_cat",
    "parent": 16,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/19"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "up": [
        {
          "embeddable": true,
          "href": "https://store.local/wp-json/wp/v2/product_cat/16"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=19"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 16,
    "count": 14,
    "description": "",
    "link": "https://store.local/product-category/clothing/",
    "name": "Clothing",
    "slug": "clothing",
    "taxonomy": "product_cat",
    "parent": 0,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/16"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=16"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 21,
    "count": 1,
    "description": "",
    "link": "https://store.local/product-category/decor/",
    "name": "Decor",
    "slug": "decor",
    "taxonomy": "product_cat",
    "parent": 0,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/21"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=21"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 18,
    "count": 3,
    "description": "",
    "link": "https://store.local/product-category/clothing/hoodies/",
    "name": "Hoodies",
    "slug": "hoodies",
    "taxonomy": "product_cat",
    "parent": 16,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/18"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "up": [
        {
          "embeddable": true,
          "href": "https://store.local/wp-json/wp/v2/product_cat/16"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=18"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 20,
    "count": 0,
    "description": "",
    "link": "https://store.local/product-category/music/",
    "name": "Music",
    "slug": "music",
    "taxonomy": "product_cat",
    "parent": 0,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/20"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=20"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 17,
    "count": 5,
    "description": "",
    "link": "https://store.local/product-category/clothing/tshirts/",
    "name": "Tshirts",
    "slug": "tshirts",
    "taxonomy": "product_cat",
    "parent": 16,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/17"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "up": [
        {
          "embeddable": true,
          "href": "https://store.local/wp-json/wp/v2/product_cat/16"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=17"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 15,
    "count": 0,
    "description": "",
    "link": "https://store.local/product-category/uncategorized/",
    "name": "Uncategorized",
    "slug": "uncategorized",
    "taxonomy": "product_cat",
    "parent": 0,
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat/15"
        }
      ],
      "collection": [
        {
          "href": "https://store.local/wp-json/wp/v2/product_cat"
        }
      ],
      "about": [
        {
          "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat"
        }
      ],
      "wp:post_type": [
        {
          "href": "https://store.local/wp-json/wp/v2/product?product_cat=15"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  }
]

Reference: https://developer.wordpress.org/rest-api/reference/taxonomies/

from woocommerce.

355am avatar 355am commented on September 25, 2024

I agree, duplicating functionality isn’t ideal, but unfortunately, the endpoint mentioned (/wp-json/wp/v2/product_cat) isn’t publicly queryable like the others under /wp-json/wc/store, which is causing some issues for my use case. Any suggestions on how to work around this?

edit: also see #42321

from woocommerce.

mikejolley avatar mikejolley commented on September 25, 2024

@355am I think you're mistaken there. The read only post type APIs are public. https://woocommerce.com/wp-json/wp/v2/product_cat

from woocommerce.

355am avatar 355am commented on September 25, 2024

@mikejolley You are right! Interestingly, I tested the endpoint with several WooCommerce webshops and consistently received 404 "rest_no_route" responses. Thank you for providing the link as evidence.

While it works, it would be nice to directly query based on the slug instead of having to make two requests. Is there any chance of introducing slug-based querying in the future?

from woocommerce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.