Product.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <article class="product">
  3. <header>
  4. <h1 v-html="product.title"/>
  5. </header>
  6. <section class="content">
  7. <div class="description" v-html="product.body"/>
  8. <!-- <div class="price-description" v-html="product.price_description"/> -->
  9. </section>
  10. <aside v-if="!isAdherent">
  11. <div
  12. class="variation"
  13. v-for="variation in product.variations"
  14. :key="variation.id"
  15. >
  16. <div class="variation-description" v-html="variation.description"/>
  17. <button
  18. type="button"
  19. name="addtocart"
  20. @click.stop="checkaddtocart($event, variation.id)"
  21. >
  22. {{ $t("default.Add to cart") }}
  23. </button>
  24. </div>
  25. </aside>
  26. <Modal
  27. v-if="showLoginModal"
  28. @close="closeModal"
  29. :styles="{width:'500px', height:'350px'}"
  30. >
  31. <section id="pricing-modal-login-register">
  32. <h2>{{ $t("materio.Please login or create a new account to order your access") }}</h2>
  33. <LoginRegister
  34. :callbackargs="showLoginModal"
  35. @onLogedIn="onLogedIn"
  36. @onRegistered="onRegistered"
  37. />
  38. </section>
  39. </Modal>
  40. </article>
  41. </template>
  42. <script>
  43. import { REST } from 'vuejs/api/rest-axios'
  44. import router from 'vuejs/route'
  45. import { mapState, mapActions } from 'vuex'
  46. import Modal from 'vuejs/components/Helper/Modal'
  47. import LoginRegister from 'vuejs/components/Helper/LoginRegister'
  48. let basePath = drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix;
  49. export default {
  50. name: "Product",
  51. router,
  52. props: ['product'],
  53. data(){
  54. return {
  55. quantity: 1,
  56. showLoginModal:false
  57. }
  58. },
  59. computed: {
  60. ...mapState({
  61. isloggedin: state => state.User.isloggedin,
  62. isAdherent: state => state.User.isAdherent,
  63. csrftoken: state => state.User.csrftoken
  64. })
  65. },
  66. methods:{
  67. // ...mapActions({
  68. // userLogin: 'User/userLogin'
  69. // }),
  70. closeModal () {
  71. this.showLoginModal = false;
  72. },
  73. checkaddtocart(e, variation_id) {
  74. console.log('checkaddtocart')
  75. if (!this.isloggedin) {
  76. // show popup for login or register
  77. // login or register event will be catched by onLogedin or onRegistered
  78. this.showLoginModal = variation_id
  79. } else {
  80. // if already logedin directly goes to cart operations
  81. this.addtocart(variation_id)
  82. }
  83. },
  84. // event bubbled from modal login form
  85. onLogedIn (variation_id) {
  86. console.log('Product: onLogedIn. variation_id', variation_id)
  87. this.addtocart(variation_id)
  88. },
  89. // event bubbled from modal register form
  90. onRegistered (variation_id) {
  91. console.log('Product: onRegistered. variation_id', variation_id)
  92. this.addtocart(variation_id)
  93. },
  94. getCarts () {
  95. // this is bugging on stage
  96. return REST.get(`/cart?_format=json`, {}, {'X-CSRF-Token':this.csrftoken})
  97. // .then(({ data }) => {
  98. // console.log('current user carts: data', data)
  99. // })
  100. .catch((error) => {
  101. console.warn('Issue with get cart', error)
  102. Promise.reject(error)
  103. })
  104. },
  105. deleteCart (order_id) {
  106. console.log('deleting cart ', order_id)
  107. return REST.delete(`/cart/${order_id}/items?_format=json`)
  108. .then(({ data }) => {
  109. console.log(`product cart ${order_id} deleted: data`, data)
  110. })
  111. .catch((error) => {
  112. console.warn(`Issue with cart ${order_id} deleting`, error)
  113. Promise.reject(error)
  114. })
  115. },
  116. clearCarts (data) {
  117. let promises = [];
  118. // clear each cart as a promise
  119. for (let i = 0; i < data.length; i++) {
  120. promises.push(this.deleteCart(data[i].order_id))
  121. }
  122. // return all the promises as one
  123. return Promise.all(promises)
  124. },
  125. addtocart (variation_id) {
  126. console.log('addtocart')
  127. this.getCarts()
  128. .then(({data}) => {
  129. console.log('current user carts: data', data)
  130. this.clearCarts(data)
  131. .then(() => {
  132. console.log('all carts cleared')
  133. // fill the cart with new product
  134. REST.post(`/cart/add?_format=json`, [{
  135. "purchased_entity_type": "commerce_product_variation",
  136. "purchased_entity_id": variation_id,
  137. "quantity": this.quantity
  138. }])
  139. .then(({ data }) => {
  140. console.log('product added to cart: data', data)
  141. this.closeModal()
  142. // redirect to /cart
  143. // window.location.href = "/cart"
  144. // TODO: redirect to checkout instead of cart
  145. window.location.href = `/checkout/${data[0].order_id}/order_information`
  146. })
  147. .catch((error) => {
  148. console.warn('Issue with product add to cart', error)
  149. Promise.reject(error)
  150. })
  151. })
  152. })
  153. }
  154. },
  155. components: {
  156. Modal,
  157. LoginRegister
  158. }
  159. }
  160. </script>
  161. <style lang="scss" scoped>
  162. </style>