|
98 | 98 | $slideCheckoutBox.css({ zIndex: options.zIndex }); |
99 | 99 | } |
100 | 100 |
|
101 | | - const cartItems = siteStore.cartItems; |
| 101 | + let cartItems = siteStore.cartItems; |
102 | 102 | const mappedProducts = {}; |
103 | 103 |
|
104 | 104 | const methods = { |
|
338 | 338 | itemInv = methods.getInventory({ item: testItem }); |
339 | 339 | } |
340 | 340 | // max allowed is to limit how much a shopper can buy |
341 | | - const maxAllowed = item.product.maxPurchaseQuantity; |
| 341 | + const maxAllowed = parseInt(item.product.maxPurchaseQuantity, 10); |
342 | 342 | const newCount = item.count + step; |
343 | 343 | const noInvLimit = itemInv === -1; |
344 | 344 | const noMaxLimit = itemInv === -1; |
|
357 | 357 | // inventory & max allowed limited, desired count is under or equal to total inventory AND |
358 | 358 | // desired count is less than or equal to the max allowed |
359 | 359 | const addStepTwo = fullyLimited && newCount <= itemInv && newCount <= maxAllowed; |
| 360 | + // desired count is over maxAllowed |
| 361 | + const addOverMaxAllowed = fullyLimited && newCount > maxAllowed; |
360 | 362 | // inventory limit but no max allowed limit and desired count is more |
361 | 363 | // than total inventory |
362 | 364 | const addMaxStepOne = invLimitedMaxNot && (newCount > itemInv); |
|
375 | 377 | if (noInventory) { |
376 | 378 | // no inventory available to be added due to an option inventory limit |
377 | 379 | // like size/color etc. |
378 | | - methods.optionWarning[index] = `Not enough ${methods.lastOption} items in stock.`; |
379 | 380 | added = false; |
380 | | - errorMsg = methods.optionWarning[index]; |
| 381 | + if (!addOverMaxAllowed) { |
| 382 | + methods.optionWarning[index] = `Not enough ${methods.lastOption} items in stock.`; |
| 383 | + errorMsg = methods.optionWarning[index]; |
| 384 | + } |
381 | 385 | } else if (fullyUnlimited || addStepOne || addStepTwo || addStepThree) { |
382 | 386 | item.count += step; |
383 | 387 | } else if (addMaxStepOne || addMaxStepTwo) { |
|
391 | 395 | added = false; |
392 | 396 | errorMsg = methods.countWarning[index]; |
393 | 397 | } |
| 398 | + // customer error checks |
| 399 | + if (addOverMaxAllowed) { |
| 400 | + methods.countWarning[index] = `Item limited to ${maxAllowed} per order.`; |
| 401 | + } |
394 | 402 | methods.buildCart(cartItems); |
395 | 403 | methods.saveCart(cartItems); |
396 | 404 | return { added, errorMsg }; |
|
401 | 409 | methods.countWarning = {}; |
402 | 410 | methods.optionWarning = {}; |
403 | 411 | const item = cartItems[index]; |
404 | | - const minAllowed = item.product.minPurchaseQuantity; |
| 412 | + const minAllowed = parseInt(item.product.minPurchaseQuantity, 10); |
405 | 413 | const noMinLimit = minAllowed === -1; |
406 | 414 | const step = parseInt(item.product.countStep, 10); |
407 | 415 | const newCount = item.count - step; |
|
1135 | 1143 | } |
1136 | 1144 | } |
1137 | 1145 | if (index >= 0 && product) { |
1138 | | - // this would mean the at least one of the product is already in the cart |
| 1146 | + // this would mean that at least one of the product is already in the cart |
1139 | 1147 | const testItem = methods.queryCounts({ newCartItem, payload, count }); |
1140 | 1148 | const inventory = methods.getInventory({ item: testItem }); |
1141 | 1149 | const canAdd = inventory > 0 || inventory === -1; |
|
1185 | 1193 | <div class="drzSlideCheckout-cart-alertMsg"> |
1186 | 1194 | Message here |
1187 | 1195 | </div> |
1188 | | - <a |
| 1196 | + <button |
1189 | 1197 | href="#" |
1190 | 1198 | name="alert-checkout" |
1191 | | - class="drzSlideCheckout-cart-alertCO">Checkout</a> |
| 1199 | + class="drzSlideCheckout-cart-alertCO">Checkout</button> |
1192 | 1200 | </div> |
1193 | 1201 | </div> |
1194 | 1202 | `); |
|
1225 | 1233 | } |
1226 | 1234 | if (params.message) { |
1227 | 1235 | methods.$alert.find('.drzSlideCheckout-cart-alertMsg') |
1228 | | - .html(params.message); |
| 1236 | + .html(`<span role="alert">${params.message}</span>`); |
1229 | 1237 | } |
1230 | 1238 | }, |
| 1239 | + mapCart(cart = []) { |
| 1240 | + // We use this to update any items saved in the cart from a users |
| 1241 | + // previous session. In some cases, a store may update product info |
| 1242 | + // so we always want that up to date in the frontend |
| 1243 | + const map = []; |
| 1244 | + cart.forEach((item) => { |
| 1245 | + if (mappedProducts[item.product._id]) { |
| 1246 | + const updated = $.extend(true, item, { |
| 1247 | + product: mappedProducts[item.product._id], |
| 1248 | + }); |
| 1249 | + map.push(updated); |
| 1250 | + } |
| 1251 | + }); |
| 1252 | + return map; |
| 1253 | + }, |
1231 | 1254 | fetchProducts() { |
1232 | 1255 | // fetch all products on pages |
1233 | 1256 | const $productBtns = $('[data-product-id]'); |
|
1256 | 1279 | $(`[name="add-to-cart"][data-product-id="${product._id}"]`).addClass(classes.disabled); |
1257 | 1280 | } |
1258 | 1281 | }); |
| 1282 | + cartItems = methods.mapCart(cartItems); |
| 1283 | + methods.buildCart(cartItems); |
1259 | 1284 | }, |
1260 | 1285 | }); |
1261 | 1286 | }, |
|
1266 | 1291 | // bind all listeners |
1267 | 1292 | $openCartBtn.click(methods.onCartClick); |
1268 | 1293 | $backBtn.click(methods.onCartClick); |
1269 | | - methods.buildCart(cartItems); |
1270 | 1294 | // note: need to split some of the next button callbacks so |
1271 | 1295 | // they do not fire twice (ie with the form validation callback) |
1272 | 1296 | $fromStepBtn.click(methods.onFromClick); |
|
0 commit comments