{"id":29637,"date":"2023-03-17T14:23:18","date_gmt":"2023-03-17T13:23:18","guid":{"rendered":"https:\/\/alessandrobuzzi.com\/?p=29637"},"modified":"2023-03-17T14:23:18","modified_gmt":"2023-03-17T13:23:18","slug":"how-to-clear-a-woocommerce-cart-programmatically","status":"publish","type":"post","link":"https:\/\/alessandrobuzzi.com\/?p=29637","title":{"rendered":"How to clear a Woocommerce cart programmatically"},"content":{"rendered":"<p>A Simple function to clear woocommerce cart after 5 hours<\/p>\n<p>[php]<\/p>\n<p>function clear_woocommerce_cart_on_login() {<br \/>\nif ( is_user_logged_in() &amp;&amp; !WC()-&gt;cart-&gt;is_empty() ) {<br \/>\n$cart_age = current_time( &#8216;timestamp&#8217; ) &#8211; WC()-&gt;session-&gt;get( &#8216;cart_created&#8217; );<br \/>\n$hours = floor( $cart_age \/ ( 60 * 60 ) );<\/p>\n<p>if ( $hours &gt;= 5 ) {<br \/>\nWC()-&gt;cart-&gt;empty_cart();<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>add_action( &#8216;wp_login&#8217;, &#8216;clear_woocommerce_cart_on_login&#8217; );<br \/>\n&lt;pre&gt;[\/php]<\/p>\n<p>This function\u00a0checks if a user is logged in using the <code>is_user_logged_in()<\/code> function. If the user is logged in and the cart is not empty, we check the age of the cart and clear it if it is older than 5 hours.<\/p>\n<p>The function is now hooked to the <code>wp_login<\/code> action, which will run only when a user logs in. So the cart will be cleared only when a user logs in, and the cart is older than 5 hours.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Simple function to clear woocommerce cart after 5 hours [php] function clear_woocommerce_cart_on_login() { if ( is_user_logged_in() &amp;&amp; !WC()-&gt;cart-&gt;is_empty() ) { $cart_age = current_time( &#8216;timestamp&#8217; ) &#8211; WC()-&gt;session-&gt;get( &#8216;cart_created&#8217; ); $hours = floor( $cart_age \/ ( 60 * 60 ) ); if ( $hours &gt;= 5 ) { WC()-&gt;cart-&gt;empty_cart(); } } } add_action( &#8216;wp_login&#8217;, &#8216;clear_woocommerce_cart_on_login&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":29511,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-29637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-categorizzato"],"acf":[],"_links":{"self":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29637"}],"version-history":[{"count":0,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29637\/revisions"}],"wp:attachment":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}