jQuery(document).ready(function ($) { // Autocomplete de produtos $("#wpw_product_search").autocomplete({ source: function (request, response) { $.ajax({ url: wpw_ajax_obj.ajax_url, dataType: "json", data: { action: "wpw_product_search", term: request.term, security: wpw_ajax_obj.security }, success: function (data) { response($.map(data, function (item) { return { label: item.text, value: item.text, id: item.id }; })); } }); }, minLength: 2, select: function (event, ui) { // Ao selecionar produto, buscar preço via ajax e adicionar no carrinho temporário $.ajax({ url: wpw_ajax_obj.ajax_url, dataType: "json", data: { action: "wpw_get_product_price", product_id: ui.item.id, security: wpw_ajax_obj.security }, success: function (res) { if (res.success) { addToCart(ui.item.id, ui.item.value, res.data.raw_price); $("#wpw_product_search").val(''); } } }); return false; } }); let cart = []; function updateCartHtml() { let $list = $("#wpw_cart_items"); $list.empty(); if (cart.length === 0) { $list.html('

Carrinho vazio.

'); updateTotal(); return; } cart.forEach((item, index) => { let line = $('
'); line.append('
' + item.name + '
'); line.append('
Qtd:
'); line.append('
' + wpw_ajax_obj.currency_symbol + (item.price.toFixed(2)) + '
'); line.append('
'); $list.append(line); }); updateTotal(); } function updateTotal() { let total = 0; cart.forEach(item => { total += item.price * item.quantity; }); $(".wpw-total-value").text(wpw_ajax_obj.currency_symbol + total.toFixed(2)); } function addToCart(id, name, price) { let found = cart.find(item => item.id === id); if (found) { found.quantity++; } else { cart.push({ id: id, name: name, price: price, quantity: 1 }); } updateCartHtml(); } $("#wpw_cart_items").on('change', '.wpw-qty', function () { let index = $(this).data('index'); let val = parseInt($(this).val()); if (val < 1) val = 1; cart[index].quantity = val; updateCartHtml(); }); $("#wpw_cart_items").on('click', '.wpw-remove-btn', function () { let index = $(this).data('index'); cart.splice(index, 1); updateCartHtml(); }); // Atualizar troco ao alterar valor recebido $("#wpw_payment_received").on('input', function () { let received = parseFloat($(this).val()); if (isNaN(received)) received = 0; let total = 0; cart.forEach(item => total += item.price * item.quantity); let change = received - total; if (change < 0) change = 0; $("#wpw_change").text(change.toFixed(2)); }); // Finalizar pedido $("#wpw_finalize_order").on('click', function () { if (cart.length === 0) { alert('Carrinho vazio.'); return; } let payment_received = parseFloat($("#wpw_payment_received").val()); if (isNaN(payment_received)) payment_received = 0; $.ajax({ url: wpw_ajax_obj.ajax_url, method: 'POST', dataType: 'json', data: { action: 'wpw_finalize_order', security: wpw_ajax_obj.security, customer_name: $("#wpw_customer_name").val(), cart_items: cart, payment_received: payment_received, }, success: function (res) { if (res.success) { $("#wpw_order_message").html('

' + res.data.message + ' Troco: ' + wpw_ajax_obj.currency_symbol + res.data.change + '

'); cart = []; updateCartHtml(); $("#wpw_payment_received").val('0'); $("#wpw_change").text('0.00'); } else { $("#wpw_order_message").html('

' + res.data + '

'); } }, error: function () { $("#wpw_order_message").html('

Erro ao finalizar pedido.

'); } }); }); updateCartHtml(); });