WooCommerce can't see product variations
Asked Answered
M

2

7

I'm trying to manage WooCommerce entirely by its REST API but with no luck, i'm trying to insert a product with variations

  • I can succesfully create a product with a POST to {{url}}/wp-json/wc/v3/products/
  • After product creation, i have to create its variations with a POST to a separate endpoint to {{url}}/wp-json/wc/v3/products/{{product_id}}/variations, this works too
  • I can see the product variation created with a GET in {{url}}/wp-json/wc/v3/products/{{product_id}}/variations and its own GET {{url}}/wp-json/wc/v3/products/{{product_id}}/variations/{{variation_id}}
  • I can correctly see the rows created in the wordpress database
  • All calls are a 201 with the expected object as response

However, i'm not able to see any product variations on the woocommerce product page

What i found its that a product with variations that work have an array variations which contains the variations ids, but when i create a product with API the array of the product created is empty, this would explain why i'm not able to see any product variation on the product page.

This is a sample of product variation that I create:

{
"regular_price": "225",
"status": "publish",
"manage_stock": true,
"stock_quantity": 1,
"stock_status": "instock",
"image": {
  "src": "https://via.placeholder.com/150"
},
"on_sale": true,
"shipping_class": "1",
"attributes": [
  {
    "id": 2,
    "name": "Color",
    "option": "Red"
  },
  {
    "id": 3,
    "name": "Size",
    "option": "Xl"
  }
]}

I was not able to found a similar issue, any thoughts?

Mcwhorter answered 23/11, 2018 at 16:21 Comment(0)
V
3

The problem could be in the product creation process. I can't know for sure without looking at the data used for product creation, but I'll try anyway. I see that you use two different attributes for the variants. So, the products should be created to support those attributes correctly:

{
    "name": "Sample Product",
    "type": "Example",
    "description": "A Demo Product",
    "images": {
        {
            "src": "path/to/img",
            "position": 1
        }
    },
    "categories": {
        {
            "id": 12
        }
    },
    "attributes": {
        {
            "id": 2,
            "name": "Color",
            "variation": true,
            "visible": true,
            "options": [ 'Red', 'Green', 'Blue' ]
        },
        {
            "id": 3,
            "name": "Size",
            "variation": true,
            "visible": true,
            "options": [ 'M', 'L', 'XL' ]
        }
    }
}

If "variation": true is missing in the main product's attributes, then the variants created under attributes without that flag won't show up as variants. I know it's a shot in the dark, but maybe it'll help you.

Vladamar answered 21/7, 2020 at 13:6 Comment(2)
Thank you for your help but I had this problem almost two years ago, in the meantime we just threw away this woocommerce crap and rewritten everything from scratch, I hope this will help someone with the same problemMcwhorter
2024 - no 'variation' field in the product properties. What now? :))Hildick
H
0

In my case, it was the type of the parent product, which should be

"type": "variable",
Hildick answered 28/3 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.