如何清除废弃的 woocommerce 购物车 [英] How to clear an abandoned woocommerce cart

查看:37
本文介绍了如何清除废弃的 woocommerce 购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了此代码,用于在某些页面加载时清除 Woocommerce 购物车.

但是,我想知道有没有办法在购物车被放弃后清除它?

<块引用>

为了仅在首页触发,您的函数需要如下所示:

add_action( 'init', 'woocommerce_clear_cart_url' );函数 woocommerce_clear_cart_url() {全球 $woocommerce;if ( is_front_page() && isset( $_GET['empty-cart'] ) ) {$woocommerce->cart->empty_cart();}

<块引用>

}函数 is_front_page() 仅在您的 wordpress 网站的首页上返回 true.此外,您可以使用函数 is_page() 检测任何其他页面,您可以在其中传递任何页面标题、ID 或 slug

解决方案

来自这个问题:设置 WooCommerce 购物车到期时间

<块引用>

据我所知,WooCommerce 2.0.20 有一个计划维护作业,每天运行两次,将从 WordPress 选项表中删除任何购物车会话.默认过期时间设置为从用户第一次创建购物车的时间起 48 小时.我猜您的标准 WordPress 调度例程(和服务器 cron/at 作业)需要正常运行才能执行.

AFAIK 无法通过设置调整 48 小时规则.您可以在主题或相邻"文件中编写过滤器.插件.

我稍微调整了代码以切换到 24 小时会话.我不确定您是否希望每隔几分钟删除一次,因为这可能会影响性能.

add_filter('wc_session_expiring', 'so_26545001_filter_session_expiring');函数 so_26545001_filter_session_expiring($seconds) {返回 60 * 60 * 23;//23 小时}add_filter('wc_session_expiration', 'so_26545001_filter_session_expired');函数 so_26545001_filter_session_expired($seconds) {返回 60 * 60 * 24;//24小时}

I came across this code for clearing a Woocommerce cart on certain page loads.

But, I wonder is there a way to clear the cart after it has been abandoned?

For triggering only on front page your function needs to look like this:

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ( is_front_page() && isset( $_GET['empty-cart'] ) ) { 
    $woocommerce->cart->empty_cart(); 
}

} function is_front_page() returns true only on front page of your wordpress site. Also, you might detect any other page with function is_page() where you can pass any page title, ID or slug

解决方案

From this question: Set WooCommerce Cart Expiration

From what I can see, WooCommerce 2.0.20 has a scheduled maintenance job that runs twice/day that will remove any cart sessions from the WordPress options table. The default expiration time is set to 48 hours from the time the user first created the cart. I'm guessing your standard WordPress scheduling routines (and server cron/at jobs) will need to be running properly for this to execute.

AFAIK there is no way to adjust the 48 hour rule via settings. You could write a filter in your theme or in an "adjacent" plugin.

I've adjusted the code a little bit to switch to a 24 hour session. I'm not sure you want to be deleting every few minutes as that has the potential to be performance-heavy.

add_filter('wc_session_expiring', 'so_26545001_filter_session_expiring' );

function so_26545001_filter_session_expiring($seconds) {
    return 60 * 60 * 23; // 23 hours
}

add_filter('wc_session_expiration', 'so_26545001_filter_session_expired' );

function so_26545001_filter_session_expired($seconds) {
    return 60 * 60 * 24; // 24 hours
}

这篇关于如何清除废弃的 woocommerce 购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆