Back to Woocommerce

Product attributes

docs/apis/rest-api/v3/product-attributes.mdx

10.9.0-dev13.8 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Product attributes

The product attributes API allows you to create, view, update, and delete individual, or a batch, of product attributes.

Product attribute properties

AttributeTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
namestringAttribute name. MANDATORY
slugstringAn alphanumeric identifier for the resource unique to its type.
typestringType of attribute. By default only select is supported.
order_bystringDefault sort order. Options: menu_order, name, name_num and id. Default is menu_order.
has_archivesbooleanEnable/Disable attribute archives. Default is false.

Create a product attribute

This API helps you to create a new product attribute.

http
POST /wp-json/wc/v3/products/attributes
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X POST https://example.com/wp-json/wc/v3/products/attributes \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "name": "Color",
  "slug": "pa_color",
  "type": "select",
  "order_by": "menu_order",
  "has_archives": true
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
	name: 'Color',
	slug: 'pa_color',
	type: 'select',
	order_by: 'menu_order',
	has_archives: true,
};

WooCommerce.post( 'products/attributes', data )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php
$data = [
    'name' => 'Color',
    'slug' => 'pa_color',
    'type' => 'select',
    'order_by' => 'menu_order',
    'has_archives' => true
];

print_r($woocommerce->post('products/attributes', $data));
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "name": "Color",
    "slug": "pa_color",
    "type": "select",
    "order_by": "menu_order",
    "has_archives": True
}

print(wcapi.post("products/attributes", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  name: "Color",
  slug: "pa_color",
  type: "select",
  order_by: "menu_order",
  has_archives: true
}

woocommerce.post("products/attributes", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
	"id": 1,
	"name": "Color",
	"slug": "pa_color",
	"type": "select",
	"order_by": "menu_order",
	"has_archives": true,
	"_links": {
		"self": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
			}
		],
		"collection": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes"
			}
		]
	}
}
</TabItem> </Tabs>

Retrieve a product attribute

This API lets you retrieve and view a specific product attribute by ID.

http
GET /wp-json/wc/v3/products/attributes/<id>
<Tabs> <TabItem value="curl" label="cURL">
shell
curl https://example.com/wp-json/wc/v3/products/attributes/1 \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.get( 'products/attributes/1' )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->get('products/attributes/1')); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.get("products/attributes/1").json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.get("products/attributes/1").parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
	"id": 1,
	"name": "Color",
	"slug": "pa_color",
	"type": "select",
	"order_by": "menu_order",
	"has_archives": true,
	"_links": {
		"self": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
			}
		],
		"collection": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes"
			}
		]
	}
}
</TabItem> </Tabs>

List all product attributes

This API helps you to view all the product attributes.

http
GET /wp-json/wc/v3/products/attributes
<Tabs> <TabItem value="curl" label="cURL">
shell
curl https://example.com/wp-json/wc/v3/products/attributes \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.get( 'products/attributes' )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->get('products/attributes')); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.get("products/attributes").json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.get("products/attributes").parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
[
	{
		"id": 1,
		"name": "Color",
		"slug": "pa_color",
		"type": "select",
		"order_by": "menu_order",
		"has_archives": true,
		"_links": {
			"self": [
				{
					"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
				}
			],
			"collection": [
				{
					"href": "https://example.com/wp-json/wc/v3/products/attributes"
				}
			]
		}
	},
	{
		"id": 2,
		"name": "Size",
		"slug": "pa_size",
		"type": "select",
		"order_by": "menu_order",
		"has_archives": false,
		"_links": {
			"self": [
				{
					"href": "https://example.com/wp-json/wc/v3/products/attributes/2"
				}
			],
			"collection": [
				{
					"href": "https://example.com/wp-json/wc/v3/products/attributes"
				}
			]
		}
	}
]
</TabItem> </Tabs>

Available parameters

ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.

Update a product attribute

This API lets you make changes to a product attribute.

http
PUT /wp-json/wc/v3/products/attributes/<id>
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X PUT https://example.com/wp-json/wc/v3/products/attributes/1 \
	-u consumer_key:consumer_secret \
	-H "Content-Type: application/json" \
	-d '{
  "order_by": "name"
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
	order_by: 'name',
};

WooCommerce.put( 'products/attributes/1', data )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php
$data = [
    'order_by' => 'name'
];

print_r($woocommerce->put('products/attributes/1', $data));
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "order_by": "name"
}

print(wcapi.put("products/attributes/1", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  order_by: "name"
}

woocommerce.put("products/attributes/1", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
	"id": 1,
	"name": "Color",
	"slug": "pa_color",
	"type": "select",
	"order_by": "name",
	"has_archives": true,
	"_links": {
		"self": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
			}
		],
		"collection": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes"
			}
		]
	}
}
</TabItem> </Tabs>

Delete a product attribute

This API helps you delete a product attribute.

http
DELETE /wp-json/wc/v3/products/attributes/<id>

:::warning This also will delete all terms from the selected attribute. :::

<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X DELETE https://example.com/wp-json/wc/v3/products/attributes/1?force=true \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.delete( 'products/attributes/1', {
	force: true,
} )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->delete('products/attributes/1', ['force' => true])); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.delete("products/attributes/1", params={"force": True}).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.delete("products/attributes/1", force: true).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
	"id": 1,
	"name": "Color",
	"slug": "pa_color",
	"type": "select",
	"order_by": "menu_order",
	"has_archives": true,
	"_links": {
		"self": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
			}
		],
		"collection": [
			{
				"href": "https://example.com/wp-json/wc/v3/products/attributes"
			}
		]
	}
}
</TabItem> </Tabs>

Available parameters

ParameterTypeDescription
forcestringRequired to be true, as resource does not support trashing.

Batch update product attributes

This API helps you to batch create, update and delete multiple product attributes.

:::note Note: By default it's limited to up to 100 objects to be created, updated or deleted. :::

http
POST /wp-json/wc/v3/products/attributes/batch
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X POST https://example.com/wp-json/wc/v3/products/attributes/batch \
	-u consumer_key:consumer_secret \
	-H "Content-Type: application/json" \
	-d '{
  "create": [
    {
      "name": "Brand"
    },
    {
      "name": "Publisher"
    }
  ],
  "update": [
    {
      "id": 2,
      "order_by": "name"
    }
  ],
  "delete": [
    1
  ]
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
	create: [
		{
			name: 'Brand',
		},
		{
			name: 'Publisher',
		},
	],
	update: [
		{
			id: 2,
			order_by: 'name',
		},
	],
	delete: [ 1 ],
};

WooCommerce.post( 'products/attributes/batch', data )
	.then( ( response ) => {
		console.log( response.data );
	} )
	.catch( ( error ) => {
		console.log( error.response.data );
	} );
</TabItem> <TabItem value="php" label="PHP">
php
<?php
$data = [
    'create' => [
        [
            'name' => 'Brand'
        ],
        [
            'name' => 'Publisher'
        ]
    ],
    'update' => [
        [
            'id' => 2,
            'order_by' => 'name'
        ]
    ],
    'delete' => [
        1
    ]
];

print_r($woocommerce->post('products/attributes/batch', $data));
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "create": [
        {
            "name": "Brand"
        },
        {
            "name": "Publisher"
        }
    ],
    "update": [
        {
            "id": 2,
            "order_by": "name"
        }
    ],
    "delete": [
        1
    ]
}

print(wcapi.post("products/attributes/batch", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  create: [
    {
      name: "Round toe"
    },
    {
      name: "Flat"
    }
  ],
  update: [
    {
      id: 2,
      order_by: "name"
    }
  ],
  delete: [
    1
  ]
}

woocommerce.post("products/attributes/batch", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
	"create": [
		{
			"id": 7,
			"name": "Brand",
			"slug": "pa_brand",
			"type": "select",
			"order_by": "menu_order",
			"has_archives": false,
			"_links": {
				"self": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes/7"
					}
				],
				"collection": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes"
					}
				]
			}
		},
		{
			"id": 8,
			"name": "Publisher",
			"slug": "pa_publisher",
			"type": "select",
			"order_by": "menu_order",
			"has_archives": false,
			"_links": {
				"self": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes/8"
					}
				],
				"collection": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes"
					}
				]
			}
		}
	],
	"update": [
		{
			"id": 2,
			"name": "Size",
			"slug": "pa_size",
			"type": "select",
			"order_by": "menu_order",
			"has_archives": false,
			"_links": {
				"self": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes/2"
					}
				],
				"collection": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes"
					}
				]
			}
		}
	],
	"delete": [
		{
			"id": 1,
			"name": "Color",
			"slug": "pa_color",
			"type": "select",
			"order_by": "menu_order",
			"has_archives": true,
			"_links": {
				"self": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
					}
				],
				"collection": [
					{
						"href": "https://example.com/wp-json/wc/v3/products/attributes"
					}
				]
			}
		}
	]
}
</TabItem> </Tabs>