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

0

Make Woocommerce product not purchasable

Added: 1 year ago

Last Updated: 1 year ago

If you have items that can't be sold (for instance a overview of companies) disable the possibility of purchasing.

WooCommerce

AI Verified

0

WooCommerce Product Price Display with Offer Validity

Added: 7 months ago

Last Updated: 7 months ago

How long is the discounted price of one of your products valid? This code checks if the product is being viewed on a single page and has a sale end date. If true, it adds a message indicating the offe...

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 4 days ago

Deactivate some WooCommerce Checkout Fields from showing

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

31

Remove Unused Javascript

Added: 1 year ago

Last Updated: 1 month 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: 4 days ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 7 months ago

Last Updated: 1 month ago

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