jQuery(function($) { $(document).on('click', '.vh-buy-now', function(e) { e.preventDefault(); const button = $(this); const productID = button.data('product-id'); if (!productID) { return; } button.addClass('loading'); $.ajax({ type: 'POST', url: wc_add_to_cart_params.wc_ajax_url .replace('%%endpoint%%', 'add_to_cart'), data: { product_id: productID, quantity: 1 }, success: function(response) { if (response.error && response.product_url) { window.location.href = response.product_url; return; } // Cập nhật fragment giỏ hàng của WooCommerce $(document.body).trigger('added_to_cart', [ response.fragments, response.cart_hash, button ]); // Đi thẳng Checkout window.location.href = wc_checkout_params.checkout_url; }, error: function() { alert('Không thể thêm sản phẩm vào giỏ hàng.'); }, complete: function() { button.removeClass('loading'); } }); }); });