5

I want to clear the cart page on page load if this page is not a cart or a checkout page Even for logged in users and admins, any page then it clears. This code was working but its not anymore

/**
 * Clears WC Cart on Page Load
 * (Only when not on cart/checkout page)
 */
 
add_action( 'wp_head', 'bryce_clear_cart' );
function bryce_clear_cart() {
if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
    return;
}
WC()->cart->empty_cart( true );
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Maged Mohamed
  • 511
  • 5
  • 27

2 Answers2

4

Updated and enhanced.

Use WooCommerce conditional tags and try template_redirect hook instead (when cart is not empty):

add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
    if ( ! ( is_cart() || is_checkout() ) && ! WC()->cart->is_empty() ) {
        WC()->cart->empty_cart( true );
    }
}

Code goes on the functions.php file of your active child theme* (or in a plugin). It should work.

*Avoid the parent theme because all your customizations will be removed when the theme will be updated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I edited your answer to work with non logged in users too, i want it to work in both cases if user is logged in or not yet. can you check it ? – Maged Mohamed Mar 20 '19 at 11:03
  • Also i need to know this action trigger each time the user surf the website on any page like viewing the products so each time he click next product to read product details for example it triggers ?, or it only just works (trigger) if he is on the checkout/cart pages and redirect out of it to any other page ?, also does this action work even if the user cart is empty ? .. imagine the website with 10000 user online and surfing the products and their carts are empty so each time they go to another page while viewing the products it will make this action and may be make the website laggy right ? – Maged Mohamed Mar 20 '19 at 12:41
  • I have updated my code (now the active code for this hook will be only executed if cart is not empty on all other pages than cart and checkout)… So no problems now. – LoicTheAztec Mar 21 '19 at 00:29
  • Thank you for the update, but it doesn't work i don't know why so please check your code again, i edited it and it worked but i don't know if it now works fine as the condition to work only executed if cart is not empty on all other pages than cart and checkout) .. check the next answer below – Maged Mohamed Mar 21 '19 at 08:29
0

This works fine, as i said in the comment your code doesn't work i don't know why i just edited it like that it worked but i don't know if it now works fine as the condition to work only executed if cart is not empty on all other pages than cart and checkout)

    add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
    if ( ( ! ( is_cart() || is_checkout() ) ) && ! ( WC()->cart->is_empty() ) ) {
        WC()->cart->empty_cart( true );
}
}

I think there is an issue with this ! ( WC()->cart->is_empty() )

I think this function maybe doesn't work with the operator "!", my code executes the same as yours but i don't know why yours no

Maged Mohamed
  • 511
  • 5
  • 27