在 WooCommerce 4 中设置购物车过期间隔
Posted
技术标签:
【中文标题】在 WooCommerce 4 中设置购物车过期间隔【英文标题】:Set Cart Expiration Interval in WooCommerce 4 【发布时间】:2021-04-29 02:33:17 【问题描述】:那么,我如何在 woocommerce 中过期并清空购物车,因为过滤器根本不这样做,只会在给定的几分钟内增加会话,老实说,我不知道到底在哪里使用或是否以任何方式清除购物车?
有这个:
add_filter( 'wc_session_expiring', 'wc_custom_session_expiring' );
add_filter( 'wc_session_expiration', 'wc_custom_session_expiring' );
function wc_custom_session_expiring( $expiry )
return 300; // 5 mins in seconds
【问题讨论】:
【参考方案1】:下面,你有我为客户编写的代码,只是为了节省你们一些时间,如果“延迟”给定时间没有过去,这里发布的代码会更新过期时间,并且用户仍然活跃/在商店买东西。如果用户停止点击页面超过 5 分钟(您可以更改代码),他在商店中的下一次点击,或下一个加载的页面,购物车将被清空。
我正在使用 ReflectionClass 从会话类中快速读取一些受保护的值,就在过滤器的帮助下更新过期值之后。
请注意此插件使用$_SESSION
,请随意修改并在此处发布您自己的版本!
请注意这一点:$minutes = get_option( 'expire_option', '5' );
,我为此使用了管理表单字段(选项)设置,请随意添加您自己的。
<?php
/**
* Plugin Name: Woocommerce Expire Cart
* Plugin URI: http://webake.ro
* Description: Expire & clear cart after 5 minutes (default) , or use expire_option field if set !!
* Version: 2.0
* Author: Alin Razvan
* Author URI: https://webake.ro
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
session_start();
# https://***.com/questions/20334355/how-to-get-protected-property-of-object-in-php
function accessProtected($obj, $prop)
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($prop);
$property->setAccessible(true);
return $property->getValue($obj);
/*
SET Cart expire after 5 minutes ,
*/
add_filter( 'wc_session_expiring', 'filter_wc_session_expiring', 998, 1 );
add_filter( 'wc_session_expiration', 'filter_wc_session_expiration', 999, 1 );
function filter_wc_session_expiring()
// get expire_option minutes time !
$minutes = get_option( 'expire_option', '5' );
if(!$minutes)$expiring_time = "300"; /* 5 mins in seconds */
else $expiring_time = ($minutes * 60);
return $expiring_time;
function filter_wc_session_expiration()
// get expire_option minutes time !
$minutes = get_option( 'expire_option', '5' );
if(!$minutes)$expiring_time = "300"; /* 5 mins in seconds */
else $expiring_time = ($minutes * 60);
return $expiring_time;
/*
Since the cart is not going to empty ,
because this is most likely a woocommerce bugg , we do this to fix it !
*/
add_action('wp_loaded','custom_expire_cart_loader');
function custom_expire_cart_loader()
global $woocommerce;
// if is not admin panel , execute only on frontend !
if(isset($woocommerce)&&!is_admin())
$current_user_id = get_current_user_id();
$gtsession = $woocommerce->session;
$info = $gtsession->get_session($current_user_id, $default = false );
//-------------------------------------
# dirty ,i know , but no other way for quick solution
$session_expiring = accessProtected($gtsession, '_session_expiring');
$session_expiration = accessProtected($gtsession, '_session_expiration');
//-------------------------------------
if(!isset($_SESSION['lucky_expire_cart']))
$_SESSION['lucky_expire_cart'] = $session_expiring;
$lucky_cart_session = $session_expiring;
else
$lucky_cart_session = $_SESSION['lucky_expire_cart'];
#echo "Now:".date('Y-m-d H:i:s')."\n<br/>";
//echo "Session expiration:".date('Y-m-d H:i:s',$session_expiration)."\n<br/>";
#echo "Expiring:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
#echo "Expire saved compare:".date('Y-m-d H:i:s',$lucky_cart_session)."\n<br/>";
# Expired due to delay in action (page click ,page refresh etc)
# ex: 5 mins passed , expire session
if($lucky_cart_session<=time())
# -------- This empties the cart ! -----------
WC()->cart->empty_cart(true);
# --------------------------------------------
#echo "Is expired now !";
unset($_SESSION['lucky_expire_cart']);
$_SESSION['lucky_expire_cart'] = $session_expiring;
#echo "Expire new set to:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
# Not yet expired by delay ,
# we do update the expire time to new one
else
unset($_SESSION['lucky_expire_cart']);
$_SESSION['lucky_expire_cart'] = $session_expiring;
#echo "Not yet expired by delay ! \n<br/>";
#echo "Expire saved updated to:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
?>
【讨论】:
以上是关于在 WooCommerce 4 中设置购物车过期间隔的主要内容,如果未能解决你的问题,请参考以下文章
在.htaccess中设置Expires Header对于Woocommerce网站来说是一个糟糕的主意吗?
根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关