Product.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.field_description" />
  8. <span class="price">{{ product.price__number }}</span>
  9. </section>
  10. <aside class="">
  11. <input
  12. v-if="product.field_multiple"
  13. v-model="quantity"
  14. placeholder="quantity"
  15. type="text"
  16. name="quantity"
  17. value="1"
  18. />
  19. <button
  20. type="button"
  21. name="addtocart"
  22. @click.stop="checkaddtocart"
  23. >
  24. Commander
  25. </button>
  26. </aside>
  27. <Modal
  28. v-if="login"
  29. >
  30. i'm a modal
  31. </Modal>
  32. </article>
  33. </template>
  34. <script>
  35. import { REST } from 'vuejs/api/rest-axios'
  36. import router from 'vuejs/route'
  37. import { mapState, mapActions } from 'vuex'
  38. import Modal from 'vuejs/components/Helper/Modal'
  39. let basePath = drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix;
  40. export default {
  41. name: "Product",
  42. router,
  43. props: ['product'],
  44. data(){
  45. return {
  46. quantity: 1,
  47. login:false,
  48. register:false
  49. }
  50. },
  51. computed: {
  52. ...mapState({
  53. isloggedin: state => state.User.isloggedin
  54. })
  55. },
  56. methods:{
  57. ...mapActions({
  58. userLogin: 'User/userLogin'
  59. }),
  60. checkaddtocart() {
  61. console.log('checkaddtocart');
  62. if(!this.isloggedin){
  63. // TODO: show popup login or register
  64. this.login = true
  65. // TODO: rest login or register
  66. // TODO: rest login
  67. // TODO: this.addtocart()
  68. }else{
  69. this.addtocart()
  70. }
  71. },
  72. addtocart () {
  73. console.log("addtocart")
  74. REST.post(`/cart/add?_format=json`, [{
  75. "purchased_entity_type": "commerce_product_variation",
  76. "purchased_entity_id": this.product.variation_id,
  77. "quantity": this.quantity
  78. }])
  79. .then(({ data }) => {
  80. console.log('product add to cart REST: data', data)
  81. })
  82. .catch(( error ) => {
  83. console.warn('Issue with pricing', error)
  84. Promise.reject(error)
  85. })
  86. }
  87. },
  88. components: {
  89. Modal
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. </style>