Creating a Search for Shopping Cart Information
The following examples show how you can use the SuiteScript 1.0 nlapiSearchRecord
method to get shopping cart information.
-
Use the following code snippet to search carts from a certain website (ID:1) which contain a particular item (item ID:352):
require(['N/search'], function(search) { // Create a search for shopping cart records var cartSearch = search.create({ type: search.Type.SHOPPING_CART }); // Create search filters var websiteIdFilter = search.createFilter({ name: 'websiteid', operator: search.Operator.ANYOF, values: [1] }); var itemIdFilter = search.createFilter({ name: 'itemid', operator: search.Operator.ANYOF, values: [352] }); // Create search columns var itemIdColumn = search.createColumn({ name: 'itemid' }); var itemQtyColumn = search.createColumn({ name: 'itemqty' }); var websiteIdColumn = search.createColumn({ name: 'websiteid' }); var customerIdColumn = search.createColumn({ name: 'customerid' }); // Add the filters and columns to the search cartSearch.filters = [websiteIdFilter, itemIdFilter]; cartSearch.columns = [itemIdColumn, itemQtyColumn, websiteIdColumn, customerIdColumn]; // Run the search var results = cartSearch.run(); });
-
Use the following code snippet to access the shopping cart using the customer record. This snippet is written to get carts from all websites visited by a certain customer (ID:324):
require(['N/search'], function(search) { // Create a search for customer records var customerSearch = search.create({ type: search.Type.CUSTOMER }); // Create a search filter var internalIdFilter = search.createFilter({ name: 'internalid', operator: search.Operator.ANYOF, values: [324] }); // Create search columns var websiteIdColumn = search.createColumn({ name: 'websiteid', join: 'shoppingcart' }); var itemIdColumn = search.createColumn({ name: 'itemid', join: 'shoppingcart' }); var itemQtyColumn = search.createColumn({ name: 'itemqty', join: 'shoppingcart' }); // Add the filter and columns to the search customerSearch.filters = [internalIdFilter]; customerSearch.columns = [websiteIdColumn, itemIdColumn, itemQtyColumn]; // Run the search and get the value of the websiteid field in each result customerSearch.run().each(function(result) { var websiteIdField = result.getValue({ name: 'websiteid' }); // Work with the websiteid value return true; }); });