AI Verified

Name

Clear Woo Cart after 5 mins

About

This will clear the Woo Cart after 5 mins of inactivity

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

23/01/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Clear Woo Cart after 5 mins

 
                    
1if ( ! class_exists( 'WC_Session' ) ) {
2 return;
3}
4 
5class WC_Clear_Cart_After_Time {
6 
7 protected $cart_cleared;
8 
9 public function __construct() {
10 $this->cart_cleared = false;
11 add_action( 'init', array( $this, 'clear_cart_after_time' ) );
12 }
13 
14 public function clear_cart_after_time() {
15 if ( ! $this->cart_cleared && is_user_logged_in() ) {
16 $last_activity = get_user_meta( get_current_user_id(), '_wc_last_activity', true );
17 $now = time();
18 
19 if ( ! empty( $last_activity ) && $now - $last_activity >= 300 ) {
20 WC()->cart->empty_cart();
21 $this->cart_cleared = true;
22 }
23 
24 update_user_meta( get_current_user_id(), '_wc_last_activity', $now );
25 } elseif ( ! $this->cart_cleared && ! is_user_logged_in() ) {
26 if ( isset( $_COOKIE['woocommerce_cart_last_activity'] ) ) {
27 $last_activity = $_COOKIE['woocommerce_cart_last_activity'];
28 $now = time();
29 
30 if ( $now - $last_activity >= 300 ) {
31 WC()->cart->empty_cart();
32 $this->cart_cleared = true;
33 }
34 }
35 
36 setcookie( 'woocommerce_cart_last_activity', time(), time() + 3600 * 24 );
37 }
38 }
39}
40 
41new WC_Clear_Cart_After_Time();

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Change In Stock Text

Added: 1 year ago

Last Updated: 2 weeks ago

If you’ve enabled stock management on our store, customers will see the stock status on the single product page. If a product has availability, they’ll see a green happy face and the text “In stock”....

WooCommerce

AI Verified

0

Streamline Your Shopping with Automatic Coupon Code Integration in Your Cart

Added: 8 months ago

Last Updated: 8 months ago

Enhance your shopping experience with seamless savings! We believe in making your store user-friendly to boost order satisfaction. Say goodbye to manual coupon entries – our system now automatically a...

WooCommerce

AI Verified

0

WooCommerce Remove Cross-Sells On Cart Page

Added: 1 year ago

Last Updated: 1 year ago

To remove the cross-sell products which show in the cart, it’s a quick and simple one liner. Typically, cross-sells only show in the cart (using a default theme that is) so removing them here wouldn’t...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

32

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.