Is there a way to access the Item Search API respo...
# suitecommerce
r
Is there a way to access the Item Search API response data in shopping.ssp?
p
From backend (SSP and SS files) you can via 2 mechanisms: • For getting a set of items by id, commerce api has session.getItemFieldValues • For getting the raw api call, you can just do nlapiRequestURL to the item api you are interested in.
That being said... calling items from shopping.ssp doesn't sound like a good idea.
r
Thanks @PabloZ, I'm trying to redirect a user to the PDP if their search results return a unique single item in the PLP and thought that accessing the item search results would be a viable solution. Do you have any suggestions about how you might accomplish this or where in code you would extend to make this happen?
p
That should be built either with the extensibility API with the PLP component, or otherwise by modifying/extending the Facet Router or Facet Browse View.
r
Thanks again @PabloZ, I've been trying to utilize the extensibility API by creating an extension with a module that I access the PLP component from. Is it possible to cause a url redirect from the module mountToApp function? The idea I have is to check the PLP's items returned, and if only one item is returned, redirect the user to that item's PDP. I found an article that utilizes the PLP component to display an additional view and am wondering if I need to create a view and template to handle this instead of trying to code this within the mountToApp function.
s
The PLP component probably will not be able to access the search results until after rendering has begun (the page waits for a success/fail before doing so) so the redirect might not be very pretty if you do it that way. You have found the article that adds a child view on the PLP, my suggestion would be rather than straight redirecting a user after completing a keyword search that returns one item, just to show a banner that or something that encourages it.
I, personally, think that redirecting a user straight to a PDP when there is only one result is bad UX
p
@Ross Ireland i've just coded this for fun.
Copy code
/* global SC */
define('SZeta.OneResultRedirect', [
    'Backbone'
], function SZetaOneResultRedirect(
    Backbone
) {
    'use strict';
    return {
        mountToApp: function mountToApp (application) {
            application.getLayout().on('beforeAppendView', function onBeforeAppendView() {
                var itemsInfo;
                var firstItem;
                var onlyOneItem;
                var plpComponent = application.getComponent('PLP');
                if (plpComponent) {
                    itemsInfo = plpComponent.getItemsInfo();
                    firstItem = itemsInfo && itemsInfo.length > 0 && itemsInfo[0];
                    onlyOneItem = itemsInfo && itemsInfo.length === 1;
                    if (!onlyOneItem || !firstItem) {
                        return;
                    }
                    if (SC.ENVIRONMENT.jsEnvironment === 'browser') {
                        Backbone.history.navigate(firstItem.keyMapping_url, { trigger: true });
                    }
                }
            });
        }
    };
});
Disclaimer: This hasn't been tested except for the bare minimum. It's not something that is live on any projects either. Use at own discression šŸ™‚_._
For example, the use case where you search, have N results... and then you click on a facet to narrow down, and when applying the filter you then get one result, it will redirect you. Which might be annoying.
ā˜šŸ» 1
Or even worse... page 2 of results navigation.
I guess you can enhance it to support those use cases by reading from PLP component and the URL of the page, and add extra conditions for it to run.
s
btw you can swap out the use of the SC global var by using the environment component
bowtie 1