Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce can't see product variations

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?

like image 671
Tizio Fittizio Avatar asked Nov 23 '18 16:11

Tizio Fittizio


1 Answers

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.

like image 89
Santhana Krishnan Avatar answered Sep 24 '22 01:09

Santhana Krishnan