1

Is it possible to get a basket instance for a profile in SFCC?

Here are my intentions:

I have a job with the step which does the following: CustomerMgr.processProfiles(sendEmailIfNecessary, ""); i.e. I query all the customers which created an account in my SiteGenesis store and then sendEmailIfNecessary needs to check for each customer whether or not the customer added something into his or her cart and in case he or she did add something the email should be issued telling the customer that there are goods in their cart.

So, in sendEmailIfNecessary I have an access to the profile of the user under consideration and now I want to access the basket for the customer.

I tried to use BasketMgr as a solution. And here is my attempt:

var sendEmailIfNecessary = function(profile) {
    var basket = BasketMgr.getBasket(profile.getUUID());
    var stop = 1;
};

But the solution does not work. I am telling this, because once I place a breakpoint in UX Studio on the var stop = 1; line I never reach it, while I am able to reach the line var basket = BasketMgr.getBasket(profile.getUUID()); with a breakpoint. Also I am not able to step over the line var basket = BasketMgr.getBasket(profile.getUUID());.

Also, the solution I tried should not work according to the documentation, since it states that:

This method returns a valid basket of the session customer or null if none is found.

While I am trying to access not the session customer, but all registered customers. And I was not able to find anything else reasonable in the documentation in regard to my problem.

Will appreciate any help. Thank you.

2 Answers 2

5

In the documentation it is said

"Restricted to agent scenario use cases: This method will result in an exception if called by a user without permission Create_Order_On_Behalf_Of or if no customer is logged in the session."

When you are in job context, you do not have a customer logged in the session. Hence you cannot use this Script API method this way.

Also the parameter is the UUID of the basket object, not of the customer profile. https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/class_dw_order_BasketMgr.html#dw_order_BasketMgr_getBasket_String_DetailAnchor

You could try using GET /customers/{customer_id}/baskets Shop API resource https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware.dochelp/OCAPI/19.1/shop/Resources/Customers.html#id152082845__id-1657198395

However keep in mind that for this one you would need a valid Business Manager user OAuth token, as you would not be able to use customer JWT token. The Business Manager user would need to have both the functional permissions Login_On_Behalf and Create_Order_On_Behalf_Of.

I personally have not tried using this Shop API from a job context in the way you are planning to do it. You may need to play around a bit with curl and API Explorer https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware.dochelp/OCAPI/19.1/usage/APIExplorer.html

Also take a look on the OCAPI performance best practices on the xchange portal (requires xchange developer login) https://xchange.demandware.com/docs/DOC-50733

2
  • Hello @Zatin Zlatev. Thank you for the help. How could I get the needed port for the request: hostname:port/dw/shop/v19_1/customers{customer_id}/baskets? And could you also provide a link on how could I generate an OAuth token? Why would one use REST API instead of a usual backend API is it a SFCC shortcoming? Or are there easier ways of implementing the abandonment cart in SFCC? Mar 5, 2019 at 13:11
  • You can use Script API, if you have storefront session, which you do not in a job context. Moreover you need to be logged in as this customer or as an agent that has the rights to do things on behalf of customers. If you have not used OCAPI before, I suggest you read the documentation from documentation.b2c.commercecloud.salesforce.com/DOC1/topic/… Mar 7, 2019 at 16:18
2

This information can only be gathered from OCAPI with a business manager login:

Example authentication request / response (I use sandbox client_id):

POST https://iteliosXX-alliance-prtnr-eu07-dw.demandware.net/dw/oauth2/access_token?client_id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: 5a73e33a-23a9-45a9-a942-d45204502bb9
Authorization: Basic xxxxxxxxxxxxx
User-Agent: PostmanRuntime/7.6.0
Accept: */*
Host: iteliosXX-alliance-prtnr-eu07-dw.demandware.net
cookie: dwsid=2IeGfCl4glug3k_eevyvGASfloEJ41MvZ21N3JXUhwV7BHzSty8g22fqYlbDIHzBjpQoKoK6UjBmERyxWPApdg==; cqcid=bcrzVR9FbnIZwa0r35B0VhRc6L; sid=HFEGvfualJeFyFWPh53SIJAS3zNG51IPc48; dwanonymous_5eac94f25e98e4dcb13b163dcfcb7651=bcrzVR9FbnIZwa0r35B0VhRc6L; dwsecuretoken_5eac94f25e98e4dcb13b163dcfcb7651=XAPaYwKFBYrRONspgSspTFoGiPIgQy4BQw==; __cq_dnt=1; dw_dnt=1
accept-encoding: gzip, deflate
content-length: 93
grant_type=urn:demandware:params:oauth:grant-type:client-id:dwsid:dwsecuretoken

HTTP/1.1 200
status: 200
Accept-Ranges: bytes
Keep-Alive: timeout=5
x-dw-request-base-id: W1leyCV6f1xlAAAK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 94
Date: Wed, 06 Mar 2019 07:43:33 GMT
Server: Apache
{"access_token":"87a90297-6610-4ccd-8bbd- 
c328fec80f5c","expires_in":899,"token_type":"Bearer"}

example basket request / response (pay attention to the access token):

GET https://iteliosXX-alliance-prtnr-eu07-dw.demandware.net/s/RefArchGlobal/dw/shop/v19_1/customers/abgnAf57gth8XEZ3lVMG3dJyzI/baskets
Origin: http://www.sitegenesis.com
x-dw-client-id: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Content-Type: application/json
Authorization: Bearer xxxxxxxxxxxxxxxx
cache-control: no-cache
Postman-Token: 4a6d7c88-1d26-4495-b60f-b671065ce352
User-Agent: PostmanRuntime/7.6.0
Accept: */*
Host: itelios-alliance-prtnr-eu07-dw.demandware.net
cookie: dwsid=2IeGfCl4glug3k_eevyvGASfloEJ41MvZ21N3JXUhwV7BHzSty8g22fqYlbDIHzBjpQoKoK6UjBmERyxWPApdg==; cqcid=bcrzVR9FbnIZwa0r35B0VhRc6L; sid=HFEGvfualJeFyFWPh53SIJAS3zNG51IPc48; dwanonymous_5eac94f25e98e4dcb13b163dcfcb7651=bcrzVR9FbnIZwa0r35B0VhRc6L; dwsecuretoken_5eac94f25e98e4dcb13b163dcfcb7651=XAPaYwKFBYrRONspgSspTFoGiPIgQy4BQw==; __cq_dnt=1; dw_dnt=1
accept-encoding: gzip, deflate

HTTP/1.1 200
status: 200
Accept-Ranges: bytes
Keep-Alive: timeout=5
x-dw-request-base-id: W1mSyNF6f1xlAAAK
Access-Control-Allow-Origin: http://www.sitegenesis.com
Access-Control-Expose-Headers: etag,location,x-dw-version-status,x-dw-resource-state,authorization,x-dw-request-base-id
Access-Control-Allow-Credentials: true
Allow: GET,HEAD,OPTIONS
x-dw-version-status: deprecated
Cache-Control: max-age=0,no-cache,no-store,must-revalidate
vary: accept-encoding
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 06 Mar 2019 07:46:26 GMT
Server: Apache
{"_v":"19.1","_type":"baskets_result","baskets":....

Dont forget to set the proper client permissions in business manager for your client_id. See below example from the documentation which should be enabled for the SHOP API on a site or global level.

{
  "_v":"19.1",
  "clients":
  [
    {
      "allowed_origins": 
["http://www.sitegenesis.com","https://secure.sitegenesis.com"],
       "client_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
       "response_headers":{"x-foo":"bar","P3P":"CP=\"NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM\""},
       "resources":
        [
          {
      "resource_id":"/product_search",
      "methods":["get"],
      "read_attributes":"(**)",
      "write_attributes":"(**)",
      "cache_time":900,
      "version_range":{"from":"19.1"}
    },
    {
      "resource_id":"/products/*/bundled_products",
      "methods":["get"],
      "read_attributes":"(c_name,c_street)",
      "write_attributes":"(**)"
    },
    {
      "resource_id":"/baskets/*/items",
      "methods":["post"],
      "read_attributes":"(**)",
      "write_attributes":"(product_id, quantity)"
    }
  ]
    }
   ]
   } 
3
  • Great, thank you. 1 Could you also post the Open Commerce API Settings from your BM, please? 2 I have to create a web service and configure it in order to perform such requests. Am I right? Mar 6, 2019 at 8:20
  • 1
    You only need to set the client permissions (what cliend_id=aaaaa...) is allowed to do) to enable use of the basket service. This is well described in the documentation: Salesforce B2C Commerce 19.3 > Open Commerce API 19.3 > OCAPI Usage 19.3
    – Jesper
    Mar 7, 2019 at 6:50
  • Make sure to replace the whole text after "Authorization: Basic" as it is base64 encoded (almost plain text) or replace it with some dummy data like dXNlcjpwYXNzOmFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ== Mar 7, 2019 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .