let input = $(this).closest('#kimplaycart').find('[name=qty]');
$.each(input, function (i, item) {
let ref = $(item).data('ref')
- $this.items['' + ref] = {
- quantity: $(item).val(),
- }
+ $this.items['' + ref]['quantity'] = $(item).val();
})
$this.fluidbook.menu.quickCloseView();
$this.getContactForm();
$(document).on(this.fluidbook.input.clickEvent, '#kimplay-additem button', function () {
let form = $(this).closest('#kimplay-additem');
$this.items['' + $(this).data('ref')] = {
+ name: $(form).find('h3').text(),
quantity: $(form).find('[name=qty]').val(),
comment: $(form).find('[name=comment]').val()
};
});
},
+ emptyCart: function () {
+ this.items = [];
+ this.updateCart();
+ this.save();
+ resize();
+ },
+
addToCart: function (ref) {
let quantity, comment;
let view = `<div id="kimplay-additem">
${this.fluidbook.menu.getCaption("", 'small')}
- <div class="content">
- <div class="image"><img src="${this.data[ref].image}" /></div>
- <div class="form">
- <h3>${this.data[ref].name}</h3>
- <div>(réf. ${ref})</div>
- <br>
- Quantité souhaitée :<br>
- <input type="text" name="qty" value="${quantity}"><br>
- Commentaire (facultatif)
- <textarea name="comment">${comment}</textarea><br>
- <button data-ref="${ref}">Ajouter à ma sélection</button>
- </div>
- </div>
- </div>`;
+ <div class="content">
+ <div class="image"><img src="${this.data[ref].image}" /></div>
+ <div class="form">
+ <h3>${this.data[ref].name}</h3>
+ <div>(réf. ${ref})</div>
+ <br>
+ <label>Quantité souhaitée :</label>
+ <input type="text" name="qty" value="${quantity}"><br>
+ <label>Commentaire (facultatif)</label>
+ <textarea name="comment">${comment}</textarea><br>
+ <button data-ref="${ref}">Ajouter à ma sélection</button>
+ </div>
+ </div>
+ </div>`;
this.fluidbook.menu.openCustomView(view, 'cart-kimplay-qty');
},
content += '<thead>';
content += '<tr>';
content += '<th></th>';
- content += '<th>' + this.fluidbook.l10n.__('ref') + '</th>';
- content += '<th class="col-designation">' + this.fluidbook.l10n.__('designation') + '</th>';
+ content += '<th>' + this.fluidbook.l10n.__('réf') + '</th>';
+ content += '<th class="col-designation">' + this.fluidbook.l10n.__('désignation') + '</th>';
content += '<th class="col-quantite">' + this.fluidbook.l10n.__('quantité') + '</th>';
content += '<th>' + this.fluidbook.l10n.__('commentaire') + '</th>';
content += '<th></th>';
content += '<tbody>';
$.each(this.getItems(), function (i) {
var item = $this.items[i];
- if (item === undefined) {
+ if (item === undefined || item === null) {
return;
}
var dataByRef = $this.data[i]
content += '<tr>';
+
if (dataByRef.image) {
content += '<td class="image"><img src="' + dataByRef.image + '"/></td>';
- }else {
+ }
+ else {
content += '<td></td>'
}
+
content += '<td class="reference"><span>' + i + '</span></td>';
- content += '<td class="designation"><span>' + dataByRef.name + '</span></td>';
+ content += '<td class="designation"><span>' + item.name + '</span></td>';
content += '<td class="quantite"><input type="text" name="qty" value="' + item.quantity + '" data-ref="' + i + '"></td>';
content += '<td class="commentaire"><span>' + item.comment + '</span></td>';
content += '<td class="delete"><a href="#" data-cart-delete="' + i + '">' + getSpriteIcon('interface-close') + '</a></td>';
getContactForm: function() {
let view = `<div id="kimplay-sendcart">
${this.fluidbook.menu.getCaption("Mes coordonnées", 'small')}
- <div class="content">
- <div class="form">
- <label for="company">Nom de l'entreprise*</label>
- <input type="text" name="company"><br>
- <label for="name">Nom*</label>
- <input type="text" name="name"><br>
- <label for="firstname">Prénom</label>
- <input type="text" name="firstname"><br>
- <label for="mail">Email*</label>
- <input type="email" name="mail"><br>
- <label for="phone">Téléphone*</label>
- <input type="text" name="phone"><br>
- <label for="address">Adresse</label>
- <input type="text" name="address"><br>
- <label for="message">Message</label>
- <textarea name="message"></textarea><br>
- <span>*Champs obligatoires</span>
- </div>
- <div class="cart-footer">
- <div class="fonctions">
- <a href="#/closeview" class="completeSelection" role="button" aria-label="${this.fluidbook.l10n.__('close')}">${this.fluidbook.l10n.__('compléter ma sélection')}</a>
- <button class="sendRequest" id="send-request">${this.fluidbook.l10n.__('envoyer ma demande')}</button>
- </div>
- </div>
- </div>
- </div>`;
+ <div class="content">
+ <form id="kimplay-sendcart-form">
+ <label for="company">Nom de l'entreprise*</label>
+ <input type="text" id="company" name="company"><br>
+ <label for="name">Nom*</label>
+ <input type="text" name="name"><br>
+ <label for="firstname">Prénom</label>
+ <input type="text" name="firstname"><br>
+ <label for="mail">Email*</label>
+ <input type="email" name="mail"><br>
+ <label for="phone">Téléphone*</label>
+ <input type="text" name="phone"><br>
+ <label for="address">Adresse</label>
+ <input type="text" name="address"><br>
+ <label for="message">Message</label>
+ <textarea name="message"></textarea><br>
+ <span>*Champs obligatoires</span>
+ </form>
+ <div class="cart-footer">
+ <div class="fonctions">
+ <a href="#/closeview" class="completeSelection" role="button" aria-label="${this.fluidbook.l10n.__('close')}">${this.fluidbook.l10n.__('compléter ma sélection')}</a>
+ <button class="sendRequest" id="send-request">${this.fluidbook.l10n.__('envoyer ma demande')}</button>
+ </div>
+ </div>
+ </div>
+ </div>`;
this.fluidbook.menu.openCustomView(view, 'cart-kimplay-sendcart');
},
sendRequest: function() {
const $this = this;
+ const form = document.getElementById("kimplay-sendcart-form");
+ const formData = new FormData(form);
+ formData.append('cart_items', JSON.stringify($this.getItems()))
$.ajax({
url: $this.fluidbook.service.getBaseURL(true) + 'kimplay',
cache: false,
- data: {products: $this.getItems()},
+ data: formData,
+ processData: false,
+ contentType: false,
method: 'post',
xhrFields: {withCredentials: true},
- dataType: 'json',
success: function () {
+ $this.emptyCart();
$this.updateTitle('Merci !')
$this.updateCartContent($this.getConfirm())
$this.fluidbook.resize.resize();
- }
+ },
+ error: function (xhr, status, error) {
+ let errorsMessage = JSON.parse(xhr.responseText);
+ $this.displayErrors(errorsMessage)
+ },
});
- }
+ },
+
+ displayErrors: function (errors) {
+ $(".error").removeClass("error")
+ for (let k in errors['errors']) {
+ $("#kimplay-sendcart input[name=" + k + "]").val("")
+ $("#kimplay-sendcart label[for=" + k + "]").addClass('error')
+ $("#kimplay-sendcart input[name=" + k + "]")[0].placeholder = `${errors['errors'][k][0]}`
+ }
+ },
};
\ No newline at end of file