Product.vue 4.8 KB

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