[SalesForce] Get a basket instance for a profile in SFCC

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.

Best Answer

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

Related Topic