]> _ Git - pmi.git/commitdiff
fix #2981 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 14:47:05 +0000 (16:47 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 14:47:05 +0000 (16:47 +0200)
app/Http/Controllers/AjaxController.php
resources/js/app.js
resources/js/components/Cart.vue
resources/js/components/CartItem.vue
resources/views/pages/cart.blade.php

index d5e06144b543c4530028e519dcdb8ae99ac7a5ea..23700ed2fe5aa7659ed8711a706e6c55341f6aae 100644 (file)
@@ -141,17 +141,29 @@ class AjaxController extends CubistFrontController
         $cart_items = $request->session()->get('cart_items', []);
 
         switch ($request->input('action')) {
-
             case 'add':
                 // If the item already exists in the cart, increment the quantity
                 if (isset($cart_items[$id])) {
                     $cart_items[$id] += $quantity;
                 } else {
                     $cart_items[$id] = $quantity;
+                    $needs_update = true;
                 }
                 break;
 
             case 'update':
+                if (!isset($cart_items[$id])) {
+                    $ga['add'][$id] = $quantity;
+                } else {
+                    if ($cart_items[$id] < $quantity) {
+                        $ga['add'][$id] = $quantity - $cart_items[$id];
+                    } else if ($cart_items[$id] > $quantity) {
+                        $ga['remove'][$id] = $cart_items[$id] - $quantity;
+                    }
+                }
+                if ($quantity <= 0) {
+                    $needs_update = true;
+                }
                 $cart_items[$id] = $quantity;
                 break;
 
@@ -163,7 +175,7 @@ class AjaxController extends CubistFrontController
         // Save back to the session
         $request->session()->put('cart_items', $cart_items);
 
-        return Product::getCartData();
+        return ['needs_update' => $needs_update, 'cart_data' => Product::getCartData(), 'ga' => $ga];
     }
 
     public function request_quote(Request $request)
index cc15862cb930a2c400d039ef3ac7440fa5e895c8..6b112e8a33a88dfd91dd714943997549aa431c82 100644 (file)
@@ -8,8 +8,8 @@ require('./bootstrap');
 require('./menu');
 require('../../vendor/cubist/cms-back/src/public/emailobfuscator/emailobfuscator');
 
-var glob = require( 'glob' );
-var path = require( 'path' );
+var glob = require('glob');
+var path = require('path');
 
 window.Vue = require('vue');
 window.eventBus = new Vue();
@@ -40,6 +40,7 @@ const app = new Vue({
 
     data: {
         items: {}, // Populated from data attribute on root element so we can pass data from PHP
+        savingCart: false,
     },
 
     beforeMount() {
@@ -47,7 +48,6 @@ const app = new Vue({
     },
 
     mounted() {
-
         eventBus.$on('add-item', data => {
             data.action = 'add';
             this.saveCart(data);
@@ -78,15 +78,28 @@ const app = new Vue({
     },
 
     methods: {
-
         saveCart(data) {
             let root = this;
-
             axios.post('/ajax/cart', data)
                 .then(function (response) {
-                    //console.log('Cart updated');
-                    //console.table(response.data);
-                    root.items = response.data;
+                    if(response.data.needs_update) {
+                        root.items = response.data.cart_data;
+                    }
+
+                    // Google analytics cart events
+                    var idToName = {};
+                    root.items.forEach(function (i) {
+                        idToName[i.id] = i.reference;
+                    });
+
+                    for (var id in response.data.ga.add) {
+                        let quantity = response.data.ga.add[id];
+                        cubistga.addToCart(id, idToName[id], quantity);
+                    }
+                    for (var id in response.data.ga.remove) {
+                        let quantity = response.data.ga.remove[id];
+                        cubistga.removeFromCart(id, idToName[id], quantity);
+                    }
                 })
                 .catch(function (error) {
                     console.error('Error saving cart!', error);
index e60158278cafce5e90572fe20dd68eae255a0a75..16dc90802ae56dbdaf65e39a8f87b87c596ef02b 100644 (file)
@@ -1,6 +1,6 @@
 <template>
     <div class="cart-wrapper">
-        <cart-item v-for="item in items" :key="item.id" :item="item"></cart-item>
+        <cart-item v-for="item in items" :key="item.id" :item="item" :sendevents="sendevents"></cart-item>
     </div>
 </template>
 
             items: {
                 type: Array,
                 required: true,
-            }
+            },
+            sendevents:{
+                type:Boolean,
+                default:true,
+            },
         },
 
         methods: {
index 9380099d184f0da0f55170d0ffee45d10e7171e5..26bfea159bd33f40108676cd18f70b744e441bad 100644 (file)
@@ -6,13 +6,14 @@
         </a>
         <div class="pl-6 leading-relaxed flex-grow">
             <a :href="item.URL" class="block text-navy font-display">
+
                 {{ item.category }}
                 <br>
                 {{ item.name }}
             </a>
             <div class="cart-quantity bg-grey-100 py-1 pl-3 my-2 flex items-center justify-between">
                 <span class="mr-2">Quantité</span>
-                <number-input :value="item.quantity" :min="1" inline center controls @change="updateQuantity"></number-input>
+                <number-input v-model="item.quantity" :min="1" inline center controls></number-input>
             </div>
             <a href="#" class="cart-delete-item text-red block" @click.prevent="deleteItem">
                 Supprimer
         props: {
             item: {
                 type: Object
-            }
+            },
+            sendevents:{
+                type:Boolean,
+                default:true,
+            },
         },
 
-        methods: {
-            updateQuantity(newValue, oldValue) {
+        watch: {
+            'item.quantity': function (newValue, oldValue) {
+                if (!this.sendevents) {
+                    return;
+                }
+                if (newValue === oldValue) {
+                    return;
+                }
                 eventBus.$emit('update-item', {
                     id: this.item.id,
                     quantity: newValue,
                 });
-            },
+            }
+        },
 
+        methods: {
             deleteItem() {
                 eventBus.$emit('delete-item', this.item.id);
             }
index b4b42ac467df1fce89fde37014af2adf58e85e6e..3ac9298553502bb12d311cd71c15ee607bd5ab1e 100644 (file)
@@ -12,7 +12,7 @@
 
                 {{-- Nested divs to allow grey backgrounds of columns to match the height of their content instead of total height --}}
                 <div>
-                    <cart :items='items' class="cart-page bg-grey-100 p-1v pb-0 overflow-hidden"></cart>
+                    <cart :items='items' :sendevents="false" class="cart-page bg-grey-100 p-1v pb-0 overflow-hidden"></cart>
                 </div>
 
                 <div>