I'm trying to clear the cart on home page. I've added this part to home page head section:
<script type='text/javascript'>
function clearCart() {
jQuery.post(
"https://abcdefgh.com/wp-admin/admin-ajax.php",
//ajaxurl,
{
"action": "clearcart"
}
);
console.log('its homepage bro!');
}
jQuery(document).ready(function(){
clearCart();
});
</script>
And, added this part to functions.php:
add_action('wp_ajax_nopriv_clearcart',function(){
global $woocommerce;
$woocommerce->cart->empty_cart(true);
});
If the user is not logged in, the cart is cleared on the home page. But, if the user is logged in, it does not work although the javascript part is executed (I checked via console log.).
Why do you think it happens and how can I resolve it?