Product price update
When a shopper adds a product to the cart, a third-party system is used to update the product price based on the shopper's information. For example, the price can be updated based on the shopper's location or the product's availability.
Webhook name
observer.sales_quote_item_set_product
Payloads
The following observer.sales_quote_item_set_product
default payload was obtained from execution of the application code. Some data has been adjusted or deleted for brevity.
Copied to your clipboard{"subject": [],"eventName": "sales_quote_item_set_product","data": {"product": {"store_id": 1,"entity_id": "11","attribute_set_id": "4","type_id": "simple","sku": "Simple product 3",..."extension_attributes": {...},...},"quote_item": {"store_id": 1,"quote_id": null,"product": {"store_id": 1,"entity_id": "11","attribute_set_id": "4","type_id": "simple","sku": "Simple product 3","has_options": "0",....},"product_id": "11","product_type": "simple","sku": "Simple product 3","name": "Simple product 3","weight": "10.000000","tax_class_id": "2","base_cost": null,"is_qty_decimal": false}}}
Copied to your clipboard{"product": {"name": "Simple product 3","sku": "Simple product 3","price": "10.000000"}}
Configuration
Copied to your clipboard<method name="observer.sales_quote_item_set_product" type="before"><hooks><batch name="product_price_update"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><hook name="sales_quote_item_update_product" url="{env:APP_BUILDER_URL}/validate-product-name" method="POST" fallbackErrorMessage="The product name cannot be validated" timeout="5000" softTimeout="1000"><fields><field name='product.name' source='data.product.name' /><field name='product.sku' source='data.product.sku' /><field name='product.price' source='data.product.price' /></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: observer.sales_quote_item_set_productWebhook type: beforeBatch name: product_price_updateHook name: sales_quote_item_update_productURL: <Host>/validate-product-nameTimeout: 5000Soft timeout: 1000Fallback Error Message: The product name cannot be validatedRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: product.nameSource: data.product.nameActive: YesName: product.skuSource: data.product.skuActive: YesName: product.priceSource: data.product.priceActive: Yes
Copied to your clipboard<method name="observer.sales_quote_item_set_product" type="before"><hooks><batch name="product_price_update"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><hook name="sales_quote_item_update_product" url="{env:APP_BUILDER_URL}/validate-product-name" method="POST" fallbackErrorMessage="The product name cannot be validated" timeout="5000" softTimeout="1000"><fields><field name='product.name' source='data.product.name' /><field name='product.sku' source='data.product.sku' /><field name='product.price' source='data.product.price' /></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: observer.sales_quote_item_set_productWebhook type: beforeBatch name: product_price_updateHook name: sales_quote_item_update_productURL: <Host>/validate-product-nameTimeout: 5000Soft timeout: 1000Fallback Error Message: The product name cannot be validatedRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: product.nameSource: data.product.nameActive: YesName: product.skuSource: data.product.skuActive: YesName: product.priceSource: data.product.priceActive: Yes
Endpoint code example
The following code example shows how to implement the webhook on your custom endpoint.
Copied to your clipboardconst fetch = require('node-fetch')const { Core } = require('@adobe/aio-sdk')const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')// main function that will be executed by Adobe I/O Runtimeasync function main (params) {// create a Loggerconst logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })try {// 'info' is the default level if not setlogger.info('Calling the main action')// log parameters, only if params.LOG_LEVEL === 'debug'logger.debug(stringParameters(params))//check for missing request input parameters and headersconst requiredParams = [/* add required params */]const requiredHeaders = ['Authorization']const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)if (errorMessage) {// return and log client errorsreturn errorResponse(400, errorMessage, logger)}// Place the real logic (calling 3rd party endpoints) here.// In this example price is just reduced by 25%.const { product: product} = params;let operations = [];operations.push({op: 'replace',path: 'data/product/price',value: parseFloat(product.price) * 0.75});// If no price or other updates is needed the success operation must be returned// operations.push({// op: 'success'// })return {statusCode: 200,body: JSON.stringify(operations)}} catch (error) {// log any server errorslogger.error(error)// return with 500return errorResponse(500, 'server error', logger)}}exports.main = main
The product will be added to the cart with the updated price if the webhook returns a replace operation with the new price. The product will be added to the cart with the original price if the webhook returns a success operation.