UNPKG

@eastsideco/escshopify

Version:

WIP JS library for Shopify, containing a variety of useful functionality.

421 lines (367 loc) 17.1 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/ajax/Cart.js | @eastsideco/escshopify</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> <a href="https://bitbucket.org/sigapps/escshopify.git">Repository</a></header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-cart">cart</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-easyCurrency">easyCurrency</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-geoService">geoService</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-utils">utils</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-external">E</span><span data-ice="name"><span><a href="https://www.npmjs.com/package/promise-queue">promise-queue</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#ajax">ajax</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/ajax/Cart.js~Cart.html">Cart</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyCart">ShopifyCart</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyCartLineItem">ShopifyCartLineItem</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency">easycurrency</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/EasyCurrency.js~EasyCurrency.html">EasyCurrency</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/MoneySpan.js~MoneySpan.html">MoneySpan</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/MoneySpanSet.js~MoneySpanSet.html">MoneySpanSet</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/Session.js~Session.html">Session</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/config.js~Config.html">Config</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/state.js~State.html">State</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-formatters">easycurrency/formatters</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/formatters/DefaultFormatter.js~DefaultFormatter.html">DefaultFormatter</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/formatters/Formatter.js~Formatter.html">Formatter</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-helpers">easycurrency/helpers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/helpers/SelectHelper.js~SelectHelper.html">SelectHelper</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-resolvers">easycurrency/resolvers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver.html">CurrencyResolver</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/resolvers/GeoserviceResolver.js~GeoserviceResolver.html">GeoserviceResolver</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#geoservice">geoservice</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/geoservice/API.js~API.html">API</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/geoservice/GeoService.js~GeoService.html">GeoService</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#http">http</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/http/RequestQueuable.js~RequestQueuable.html">RequestQueuable</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils">utils</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/Log.js~Log.html">Log</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/general.js~GeneralUtils.html">GeneralUtils</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils-loggers">utils/loggers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/ConsoleLogger.js~ConsoleLogger.html">ConsoleLogger</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/Logger.js~Logger.html">Logger</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/logLevel.js~LogLevel.html">LogLevel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-logLevel">logLevel</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/ajax/Cart.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Evee from &apos;evee&apos;; import Queue from &apos;promise-queue&apos;; import deepEqual from &apos;deep-equal&apos;; import _ from &apos;lodash&apos;; import config from &apos;config&apos;; import log from &apos;log&apos;; import ajaxApi from &apos;http/shopifyAJAXAPI&apos;; const TAG = &apos;Cart&apos;; /** * @typedef {Object} ShopifyCartLineItem * @property {Number} id * @property {Object|null} properties * @property {Number} quantity * @property {Number} variant_id * @property {String} key * @property {String} title * @property {Number} price * @property {Number} original_price * @property {Number} discounted_price * @property {Number} original_line_price * @property {Number} line_price * @property {Number} total_discount * @property {Object[]} discounts * @property {String|null} sku * @property {Number} grams * @property {String} vendor * @property {Boolean} taxable * @property {Number} product_id * @property {Boolean} gift_card * @property {String} url * @property {String|null} image * @property {String} handle * @property {Boolean} requires_shipping * @property {String|null} product_type * @property {String} product_title * @property {String|null} product_description * @property {String} variant_title * @property {String[]} variant_options */ /** * @typedef {Object} ShopifyCart * @property {String} token * @property {String} note * @property {Object} attributes * @property {Number} total_price * @property {Number} total_weight * @property {ShopifyCartLineItem[]} items * @property {Boolean} requires_shipping */ /** * Shopify Cart entity. * @extends {evee} */ export default class Cart extends Evee { /** * Construct a new instance of the cart entity. */ constructor() { super(); /** @type {promise-queue} */ this._queue = new Queue(1, Infinity); this._attributes = {}; this._ready = false; /** @type {Object[]} */ this._items = []; this._attributes = {}; } /** * Initialize the cart entity with the current state of the visitor&apos;s cart. * @param {ShopifyCart} cartData - Shopify Cart object. */ initialize(cartData) { this._loadFromShopifyCart(cartData); this._emitUpdate(&apos;init&apos;, null); if (this.items.length == 0) { this._emitClear([]); } log.send(log.DEBUG, TAG, &apos;Loaded.&apos;); } /** * Alias to getItems(); * @type {ShopifyCartLineItem[]} */ get items() { return this.getItems(); } /** * Returns items in the cart (in the normal Shopify line item format. * @returns {ShopifyCartLineItem[]} */ getItems() { return this._items; } /** * Alias to getTotalPrice(); * @type {Number} */ get total_price() { return this.getTotalPrice(); } /** * Alias to getTotalPrice(); * @type {Number} */ get totalPrice() { return this.getTotalPrice(); } /** * Calculates the total value of the cart. * @returns {Number} */ getTotalPrice() { return _.reduce(this.items, (total, item) =&gt; { return total + item.line_price; }, 0) } /** * Reloads the cart state via AJAX. * @returns {Promise&lt;src/ajax/Cart.js~Cart, Error&gt;} */ async reload() { var cartData = await ajaxApi.get(&apos;/cart.js&apos;); this._loadFromShopifyCart(cartData.data); return this; } /** * Alias to getAttributes() * @type {Object} */ get attributes() { return this.getAttributes(); } /** * Returns cart attributes. * @returns {Object} */ getAttributes() { return this._attributes; } /** * Sets all attributes on the cart. This will override/remove existing attributes. * @param {Object} attributes - New attributes for the cart. * @returns {Promise&lt;src/ajax/Cart.js~Cart, Error&gt;} */ setAttributes(attributes) { log.sendObject(log.DEBUG, TAG, &apos;Setting attributes...&apos;, { attributes }); return this._queue.add(async () =&gt; { var cart = await ajaxApi.post({attributes}); this._loadFromShopifyCart(cart.data); this._emitUpdate(&apos;attribute-updated&apos;, null); }); } getAttribute(key) { return this._attributes[key]; } setAttribute(key, value) { log.sendObject(log.DEBUG, TAG, &apos;Setting attribute...&apos;, { key, value }); return this._queue.add(async () =&gt; { var newAttributes = Object.assign({}, this._attributes); newAttributes[key] = value; var cart = await ajaxApi.post({attributes: newAttributes}); this._loadFromShopifyCart(cart.data); this._emitUpdate(&apos;attribute-updated&apos;, null); }); } /** * Add a new item to the cart. * @param {Number|String} id - Variant ID to add to cart. * @param {Number} quantity - Quantity to add. * @param {Object} properties - Line item attributes. * @returns {Promise&lt;ShopifyCartLineItem, Error&gt;} */ addItem(id, quantity, properties) { log.sendObject(log.DEBUG, TAG, &apos;Queuing addItem...&apos;, { id, quantity, properties }); return this._queue.add(async () =&gt; { var res = await ajaxApi.post(&apos;/cart/add.js&apos;, { id, quantity, properties }); var data = res.data; log.sendObject(log.DEBUG, TAG, &apos;addItem resonse&apos;, data); if (data.id) { var item = this._loadItem(data); this._emitAdd(item); this._emitUpdate(&apos;add&apos;, item); return item; } throw new Error(data); }); } updateItem(lineNumber, quantity, properties) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } removeItem(lineNumber) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } updateItemById(id, quantity, properties) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } updateItemById(id, quantity, properties) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } updateItemQuantitiesById(updates) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } removeItemById(id) { return this._queue.add(() =&gt; { return new Promise((acc, rej) =&gt; { }); }); } /** * Clear the cart. * @returns {Promise&lt;src/ajax/Cart.js~Cart, Error&gt;} */ clear() { return this._queue.add(async () =&gt; { var cartData = await ajaxApi.post(&apos;/cart/clear.js&apos;); var oldItems = this.items; this._loadFromShopifyCart(cartData); this._emitClear(oldItems); }); } _loadItem(itemData) { for (var item of this.items) { if (item.id == itemData.id &amp;&amp; deepEqual(item.properties, itemData.properties)) { item.quantity = itemData.quantity; return item; } } this.items.push(itemData); return itemData; } _emitUpdate(operation, item) { var data ={ items: this.items, operation, item }; log.sendObject(log.DEBUG, TAG, &apos;Event: update&apos;, data); this.emit(&apos;update&apos;, data); } _emitClear(oldItems) { var data ={ oldItems }; log.sendObject(log.DEBUG, TAG, &apos;Event: clear&apos;, data); this.emit(&apos;clear&apos;, data); } _emitAdd(item) { var data ={ items: this.items, item }; log.sendObject(log.DEBUG, TAG, &apos;Event: add&apos;, data); this.emit(&apos;add&apos;, data); } _emitUpdateItem(item) { var data ={ items: this.items, item }; log.sendObject(log.DEBUG, TAG, &apos;Event: updae-item&apos;, data); this.emit(&apos;update-item&apos;, data); } _emitRemove(item) { var data = { items: this.items, item }; log.sendObject(log.DEBUG, TAG, &apos;Event: remove&apos;, data); this.emit(&apos;clear&apos;, data); } _loadFromShopifyCart(cart) { this._items = cart.items; } } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.0.4)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>