项目作者: the-couch

项目描述 :
jquery free ajax cart for shopify
高级语言: JavaScript
项目地址: git://github.com/the-couch/richer.git
创建时间: 2017-08-26T16:18:59Z
项目社区:https://github.com/the-couch/richer

开源协议:

下载


Richer

js-standard-style

What is it?

This library is built on top of slater (which is a fork of shopify slate). It is based on the old implementation of the Timber depreciated richcart. The idea behind this comes from Slate and Timber both being tied to jQuery and wanting to build something that could be used independent of that.

Using It

We use richer to handle API requests to the shopify AJAX cart. How you implement it is entirely up to you. We expose a couple of common routes that allow you to easily handle updating/refreshing/generating your ajax cart.

I’ll outline a number of examples in YOYO below

Initialize a cart

  1. import RicherAPI from 'richer'
  2. // Some DOM defaults
  3. const defaults = {
  4. addToCart: '.js-add-to-cart', // classname
  5. addToCartForm: 'AddToCartForm', // id
  6. cartContainer: 'CartContainer', // id
  7. cartCounter: 'CartCounter', // id
  8. items: []
  9. }
  10. const config = Object.assign({}, defaults, options)
  11. const dom = {
  12. addToCartForm: document.getElementById(config.addToCartForm),
  13. cartContainer: document.getElementById(config.cartContainer),
  14. cartCounter: document.getElementById(config.cartCounter)
  15. }
  16. RicherAPI.getCart(cartUpdateCallback)
  17. // Updates a cart number and builds our cart
  18. const cartUpdateCallback = (cart) => {
  19. updateCount(cart)
  20. buildCart(cart)
  21. }
  22. const updateCount = (cart) => {
  23. const counter = dom.cartCounter
  24. counter.innerHTML = cart.item_count
  25. }
  26. const buildCart = (cart) => {
  27. const cartContainer = dom.cartContainer
  28. cartContainer.innerHTML = null
  29. if (cart.item_count === 0) {
  30. cartContainer.innerHTML = `<p>We're sorry your cart is empty</p>`
  31. return
  32. }
  33. var el = cartBlock(cart.items, cart, update)
  34. function cartBlock (items, cart, qtyControl) {
  35. return yo`
  36. <div class='r-cart'>
  37. ${items.map((item, index) => {
  38. const product = cleanProduct(item, index, config)
  39. return yo`
  40. <div class="r-cart__product f jcb">
  41. <div>
  42. <img src='${product.image}' alt='${product.name}' />
  43. </div>
  44. <div class="r-cart__product_info">
  45. <h5><a href='${product.url}'>${product.name}</a></h5>
  46. ${product.variation ? yo`<span>${product.variation}</span>` : null}
  47. ${realPrice(product.discountsApplied, product.originalLinePrice, product.linePrice)}
  48. ${yo`
  49. <div class="r-cart__qty f jcb">
  50. <div class="r-cart__qty_control" onclick=${() => qtyControl(product, product.itemMinus)}>
  51. <svg width="20" height="20" viewBox="0 0 20 20"><path fill="#444" d="M17.543 11.029H2.1A1.032 1.032 0 0 1 1.071 10c0-.566.463-1.029 1.029-1.029h15.443c.566 0 1.029.463 1.029 1.029 0 .566-.463 1.029-1.029 1.029z"></path></svg>
  52. </div>
  53. <span>${product.itemQty}</span>
  54. <div class="r-cart__qty_control" onclick=${() => qtyControl(product, product.itemAdd)}>
  55. <svg width="20" height="20" viewBox="0 0 20 20" class="icon"><path fill="#444" d="M17.409 8.929h-6.695V2.258c0-.566-.506-1.029-1.071-1.029s-1.071.463-1.071 1.029v6.671H1.967C1.401 8.929.938 9.435.938 10s.463 1.071 1.029 1.071h6.605V17.7c0 .566.506 1.029 1.071 1.029s1.071-.463 1.071-1.029v-6.629h6.695c.566 0 1.029-.506 1.029-1.071s-.463-1.071-1.029-1.071z"></path></svg>
  56. </div>
  57. </div>
  58. `}
  59. </div>
  60. </div>
  61. `
  62. })}
  63. ${subTotal(cart.total_price, cart.total_cart_discount)}
  64. </div>
  65. `
  66. }
  67. function subTotal (total, discount) {
  68. // TODO: handling discounts
  69. const totalPrice = slate.Currency.formatMoney(total) // eslint-disable-line
  70. return yo`
  71. <div>
  72. <h5>Subtotal: ${totalPrice}</h5>
  73. </div>
  74. `
  75. }
  76. function realPrice (discountsApplied, originalLinePrice, linePrice) {
  77. if (discountsApplied) {
  78. return yo`
  79. <div>
  80. <small className='strike'>${originalLinePrice}</small>
  81. <br /><span>${linePrice}</span>
  82. </div>
  83. `
  84. } else {
  85. return yo`
  86. <span>${linePrice}</span>
  87. `
  88. }
  89. }
  90. function update (item, quantity) {
  91. RicherAPI.changeItem((item.index + 1), quantity, refreshCart)
  92. }
  93. function refreshCart (cart) {
  94. let newCart = cartBlock(cart.items, cart, update)
  95. yo.update(el, newCart)
  96. }
  97. cartContainer.appendChild(el)
  98. }
  1. import RicherAPI from 'richer'
  2. const dom = {
  3. addToCartForm: document.getElementById('AddToCartForm'),
  4. }
  5. const AddToCart = () => {
  6. const form = dom.addToCartForm
  7. form.addEventListener('submit', (e) => {
  8. e.preventDefault()
  9. form.classList.remove('is-added')
  10. form.classList.add('is-adding')
  11. RicherAPI.addItemFromForm(e.target, itemAddedCallback, itemErrorCallback)
  12. })
  13. const itemAddedCallback = () => {
  14. RicherAPI.getCart(cartUpdateCallback)
  15. }
  16. const itemErrorCallback = (XMLHttpRequest, textStatus) => {
  17. console.log('error family')
  18. }
  19. }
  20. const cartUpdateCallback = (cart) => {
  21. updateCount(cart)
  22. buildCart(cart)
  23. RicherAPI.onCartUpdate(cart)
  24. }

MIT License