Это код HTML (и JavaScript), который я использую:
Код: Выделить всё
✅
function f1(){
// const cartItems=[9660, 9568, 8625]
const cartItems=9568
const data = {
action: 'AddtocartF',
ProductsToPay: JSON.stringify(cartItems)
};
fetch('https://9effa.ma/wp-admin/admin-ajax.php', {
method: 'POST',
body: new URLSearchParams(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
})
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => {
console.error('Error:', error);
});
}
Код: Выделить всё
add_action('wp_ajax_AddtocartF', 'AddtocartFunction');
add_action('wp_ajax_nopriv_AddtocartF', 'AddtocartFunction');
function AddtocartFunction(){
if (!isset($_POST['ProductsToPay'])) {
wp_send_json_error(['message' => "Veuillez ajouter les produits au panier avant de passer au paiement."]);
}
$product_id = $_POST['ProductsToPay'];
$quantity = 1;
$WC_Cart = new WC_Cart();
$result = $WC_Cart->add_to_cart($product_id, $quantity);
if (is_wp_error($result)) {
// Handle the error case
wp_send_json_error(['message' => $result->get_error_message()]);
} else {
// Product added successfully
$response = ['success' => true, 'message' => 'Product added to cart', 'id' => $product_id, 'result value: ' => $result];
wp_send_json_success($response);
}
wp_die();
}
Вот сообщение, которое я получаю в консоли:
Код: Выделить всё
{
"success": true,
"data": {
"success": true,
"message": "Product added to cart",
"id": "9568",
"result value: ": "621d187a8e1a345cc07422a61c669654"
}
}
Я пробовал отключить все плагины, кроме Elementor и WooCommerce.
Я протестировал множество разных идентификаторов продуктов.
Подробнее здесь: https://stackoverflow.com/questions/786 ... lity-issue
Мобильная версия