• Resolved klaushauschild

    (@klaushauschild)


    Hi,

    I try to import variable products autmatically via the REST API of WooCommerce (3.2.1).
    In my system is one attribute stored with ID 1:

    [
    	{
    		"id": 1,
    		"name": "Entwickelt oder Digital",
    		"slug": "pa_2xju1krpnrjvxux2odlox1fb",
    		"type": "select",
    		"order_by": "menu_order",
    		"has_archives": false,
    		"_links": {
    			"self": [{
    					"href": "...\/wp-json\/wc\/v2\/products\/attributes\/1"
    				}
    			],
    			"collection": [{
    					"href": "...\/wp-json\/wc\/v2\/products\/attributes"
    				}
    			]
    		}
    	}
    ]

    Then I try to create a variable product via POST https://…/wp-json/wc/v2/products with

    {
    	"name": "IMG_5993",
    	"attributes": [{
    			"id": 1,
    			"variation": true
    		}
    	],
    	"categories": [{
    			"id": 67
    		}
    	],
    	"type": "variable",
    	"slug": "lcc5lwpteuabkpm10frauldo"
    }

    As a result the product appears in the shop but misses the requested attribute. The response for my requests looks like

    {
    	"id": 271,
    	"name": "IMG_5993",
    	"slug": "lcc5lwpteuabkpm10frauldo",
    	"permalink": ".../index.php/produkt/lcc5lwpteuabkpm10frauldo/",
    	"date_created": "2017-10-30T13:43:58",
    	"date_created_gmt": "2017-10-30T12:43:58",
    	"date_modified": "2017-10-30T13:43:58",
    	"date_modified_gmt": "2017-10-30T12:43:58",
    	"type": "variable",
    	"status": "publish",
    	"featured": false,
    	"catalog_visibility": "visible",
    	"description": "",
    	"short_description": "",
    	"sku": "",
    	"price": "",
    	"regular_price": "",
    	"sale_price": "",
    	"date_on_sale_from": null,
    	"date_on_sale_from_gmt": null,
    	"date_on_sale_to": null,
    	"date_on_sale_to_gmt": null,
    	"price_html": "",
    	"on_sale": false,
    	"purchasable": false,
    	"total_sales": 0,
    	"virtual": false,
    	"downloadable": false,
    	"downloads": [],
    	"download_limit": -1,
    	"download_expiry": -1,
    	"external_url": "",
    	"button_text": "",
    	"tax_status": "taxable",
    	"tax_class": "",
    	"manage_stock": false,
    	"stock_quantity": null,
    	"in_stock": false,
    	"backorders": "no",
    	"backorders_allowed": false,
    	"backordered": false,
    	"sold_individually": false,
    	"weight": "",
    	"dimensions": {
    		"length": "",
    		"width": "",
    		"height": ""
    	},
    	"shipping_required": true,
    	"shipping_taxable": true,
    	"shipping_class": "",
    	"shipping_class_id": 0,
    	"reviews_allowed": true,
    	"average_rating": "0",
    	"rating_count": 0,
    	"related_ids": [],
    	"upsell_ids": [],
    	"cross_sell_ids": [],
    	"parent_id": 0,
    	"purchase_note": "",
    	"categories": [{
    			"id": 67,
    			"name": "Shooting vom 30.10.2017",
    			"slug": "78exy65zicfue0u9qifdthjq"
    		}
    	],
    	"tags": [],
    	"images": [{
    			"id": 0,
    			"date_created": "2017-10-30T14:44:08",
    			"date_created_gmt": "2017-10-30T12:44:08",
    			"date_modified": "2017-10-30T14:44:08",
    			"date_modified_gmt": "2017-10-30T12:44:08",
    			"src": ".../wp-content/plugins/woocommerce/assets/images/placeholder.png",
    			"name": "Platzhalter",
    			"alt": "Platzhalter",
    			"position": 0
    		}
    	],
    	"attributes": [],
    	"default_attributes": [],
    	"variations": [],
    	"grouped_products": [],
    	"menu_order": 0,
    	"meta_data": [{
    			"id": 3294,
    			"key": "_wpas_done_all",
    			"value": "1"
    		}
    	],
    	"_links": {
    		"self": [{
    				"href": ".../index.php/wp-json/wc/v2/products/271"
    			}
    		],
    		"collection": [{
    				"href": ".../index.php/wp-json/wc/v2/products"
    			}
    		]
    	}
    }

    Here you can see the attributes array is empty.

    Where is the problem? Am I missing required parameters or preconditions? Or is there a problem in the API for variable products?

    Please help.
    Best regards, Klaus Hauschild

Viewing 5 replies - 1 through 5 (of 5 total)
  • It is very much possible that the problem comes because you did not provide the attribute options.

    Here is how the docs define it:

    "attributes": [
        {
          "id": 6,
          "position": 0,
          "visible": false,
          "variation": true,
          "options": [
            "Black",
            "Green"
          ]
        },
        {
          "name": "Size",
          "position": 0,
          "visible": true,
          "variation": true,
          "options": [
            "S",
            "M"
          ]
        }
      ],
    Thread Starter klaushauschild

    (@klaushauschild)

    Thanks for the advise but it is not very understandable because I defined already the referenced attribute via the API. If it is so than the API has at this point a strange design.
    I give it a try and post my results.

    You did define an attribute, but not its options. It is like telling WC, “I have t-shirts in attribute “color”, please create my variations”. The logical question is, what color? Even if you go to the admin, create a variable product then add attribute “color” without options, this attribute will not be saved.

    Thread Starter klaushauschild

    (@klaushauschild)

    Thanks a lot. Now it seems to work. There are other problems, but the attribute association is fine.
    I’m confused because the mentioned attribute has options. The options arn’t visible in the JSON but in the frontend.

    Thanks for your help.

    We haven’t heard back from you in a while, so I’m going to mark this as resolved. If you have any further questions, you can start a new thread.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[REST API] variable product created without given attributes’ is closed to new replies.