From: Vincent Vanwaelscappel Date: Tue, 3 Sep 2019 14:47:05 +0000 (+0200) Subject: fix #2981 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=dd7d1d6844f476db3d2eaac4beb3150f261b9b45;p=pmi.git fix #2981 @2 --- diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index d5e0614..23700ed 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -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) diff --git a/resources/js/app.js b/resources/js/app.js index cc15862..6b112e8 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -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); diff --git a/resources/js/components/Cart.vue b/resources/js/components/Cart.vue index e601582..16dc908 100644 --- a/resources/js/components/Cart.vue +++ b/resources/js/components/Cart.vue @@ -1,6 +1,6 @@ @@ -18,7 +18,11 @@ items: { type: Array, required: true, - } + }, + sendevents:{ + type:Boolean, + default:true, + }, }, methods: { diff --git a/resources/js/components/CartItem.vue b/resources/js/components/CartItem.vue index 9380099..26bfea1 100644 --- a/resources/js/components/CartItem.vue +++ b/resources/js/components/CartItem.vue @@ -6,13 +6,14 @@
+ {{ item.category }}
{{ item.name }}
Quantité - +
Supprimer @@ -34,17 +35,29 @@ 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); } diff --git a/resources/views/pages/cart.blade.php b/resources/views/pages/cart.blade.php index b4b42ac..3ac9298 100644 --- a/resources/views/pages/cart.blade.php +++ b/resources/views/pages/cart.blade.php @@ -12,7 +12,7 @@ {{-- Nested divs to allow grey backgrounds of columns to match the height of their content instead of total height --}}
- +