Para crear un producto mediante API es necesario autenticarse. Puedes aprender cómo obtener las claves necesarias en nuestro tutorial sobre cómo obtener claves API.
Para crear un producto es necesario hacer una petición POST al siguiente endpoint:
https://ejemplo.com/wp-json/wc/v3/products
En el cuerpo de la petición pasamos todos los parámetros necesario para crear el producto. Este es un ejemplo con todos los parámetros que permite la creación de un producto por API:
{
"name": "Camiseta Pomatio",
"slug": "camiseta-pomatio",
"sku": "camiseta-pomatio",
"type": "simple",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "https://ejemplo.com/wp-content/uploads/2023/07/T_2_front-4.jpg"
},
{
"src": "https://ejemplo.com/wp-content/uploads/2023/07/T_2_back-4.jpg"
}
],
"lang" => "es_ES"
}
*Los idiomas admitidos son los por defecto de WordPress. Aquí la lista completa de idiomas permitidos.
Si el producto se crea correctamente, recibiremos un JSON como el siguiente:
{
"id": 1994,
"name": "Camiseta Pomatio",
"slug": "camiseta-pomatio",
"permalink": "https://ejemplo.com/producto/camiseta-pomatio/",
"date_created": "2023-07-18T17:01:14",
"date_created_gmt": "2023-07-18T20:01:14",
"date_modified": "2023-07-18T17:01:14",
"date_modified_gmt": "2023-07-18T20:01:14",
"type": "simple",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n",
"short_description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>\n",
"sku": "",
"price": "21.99",
"regular_price": "21.99",
"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": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">$</span>21.99</span>",
"on_sale": false,
"purchasable": true,
"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,
"stock_status": "instock",
"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.00",
"rating_count": 0,
"related_ids": [
53,
40,
56,
479,
99
],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 9,
"name": "Ropa",
"slug": "ropa"
},
{
"id": 14,
"name": "Camisetas",
"slug": "camisetas"
}
],
"tags": [],
"images": [
{
"id": 792,
"date_created": "2023-07-18T14:01:13",
"date_created_gmt": "2023-07-18T20:01:13",
"date_modified": "2023-07-18T14:01:13",
"date_modified_gmt": "2023-07-18T20:01:13",
"src": "https://ejemplo.com/wp-content/uploads/2023/07/T_2_front-4.jpg",
"name": "",
"alt": ""
},
{
"id": 793,
"date_created": "2023-07-18T14:01:14",
"date_created_gmt": "2023-07-18T20:01:14",
"date_modified": "2023-07-18T14:01:14",
"date_modified_gmt": "2023-07-18T20:01:14",
"src": "https://ejemplo.com/wp-content/uploads/2023/07/T_2_back-2.jpg",
"name": "",
"alt": ""
}
],
"attributes": [],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 0,
"meta_data": [],
"_links": {
"self": [
{
"href": "https://ejemplo.com/wp-json/wc/v3/products/1994"
}
],
"collection": [
{
"href": "https://ejemplo.com/wp-json/wc/v3/products"
}
]
}
}
Para crear productos en masa, hay que hacer una petición POST al siguiente endpoint:
https://ejemplo.com/wp-json/wc/v3/products/batch
Ejemplo de cómo crear dos o más productos a la vez utilizando la API:
{
"create": [
{
"name": "Producto 1",
"type": "simple",
"regular_price": "21.99",
"virtual": true,
"downloadable": true,
"downloads": [],
"categories": [
{
"id": 11
},
{
"id": 13
}
],
"images": [
{
"src": "https://ejemplo.com/wp-content/uploads/sites/56/2013/06/imagen-1.jpg"
}
]
},
{
"name": "Producto 2",
"type": "simple",
"regular_price": "35.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "https://ejemplo.com/wp-content/uploads/sites/56/2013/06/imagen-1.jpg"
},
{
"src": "https://ejemplo.com/wp-content/uploads/sites/56/2013/06/imagen-2.jpg"
}
]
}
],
}