Headless Storefront Setup
Introduction
Get up and running with Price Rules API and start building your headless e-commerce store! Follow these instructions to set up your store's storefront to read the price rules you have created for your products. There are 2 public endpoints available in Price Rules:
These are public endpoints and do not require an OAuth Bearer token in the request.
Prerequisites
Before you get started, follow Getting Started to get connected to our Bold Platform.
Bold Checkout is required to use the Headless Price Rules API.
Get best price for product
Method: POST
This endpoint can be used for product pages and product collection pages. The request body for this endpoint is rather straightforward where it accepts 1 or more "items" (or products) and the best price for those items are returned from Price Rules, if a price rule that matches is found.
Request Body
{
"items": [
{
"id": "1234-001",
"price": 1099
},
{
"id": "1001-003",
"price": 2099
}
],
"lineItemIdType": "sku",
"customer": {
"tags": "gold"
}
}
Properties: [* denotes required elements]
lineItemIdType
* - the property of theitem
id
and currently should be set to skuitems
* - a list of 1 or more products from your storefront page that you are getting price rules forid
* - the SKU identified for this productprice
* - the base or original price for the product. If a price rule cannot be found for theid
this price will be returned in the response
customer
- customer information if availabletags
- customer tags
Response Body
{
"pre_best_price_log_id": "string",
"actions": [
{
"type": "BEST_ITEM_PRICE",
"sku": "sku_get",
"variant_id": 39702426812606,
"value": 25000,
"rule": [
{
"id": 1593624,
"eid": null,
"rule_set_id": 101480,
"rule_set_eid": "bicycles",
"rule_set_public_message": null,
"type": "DISCOUNT"
}
]
},
{
"type": "BEST_ITEM_PRICE",
"sku": "1001-003",
"value": 2099
}
]
}
Properties
pre_best_price_log_id
- PRE-generated ID for logging and debugging purposesactions
- the pricing actions for the requesteditems
type
- static value currently set toBEST_ITEM_PRICE
sku
- the SKU identifier of the requesteditem
and matches theitem
id
in the requestvariant_id
- identifier of the product variant, if availablevalue
- the discounted price (if a price rule is found for thesku
) or the original price of theitem
from the request (if a price rule is not found)rule
- the matching price rule for theitem
Code Samples
You will be required to add some code to your storefront theme to call the Get Best Price for Product endpoint and to translate the request and response from PRE. Here are some examples for PHP, vue.js and Ruby:
PHP curl
function callAPI($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result) { die("Connection Failure"); }
curl_close($curl);
return $result;
}
$data_array = array(
"lineItemIdType" => 'sku_id',
"items" => array(
"id" => $this->request->data['sku'],
"price" => $this->request->data['price']
),
);
$make_call = callAPI('POST', 'https://api.boldcommerce.com/price_rules/storefront/v2/shops/{shop_identifier}/price', json_encode($data_array));
$response = json_decode($make_call, true);
$errors = $response['response']['errors'];
$data = $response['response']['data'][0];
vue.js (using Axios)
product_load() {
// Simple POST request with a JSON body using axios
const product = { "items": [ { "id": "1234-001", "price": 1099 } ], "lineItemIdType": "product_id" }
axios.post("https://api.boldcommerce.com/price_rules/storefront/v2/shops/{shop_identifier}/price", product)
.then(response => this.productPrice = response.data.actions[0].value);
}
Ruby
require 'uri'
require 'net/http'
url = URI("https://api.boldcommerce.com/price_rules/storefront/v2/shops/:shop_identifier/price")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request.body = {lineItemIdType: 'product_id', items: [{ id: "1234-001", price: 1099 }]}.to_json
response = http.request(request)
puts response.read_body
Get best price for cart
Method: POST
This endpoint can be used for cart pages.
The request body for this endpoint accepts quantity and the original price from your cart items. The quantity will be used to evaluate rules with quantity conditions, for example:
"rules": [
{
"type": "DISCOUNT",
"conditions": [
{
"type": "CUSTOMER_GROUP",
"value": "bronze"
},
{
"type": "QTY_BY_PRODUCT",
"operator": ">=",
"value": 2
}
],
"actions": [
{
"type": "PRICE_ADJUST_PERCENT",
"value": -20
}
]
}, ... ]
And if a price rule condition match is found for the cart line item PRE will return the best price for that cart line item.
The price returned is the unit price and not the calculated price x quantity
subtotal.
Request Body
{
"lineItems": [
{
"key": "uniquekey1",
"price": 1099,
"id": "1234-001",
"quantity": 2
},
{
"key": "uniquekey2",
"price": 2099,
"id": "3329-003",
"quantity": 1
}
],
"lineItemIdType": "sku",
"customer": {
"tags": "gold"
}
}
Properties: [* denotes required elements]
lineItemIdType
* - the property of thelineItem
id
and currently should be set to skulineItems
* - a list of 1 or more products from your cart page that you are getting price rules forkey
* - a unique identifier key for the cart lineitem for this request and can be anything, for example a UUID, and is used for logging and debugging purposesprice
* - the base or original price for the product. If a price rule cannot be found for theid
this price will be returned in the responseid
* - the SKU identifier for thelineItem
productquantity
* - thelineItem
quantity from the cart
customer
- customer information if availabletags
- customer tags
Response Body
{
"pre_checkout_id": "POS_123",
"actions": [
{
"type": "SET_LINE_ITEM_PRICE",
"line_item_key": "uniquekey1",
"value": 550,
"sku": "1234-001",
"variant_id": 19702426812602,
"rule": {
"id": 1593607,
"eid": "red_rollerblades",
"rule_set_id": 101480,
"rule_set_eid": "bicycles",
"rule_set_public_message": null,
"type": "DISCOUNT"
}
},
{
"type": "SET_LINE_ITEM_PRICE",
"line_item_key": "uniquekey2",
"value": 1050,
"sku": "3329-003",
"variant_id": 39702426812606,
"rule": {
"id": 4593607,
"eid": "blue_rollerblades",
"rule_set_id": 601480,
"rule_set_eid": "rollerblades",
"rule_set_public_message": "Rollerblade discounts",
"type": "DISCOUNT"
}
}
]
}
Properties:
pre_checkout_id
- PRE-generated checkout ID for logging and debugging purposesactions
- the pricing actions for the requested cartlineItems
key
- the unique identifier key for the cartlineItem
from the requestprice
- the discounted price (if price rule found) or the original price from the request (if price rule is not found)id
- the SKU identifier based on thelineItemIdType
from the cartlineItem
quantity
-lineItem
cart quantity from the cartrule
- the matching price rule for the product
Code Samples:
You will be required to add some code to your storefront theme to call the Get Best Price for Cart endpoint and to translate the request and response from PRE. The code will be similar to the above samples provided for Get best price for product with the difference being the endpoint URL and the structure of the request.
Congratulations! You are now setup with Price Rules!