UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

147 lines (110 loc) 4.88 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>methods/add-item.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <script src="scripts/nav.js" defer></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="EcomCart.html">EcomCart</a><ul class='methods'><li data-type='method'><a href="EcomCart.html#addItem">addItem</a></li><li data-type='method'><a href="EcomCart.html#addProduct">addProduct</a></li><li data-type='method'><a href="EcomCart.html#clear">clear</a></li><li data-type='method'><a href="EcomCart.html#increaseItemQnt">increaseItemQnt</a></li><li data-type='method'><a href="EcomCart.html#removeItem">removeItem</a></li><li data-type='method'><a href="EcomCart.html#reset">reset</a></li><li data-type='method'><a href="EcomCart.html#save">save</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-@ecomplus_shopping-cart.html">@ecomplus/shopping-cart</a></li></ul><h3>Events</h3><ul><li><a href="EcomCart.html#event:addItem">addItem</a></li><li><a href="EcomCart.html#event:change">change</a></li><li><a href="EcomCart.html#event:clear">clear</a></li><li><a href="EcomCart.html#event:increaseItemQnt">increaseItemQnt</a></li><li><a href="EcomCart.html#event:removeItem">removeItem</a></li><li><a href="EcomCart.html#event:reset">reset</a></li><li><a href="EcomCart.html#event:save">save</a></li></ul><h3>Global</h3><ul><li><a href="global.html#ecomCart">ecomCart</a></li></ul> </nav> <div id="main"> <h1 class="page-title">methods/add-item.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>import { randomObjectId } from '@ecomplus/utils' import fixItemQuantity from './../lib/fix-item-quantity' import fixSubtotal from './../lib/fix-subtotal' /** * @method * @name EcomCart#addItem * @description Push new item to cart data and save. * * @param {object} newItem - New cart item object valid for * {@link https://developers.e-com.plus/docs/api/#/store/carts/carts E-Com Plus `cart.items`} * @param {boolean} [canSave=true] - Save cart data * * @returns {object|null} Returns the saved item object (with `_id`) or null * when new item object is invalid. * * @example ecomCart.addItem({ _id: '12300000000000000000000f', product_id: '123a5432109876543210cdef', sku: 's-MP_2B4', name: 'Mens Pique Polo Shirt', quantity: 4, price: 42.9, keep_item_price: false }) */ export default ({ data, save }, emitter, [newItem, canSave = false]) => { if ( typeof newItem.product_id !== 'string' || typeof newItem.quantity !== 'number' || !(newItem.quantity >= 0) || typeof newItem.price !== 'number' || !(newItem.price >= 0) ) { return null } let fixedItem for (let i = 0; i &lt; data.items.length; i++) { const item = data.items[i] if (item.product_id === newItem.product_id &amp;&amp; item.variation_id === newItem.variation_id) { item.quantity += newItem.quantity if (newItem.price) { item.price = newItem.price } if (newItem.final_price) { item.final_price = newItem.final_price } fixedItem = fixItemQuantity(item) } } if (!fixedItem) { if (!newItem._id) { newItem._id = randomObjectId() } data.items.push(newItem) fixedItem = fixItemQuantity(newItem) } fixSubtotal(data) /** * @event EcomCart#addItem * @type {object} * @property {object} data - Shopping cart data * @property {object} item - Item added to cart * @example ecomCart.on('addItem', ({ data, item }) => { console.log(data, item) }) */ emitter.emit('addItem', { data, item: fixedItem }) if (canSave) { save(false) } return fixedItem } </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Sat Jan 25 2020 19:06:58 GMT-0300 (Brasilia Standard Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/polyfill.js"></script> <script src="scripts/linenumber.js"></script> </body> </html>