ajax удаление товара по id из корзины
$(document).on('click', 'a.remove.package', function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'http://letique.in.ua/wp-admin/admin-ajax.php',
data: {action : 'remove_item_from_cart','product_id' : '810'},
success: function (res) {
if (res) {
alert('Removed Successfully');
}
}
});
function.php
function remove_item_from_cart() {
$cart = WC()->instance()->cart;
$id = $_POST['product_id'];
$cart_id = $cart->generate_cart_id($id);
$cart_item_id = $cart->find_product_in_cart($cart_id);
if($cart_item_id){
$cart->set_quantity($cart_item_id, 0);
return true;
}
return false;
}
add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');
ajax добавление товара
functions.php
function custom_add_to_card() {
global $woocommerce;
$woocommerce->cart->add_to_cart( $product_id = $_POST['prod_id'], $quantity = $_POST['prod_qty'], $variation_id = 0, $variation = array(), $cart_item_data = array() );
echo 'done';
wp_die();
}
add_action('wp_ajax_addtocard', 'custom_add_to_card');
add_action('wp_ajax_nopriv_addtocard', 'custom_add_to_card');
js
$(document).on('click', '.ajax_add_to_cart', function (e) { e.preventDefault();
var data = {
'action': 'addtocard',
'prod_id': $(this).attr('data-product_id'),
'prod_qty': 1,
'dataType': 'json'
};
$.ajax({
url: "https://letique.in.ua/wp-admin/admin-ajax.php",
data: data,
type: 'POST',
success: function (data) {
console.log(data);
alert("good");
}
}); });