$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;
// 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)
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();
data: {
items: {}, // Populated from data attribute on root element so we can pass data from PHP
+ savingCart: false,
},
beforeMount() {
},
mounted() {
-
eventBus.$on('add-item', data => {
data.action = 'add';
this.saveCart(data);
},
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);
<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: {
</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);
}
{{-- 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>