Using the REST API

Note #

  • This documentation can also be found in the plugin settings at: Products > Batch Numbers > REST.
  • WooCommerce REST API must be active (see below).

Activate WooCommerce REST API #

Path: WooCommerce > Settings > Advanced > REST API.
Our Batch Numbers plugin uses the WooCommerce REST API in order to provide REST API functionality.

Samples #

Compatible softwareFile
Insomnia, Postmanrest-collection-v1.3.json

Get all batches #

GET

http://localhost:10003/wp-json/wc/v3/products/batches

JSON response example:

{
  "23": {
	"id": "23",
	"date_created": "2024-02-01 09:55:08",
	"date_expiry": "2024-10-01 09:55:08",
	"user_id": "1",
	"batch_number": "c40b0dc24d",
	"supplier": "WP Overnight",
	"location": "Netherlands",
	"quantity": "20.0000",
	"quantity_available": "18.0000",
	"units": "1",
	"status": "active",
	"order_ids": "[1037,1038]",
	"shared_ids": "[275]"
  },
}

Insert a new batch through a product #

PUT

http://localhost:10003/wp-json/wc/v3/products/<product_id>

Body form-data

ParameterTypeDescription
batchesarrayAn array of batch objects.
batches[].batch_numberstringA unique identifier for the batch.
batches[].date_expirystring (datetime)The expiration date of the batch, formatted as “YYYY-MM-DD HH:MM:SS”.
batches[].quantityintegerThe total quantity of items in the batch.
batches[].quantity_availableintegerThe number of available items in the batch.
batches[].locationstringThe location of items in the batch.
batches[].statusstringThe status of the batch. Valid values are active or archived. The default value is active.
batches[].user_idintegerThe unique identifier for the user. This parameter is used to associate the batch with a specific user. The default value is 1.
batches[].poobjectThe purchase order details.
batches[].po.date_createdstring (datetime)Date the purchase order was created in “YYYY-MM-DD HH:MM:SS” format.
batches[].po.po_numberstringThe purchase order number.
batches[].po.supplierstringThe name of the supplier.
batches[].po.costfloatThe cost price of the item.
batches[].po.regular_pricefloatThe regular selling price of the item.
batches[].po.sale_pricefloatThe sale price of the item, if applicable.
batches[].po.price_marginfloatThe profit margin on the item.
batches[].po.price_discountfloatThe discount amount applied to the item.

The id should be omitted for batch objects because it will be created automatically.

JSON response example:

{
  "batches": [
	{
	  "batch_number": "EOEOEOEOEOEOE",
	  "date_expiry": "2023-12-31 00:00:00",
	  "quantity": "50",
	  "po":
		{
		  "date_created": "2023-01-16 00:00:00",
		  "po_number": "PO23456",
		  "supplier": "WPO",
		  "cost": 10.99,
		  "regular_price": 14.99,
		  "sale_price": 13.99,
		  "price_margin": 30.00,
		  "price_discount": 5.00
		}
	}
  ]
}

Update a batch through a product #

PUT

http://localhost:10003/wp-json/wc/v3/products/<product_id>

Body form-data

ParameterTypeDescription
batchesarrayAn array of batch objects.
batches[].idintegerRequired The unique identifier for the batch.
batches[].batch_numberstringA unique identifier for the batch.
batches[].date_expirystring (datetime)The expiration date of the batch, formatted as “YYYY-MM-DD HH:MM:SS”.
batches[].quantityintegerThe total quantity of items in the batch.
batches[].quantity_availableintegerThe number of available items in the batch.
batches[].locationstringThe location of items in the batch.
batches[].statusstringThe status of the batch. Valid values are active or archived. The default value is active.
batches[].user_idintegerThe unique identifier for the user. This parameter is used to associate the batch with a specific user. The default value is 1.
batches[].poobjectThe purchase order details.
batches[].po.date_createdstring (datetime)Date the purchase order was created in “YYYY-MM-DD HH:MM:SS” format.
batches[].po.po_numberstringThe purchase order number.
batches[].po.supplierstringThe name of the supplier.
batches[].po.costfloatThe cost price of the item.
batches[].po.regular_pricefloatThe regular selling price of the item.
batches[].po.sale_pricefloatThe sale price of the item, if applicable.
batches[].po.price_marginfloatThe profit margin on the item.
batches[].po.price_discountfloatThe discount amount applied to the item.

JSON response example:

{
  "batches": [
	{
	  "id": "53",
	  "quantity_available": "20",
	  "po":
		{
		  "date_created": "2023-01-16 00:00:00",
		  "po_number": "PO23456",
		  "supplier": "WPO",
		  "cost": 10.99,
		  "regular_price": 14.99,
		  "sale_price": 13.99,
		  "price_margin": 30.00,
		  "price_discount": 5.00
		}
	}
  ]
}

Update a batch directly #

PUT

http://localhost:10003/wp-json/wc/v3/products/batches/<batch_id>

Body form-data

ParameterTypeDescription
batch_numberstringA unique identifier for the batch.
date_expirystring (datetime)The expiration date of the batch, formatted as “YYYY-MM-DD HH:MM:SS”.
quantityintegerThe total quantity of items in the batch.
quantity_availableintegerThe number of available items in the batch.
locationstringThe location of items in the batch.
statusstringThe status of the batch. Valid values are active or archived. The default value is active.
user_idintegerThe unique identifier for the user. This parameter is used to associate the batch with a specific user. The default value is 1.
poobjectThe purchase order details.
po.date_createdstring (datetime)Date the purchase order was created in “YYYY-MM-DD HH:MM:SS” format.
po.po_numberstringThe purchase order number.
po.supplierstringThe name of the supplier.
po.costfloatThe cost price of the item.
po.regular_pricefloatThe regular selling price of the item.
po.sale_pricefloatThe sale price of the item, if applicable.
po.price_marginfloatThe profit margin on the item.
po.price_discountfloatThe discount amount applied to the item.

JSON response example:

{
  "quantity_available": "20",
  "po":
	{
	  "date_created": "2023-01-16 00:00:00",
	  "po_number": "PO23456",
	  "supplier": "WPO",
	  "cost": 10.99,
	  "regular_price": 14.99,
	  "sale_price": 13.99,
	  "price_margin": 30.00,
	  "price_discount": 5.00
	}
}

Delete a batch through a product #

PUT

http://localhost:10003/wp-json/wc/v3/products/<product_id>

Body form-data

ParameterTypeDescription
batchesarrayAn array of batch objects.
batches[].idintegerRequired The unique identifier for the batch.
batches[].deletebooleanIndicates whether the batch should be deleted. When set to true, the batch is marked for deletion. Only one of delete or force_delete should be sent in the request.
batches[].force_deletebooleanIndicates whether the batch should be forcefully deleted. By using this setting, you can delete batches without needing to detach the associated products first. Additionally, this will also remove the corresponding Purchase Order linked to the batch. Only one of delete or force_delete should be sent in the request.

JSON response example:

{
  "batches": [
	{
	  "id": "53",
	  "delete": "yes"
	}
  ]
}

Delete a batch purchase order through a product #

PUT

http://localhost:10003/wp-json/wc/v3/products/<product_id>

Body form-data

ParameterTypeDescription
batchesarrayAn array of batch objects.
batches[].idintegerRequired The unique identifier for the batch.
batches[].poobjectThe purchase order details associated with the batch.
batches[].po.deletebooleanIndicates whether the associated purchase order should be deleted. When set to true, the purchase order linked to the batch will be deleted.

JSON response example:

{
  "batches": [
	{
	  "id": "53",
	  "po":
		{
		  "delete": "yes"
		}
	}
  ]
}