I need help please. I have the following ajax request in the front
jQuery(document).ready(function($) {
$('#pagoboton').click(function() {
var product_id = 14589; // Reemplaza con el ID del producto que deseas cotizar
var product_price = 200; // Reemplaza con el precio modificado que desees - new price
var ajaxurl = '<?php echo home_url('wp-admin/admin-ajax.php'); ?>';
$.post( ajaxurl, {
action: 'cotizar_producto',
product_id: product_id,
product_price: product_price
}, function(response){
window.location.href = response.redirect_url;
});
});
});
And in the functions.php file i have
add_action('wp_ajax_cotizar_producto', 'cotizar_producto');
add_action('wp_ajax_nopriv_cotizar_producto', 'cotizar_producto');
function cotizar_producto() {
// Obtener los datos del formulario
$product_id = $_REQUEST['product_id'];
$product_price = floatval($_REQUEST['product_price']); // Convertir el precio a nĂºmero decimal
WC()->cart->add_to_cart($product_id, 1, $variation_id = 0, array(), array('total' => $product_price));
add_action('woocommerce_before_calculate_totals', function ($cart) use ($product_id, $product_price) {
//if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
// Verificar si es el producto que deseas modificar
if ($cart_item['product_id'] == $product_id) {
// Define el nuevo precio que deseas asignar
$nuevo_precio = $product_price;
// Establece el nuevo precio para el producto
$cart_item['data']->set_price($nuevo_precio);
// Actualiza el carrito
$cart->cart_contents[$cart_item_key] = $cart_item;
}
}
});
wp_die();
//here i need the code to redirect to the payment page
}
I tested, but not worked, I was hoping that by clicking on the button with id = #pagoboton a specific product could be added to the cart, changing its price and redirecting it to the payment page