Overview

You can use the Price service to manage product-specific pricing. The Price service enables you to:

  • Create, update, and remove a price for a specific product.
  • Create, update, retrieve, and delete a custom attribute for a specific price ID.
  • Retrieve the price of a product for a given price ID.
  • Retrieve all prices for all products of a given tenant.
  • Retrieve all prices that meet certain criteria, such as product ID, currency, or effective date.
  • Retrieve a list of prices that match a specific currency.
  • Delete all prices assigned to a specific tenant.

This service is separate from the Product service. All details established here are for the price only, regardless of whether it is product-specific or tenant-specific. Before you can assign a price to a product, the product must be created. When the product is created, the Product service generates the product ID that is required by the Price service.

There are two user roles (hybris.price_manage and hybris.price_delete_all) for the Price service. The hybris.price.manage role is equivalent to an administrative role and is necessary to make all requests (except for the GET requests). This role is associated with the access token that you have created. The hybris.price_delete_all role is only needed if you want to delete all the product prices for a tenant in a single request.


API Reference

/{tenant}/prices

This resourceType defines the GET, POST, DELETE methods and their responses for a collection resource.

/{tenant}/prices

get

Get all prices OR Get all prices by product ID(s) for a specific tenant

post

Create new price for a product. The caller must have the hybris.price_manage scope assigned.

delete

Delete all the prices of the tenant. The caller must have the hybris.price_delete_all scope assigned.

/{tenant}/prices/matchCurrency

post

Match a list of pricerows in one specific currency with the correspondent ones in target currency.

/{tenant}/prices/{priceId}

This resource type defines the GET, PUT, DELETE methods and their responses for a single element resource.

get

Get a specific price by price ID

put

Modify an existing price by its price ID for a given product. The caller must have the hybris.price_manage scope assigned.

delete

Delete a specific price by price ID. The caller must have the hybris.price_manage scope assigned.

/{tenant}/prices/{priceId}/customattributes

post

Add a new custom attribute to a given price. The caller must have the hybris.price_manage scope assigned.

/{tenant}/prices/{priceId}/customattributes/{attributeKey}

This resource type defines the GET, PUT, DELETE methods and their responses for a single element resource.

get

Get a custom attribute by key.

put

Update a custom attribute by key. The caller must have the hybris.price_manage scope assigned.

delete

Delete a custom attribute by key. The caller must have the hybris.price_manage scope assigned.


Events

For more information about events, see the PubSub service documentation.

The topic owner client is: hybris.price

Event TypeDescriptionPayloadExample
price-changeTriggered when there is a change in the price of a product.schema
{"clientId" : "hybris.clientforprice",  "originalAmount" : 9.999999999E7,  "itemYrn" : "urn:yaas:hybris:product:product-variant:myshop;P1;V1",  "currency" : "USD",  "yrn" : "urn:yaas:hybris:price:price:myshop;57962ec7ddb937001dfeebfe",  "effectiveAmount" : 9.999999999E7,  "priceEventType" : "CREATE_EVENT",  "tenant" : "myshop"}

For more information about how to read the events for a tenant


Match Currency Rules

The match currency functionality in the Price service can be used to retrieve a list of prices in a desired target currency.

When you change the site of the cart, you must make sure the corresponding price in the new currency is selected. With attributes such as sale price, volume price, unit of measurement price, and date range in Price service, the match currency functionality ensures that the current price parameters in the cart are respected when adjusting the price to the new currency.

The match currency feature looks for the most accurate price match within the target currency. If there is more than one price that matches the target currency, it looks for the price with the closest characteristic (for example, date range, volume, or unit of measurement) to the source (original) price. If it cannot find a price with the same characteristics as the source price, it returns the default price of the target currency.


Basics

Introduction

This tutorial covers the basic operations that can be made to the Price service: create, retrieve, update, and delete. In order to use the service, one must have the proper authorization. This can be achieved by injecting the access token into the header of a request.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Set the price for a product

You can set the price for any product for a specific tenant tenant by using the product's ID or YaaS Resource Name (YRN). When you set the price for a product, a unique price ID and YRN are returned. You can retrieve the price details for a specific product using the price ID. The price ID returns a price schema that contains the price, currency, product ID or YRN, and any other applicable attributes. You can assign multiple prices with different currencies to the same product.

For more information on YRNs and how they are composed, see the Global YaaS resource name (YRN) section in the Resource Identifier best practices.

The originalAmount, currency, and productId or itemYrn attributes are required in the request body.

  • productId: This ID is the product's reference identifier. This ID corresponds to the product created in the Product service. The ID format is a simple string, such as 53a358901b2e9dd2718b5c4e.
  • itemYrn: The global resource identifier is a reference to an external resource which this price is belonging to. This identifier corresponds to the product created in the Product service. It is a complex string following the resource identifier format, such as urn:yaas:hybris:product:productId-variantId:mytenant;product0001;variant0001.
  • originalAmount: The original price assigned to the given product. There is no limit to the originalAmount you can set for the price. It cannot be null or empty.
  • currency: The respective currency. It must be a string with three characters and must follow ISO standards.
  • group: A value assigned to a product's price to group certain prices and products together. You can only use the group attribute if you create a price using the itemYrn attribute.
  • measurementUnit: Sets a price for a specific unit of measurement. This attribute is optional.
    • quantity: The base quantity for a given unit of measurement, such as 1 kg.
    • unitCode: The unit of measurement. Options are kg, g, mg, l, ml, lb, qt, qtr, gal, pt, and oz.
  • dateRange: The date range in which the new price is valid. This attribute can be used if you assign a sale price, volume price, or unit of measurement price to the product. This attribute is optional.
    • startDate: The start date and time (in GMT) of the different price. The value must be in ISO 8601 format, such as 2015-01-10T10:20:00.236+0500.
    • endDate: The end date and time (in GMT) of the different price. The value must be in ISO 8601 format, such as 2015-02-10T10:20:00.236+0500.
  • wholesale: Specifies the quantity range for which a volume price is valid. This attribute is optional.
    • minQuantity The minimum quantity to which the volume price applies. The minimum value must be greater than 1, as a quantity of 1 is associated with the original price.
    • maxQuantity: The maximum quantity to which this price applies.
  • salePrice: Sets a sale price for a given product. The sale price is calculated by setting a discount. The discount can be a dollar amount or a percentage. This attribute is optional.
    • discountAmount: The value to subtract from the original price.
    • discountRate: The percentage to subtract from the original price.
    • description: Provides some additional information about this sale.

The response body contains the price ID (id) and price YRN (yrn). You can use the ID to:

  • Retrieve the price of a given product.
  • Update the product's price.
  • Delete the price assigned to this product.
    response = priceService.tenant(tenant).prices.post({
        "originalAmount": 55.5,
        "currency": "USD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p1",
        "group":"p1",
        "measurementUnit": {
            "quantity" : 1.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2014-11-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 20.00,
            "description" : "20$ OFF, Wow!"
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
        }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    priceId = response.body.id;
    yrn = response.body.yrn;
    response;

Confirm that the price was set by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Update a product's price schema

You can update the price schema of the product for this specific tenant. To do this, you need the price ID that was created when you initially created the product's price. For a given price ID, you can update any (or all) of these fields at one time:

  • originalAmount
  • currency
  • productId
  • measurementUnit
  • dateRange
  • salePrice
  • wholesale
response = priceService.tenant(tenant).prices.priceId(priceId).put({
                    "priceId": priceId,
          "yrn": yrn,
          "originalAmount": 65.42,
          "currency": "CAD",
          "itemYrn": "urn:yaas:hybris:price:price:myStore;p2",
          "group": "p2",
          "measurementUnit": {
              "quantity" : 1000.00,
              "unitCode" : "g"
          },
          "dateRange":{
              "startDate":"2015-11-10T10:10:20+08:00",
              "endDate" : "2016-10-10T10:10:23+08:00"
          },
          "salePrice":{
              "discountAmount" : 25.00,
              "description" : "Buy it while it's on discount!"
          },
          "wholesale":{
              "minQuantity" : 3.0,
              "maxQuantity" : 9.0
          }
        }, {
        headers: {
          'Authorization': 'Bearer ' + access_token,
          'Content-Type' : 'application/json'
        }
     });
     assert.equal(response.status, 204);
     response;

Retrieve a product's price using its ID

Retrieve the price you created using its ID.

response = priceService.tenant(tenant).prices.priceId(priceId).get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Delete a product's price using its ID

Delete the price you created using its ID.

response = priceService.tenant(tenant).prices.priceId(priceId).delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response.body;

Confirm that the price was deleted by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;


Querying

Introduction

This tutorial covers the multiples possibilities offered to retrieve one or multiple price(s) using the Price service's query parameters.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for the Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Create multiple prices for a tenant

In order to retrieve the prices, you must first create them for your tenant.

    response = priceService.tenant(tenant).prices.post({
        "originalAmount": 55.5,
        "currency": "USD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p1",
        "group":"p1",
        "measurementUnit": {
            "quantity" : 1.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2014-11-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 20.00,
            "description" : "20$ OFF, Wow!"
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;
    response = priceService.tenant(tenant).prices.post({
          "originalAmount": 65.5,
        "currency": "CAD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p2",
        "group":"p2",
        "measurementUnit": {
            "quantity" : 5.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2015-09-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 1.00,
            "description" : "1$ OFF, Meh."
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;
    response = priceService.tenant(tenant).prices.post({
          "originalAmount": 365.5,
        "currency": "USD",
        "productId": "prod12345",
        "measurementUnit": {
            "quantity" : 150.00,
            "unitCode" : "g"
        },
        "dateRange":{
            "startDate":"2016-09-10T10:10:20+08:00",
            "endDate" : "2016-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 300.00,
            "description" : "300$ OFF, Wut?"
        },
        "wholesale":{
            "minQuantity" : 4.0,
            "maxQuantity" : 5.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;

Confirm that the prices were set by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Retrieve all prices by query parameters

Aside from retrieving the price information for a specific price ID, you can also retrieve the information by querying a single or multiple parameters. The following is a list of the parameters you can use in your query:

  • product ID: If you assign a product different prices with different currencies, you can retrieve all prices assigned to that product using the productId value. This returns the price values for a single product.
  • item YRN: If you assign an itemYrn (product) for different prices with different currencies, you can retrieve all prices assigned to that itemYrn value. This returns the price values for a single product. If your item YRN includes a variant, but you have not set a price for that variant, the product price is returned.
  • group: Returns all the results for the item YRNs that have the same group value.
  • currency: Returns all the results that have the same currency.
  • effective date: This request allows you to retrieve all product prices, as well as those products that have a date range corresponding to the effective date. This effective date should be in ISO 8601 format, such as 2015-01-23T22:00:00.
Make sure the URI size is limited to 2 KB.

You can query by single parameter:

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      },
    query: {
      'productId': 'prod12345'
    }
  }
);
assert.equal(response.status, 200);
response.body;

Or multiple parameters:

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      },
    query: {
      'currency': 'USD',
      'group': 'p1'
    }
    }
);
assert.equal(response.status, 200);
response.body;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;



Sorting

Introduction

This tutorial covers the usage of the sorting query parameter for the Price service.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for the Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Create multiple prices for a tenant

In order to retrieve the prices, you must first create them for your tenant.

    response = priceService.tenant(tenant).prices.post({
        "originalAmount": 55.5,
        "currency": "USD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p1",
        "group":"p1",
        "measurementUnit": {
            "quantity" : 1.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2014-11-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 20.00,
            "description" : "20$ OFF, Wow!"
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;
    response = priceService.tenant(tenant).prices.post({
          "originalAmount": 65.5,
        "currency": "CAD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p2",
        "group":"p2",
        "measurementUnit": {
            "quantity" : 5.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2015-09-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 1.00,
            "description" : "1$ OFF, Meh."
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;
    response = priceService.tenant(tenant).prices.post({
          "originalAmount": 365.5,
        "currency": "USD",
        "productId": "prod12345",
        "measurementUnit": {
            "quantity" : 150.00,
            "unitCode" : "g"
        },
        "dateRange":{
            "startDate":"2016-09-10T10:10:20+08:00",
            "endDate" : "2016-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 300.00,
            "description" : "300$ OFF, Wut?"
        },
        "wholesale":{
            "minQuantity" : 4.0,
            "maxQuantity" : 5.0
        }
      }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    response;

Confirm that the prices were set by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Sort all the prices

In order to view the results in a more organized manner, you can sort how the results are displayed.

Either ascending:

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      },
    query: {
      'sort': 'originalAmount:asc'
    }
  }
);
assert.equal(response.status, 200);
response.body;

Or descending:

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      },
    query: {
      'sort': 'currency:desc'
    }
  }
);
assert.equal(response.status, 200);
response.body;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;


Partial Update

Introduction

This tutorial covers how to update only a select few parameters of a price by doing a partial update rather than a full update.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for the Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Create a price for the tenant

Before you can complete a partial update, you need to create a price.

    response = priceService.tenant(tenant).prices.post({
        "originalAmount": 55.5,
        "currency": "USD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p1",
        "group":"p1",
        "measurementUnit": {
            "quantity" : 1.00,
            "unitCode" : "kg"
        },
        "dateRange":{
            "startDate":"2014-11-10T10:10:20+08:00",
            "endDate" : "2015-10-10T10:10:23+08:00"
        },
        "salePrice":{
            "discountAmount" : 20.00,
            "description" : "20$ OFF, Wow!"
        },
        "wholesale":{
            "minQuantity" : 2.0,
            "maxQuantity" : 10.0
        }
        }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    priceId = response.body.id;
    response;

Partially update a product's price schema

You can partially update the price schema of the product for this specific tenant. To do this, you need the price ID that was created when you initially created the product's price.

response = priceService.tenant(tenant).prices.priceId(priceId).put({
          "originalAmount": 145.42
        }, {
        headers: {
          'Authorization': 'Bearer ' + access_token,
          'Content-Type' : 'application/json'
        },
        query: {
               'partial': 'true'
           }
     });
     assert.equal(response.status, 204);
     response;

Confirm that the price was updated by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;


Match Price

Introduction

This tutorial covers the usage of the Match Price functionality for the Price service. The match currency functionality retrieves a list of prices that match your target currency. For more information on the match currency functionality see the Match Currency Rules. For more information on the different price attributes, see the Set the price for a product section in the Basics tutorial.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for the Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Set the price for a product

Create a price to use for this tutorial.

    response = priceService.tenant(tenant).prices.post({
        "originalAmount": 55.5,
        "currency": "USD",
        "itemYrn": "urn:yaas:hybris:price:price:myStore;p1",
        "group":"p1",
        "measurementUnit": {
            "quantity" : 4.00,
            "unitCode" : "kg"
        }
        }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    priceId = response.body.id;
    response.body;

Match prices for a specific currency

The request is automatically triggered when you change the site code of a given cart and there are items in the cart. This ensures that the items in the cart have a corresponding price in the currency of the new site. For more information, see the Change Cart Site tutorial.

The request body contains these attributes:

  • targetCurrency: The currency to which you want to match the items in your cart. It must be a string with three characters, and it must follow ISO standards.
  • prices: Contains the price ID and quantity of the items in the cart. The quantity is needed because of the wholesale attribute. If a volume price applies to the item in the new currency, that determines the item's new price.

The response body contains a list or array of prices assigned to the target currency and price ID.

    response = priceService.tenant(tenant).prices.matchCurrency.post({
         'prices': [
                {
                    'priceId':(priceId),
                    'quantity': 4.0
                }
            ],
            'targetCurrency': "USD"
       }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 200);
    response.body;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;


Error Codes

For more information about error codes, see the API Reference.


Custom Attribute

Introduction

This tutorial covers the handling of custom attributes for the Price service. Here you can create, retrieve, update, and delete a custom-made attribute.

Setup

Assertion

Define variable assert:

    assert = chai.assert;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

    API.createClient('oAuth2Service',
    '/services/oauth2/v1/api.raml');

Now, get the token:

    tenant = projectIdPlaceholder;
    AccessToken = oAuth2Service.token.post({
      'client_id' : clientIdPlaceholder,
      'client_secret':clientSecretPlaceholder,
      'grant_type' : 'client_credentials',
      'token_type': 'Bearer',
      'scope': 'hybris.tenant='+tenant+ ' hybris.price_manage hybris.price_delete_all'
    });

To make the calls simple and the code clean, assign the id of the returned object to a variable:

    access_token = AccessToken.body.access_token;

Create an API client for the Price service

    API.createClient('priceService',
    '/services/price/v1/api.raml');

Set the price for a product

Start off by creating a new price for a product.

    response = priceService.tenant(tenant).prices.post({
              "productId": "product12345",
          "originalAmount": 55.5,
          "currency": "USD"
        }, {
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        }
        }
    );
    assert.equal(response.status, 201);
    priceId = response.body.id;
    response;

Create a custom attribute for a Price ID

You can add custom attributes such as the AlphaDisplay attribute, which spells out the price instead of showing it numerically. You can add any custom attribute you want but you must provide a key and value in the request body in order to create it.

  • key: The key is used to retrieve the value of the attribute. The value does not have any restrictions, but it cannot be null or empty.
  • value: The value you assign to the custom attribute. The value does not have any restrictions.
    response = priceService.tenant(tenant).prices.priceId(priceId).customattributes.post({
            "AlphaDisplay": "Fifty five US dollars and five pennies",
            "YouCanCreateAnyAttributeYouWant": "So long as the key isn't null or empty!"
          }, {
          headers: {
              'Authorization': 'Bearer ' + access_token,
              'Content-Type' : 'application/json'
          }
        }
    );
    assert.equal(response.status, 201);
    response;

Confirm that the custom attributes were added by retrieving the prices for the tenant.

response = priceService.tenant(tenant).prices.get({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 200);
response.body;

Update a custom attribute value

Once you have created a custom attribute, you can update its value at any time.

    response = priceService.tenant(tenant).prices.priceId(priceId).customattributes.attributeKey("AlphaDisplay").put({
            "value": "Fifty five US dollars and five cents"
          }, {
          headers: {
              'Authorization': 'Bearer ' + access_token,
              'Content-Type' : 'application/json'
          }
        }
    );
    assert.equal(response.status, 204);
    response;

Retrieve a custom attribute for a price ID

You can retrieve custom attributes that you created by making a request with the custom attribute key or the price ID.

    response = priceService.tenant(tenant).prices.priceId(priceId).customattributes.attributeKey("AlphaDisplay").get({},{
          headers: {
              'Authorization': 'Bearer ' + access_token,
              'Content-Type' : 'application/json'
          }
        }
    );
    assert.equal(response.status, 200);
    response;

Delete a custom attribute for a price ID

You can delete a custom attribute for a specific price ID.

    response = priceService.tenant(tenant).prices.priceId(priceId).customattributes.attributeKey("AlphaDisplay").delete({},{
          headers: {
              'Authorization': 'Bearer ' + access_token,
              'Content-Type' : 'application/json'
          }
        }
    );
    assert.equal(response.status, 204);
    response;

Cleanup

Before moving to other tutorials, make sure all the prices are deleted for this project.

response = priceService.tenant(tenant).prices.delete({}, {
      headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/json'
      }
    }
);
assert.equal(response.status, 204);
response;


Glossary

TermDescription
effective amountThe price of a product that is currently in effect if a sale price, volume price, or unit of measurement price is applied.
effective dateA specific date used to retrieve a list of prices. The effective date is usually applied to prices that use the date range attribute.
date rangeDate range in which the price is valid.
match currencyFeature that allows you to retrieve a list of prices that matches a specific currency.
original amountThe product"s original or default price.
productAn object that belongs to the store.
siteA location that the store is based on. Typically the site is based on a country, for example Canada.
unit of measurementPrice for a specific unit of measure. For example, kg, l, gal, etc.
volumeQuantity range for which a price is valid.


  • Send feedback

    If you find any information that is unclear or incorrect, please let us know so that we can improve the Dev Portal content.

  • Get Help

    Use our private help channel. Receive updates over email and contact our specialists directly.

  • hybris Experts

    If you need more information about this topic, visit hybris Experts to post your own question and interact with our community and experts.