当访问特定页面(在WordPress之外)自定义PHP页面时,如何清除woocommerce购物车 [英] How to clear the woocommerce cart when visiting the particular page (outside of wordpress) custom php page

查看:63
本文介绍了当访问特定页面(在WordPress之外)自定义PHP页面时,如何清除woocommerce购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有woocommerce商店,例如:example.com/store/

I am having the woocommerce store like : example.com/store/

我正在wordpress之外创建一些产品销售页面:example.com/upsell.php

And I am creating the some product sales page in outside of wordpress : example.com/upsell.php

现在,我想在您访问example.com/upsell.php后清除购物车,因为我们在加售中有多个步骤,最后我们发送了一个url请求,以将产品添加到购物车(example.com/store/cart/?add-to-cart = 1,5,8).

Now I want to clear the cart once you visit the example.com/upsell.php, because we are having multiple step in the upsell and finally we are send a url request to add the products in the cart (example.com/store/cart/?add-to-cart=1,5,8).

无论何时您访问加价页面,我们都需要清除购物车会话.

Whenever you visit the upsell page we need to clear the cart session.

如何从加售页面清除购物车会话?

How can clear the cart session from the upsell page?

推荐答案

您需要添加一个操作,该操作将清除模板重定向挂钩中的购物车项目.

You need to add an action that will clear cart items in template redirect hook.

在自定义功能中,检查当前页头&然后根据我们的条件清除购物车.

In the custom function, check the current page slug & then clear the cart as per our condition.

在主题的 functions.php 或自定义插件文件中使用以下代码段.

Use the below code snippet in your theme's functions.php or custom plugin file.

add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
    global $post;
    $slug = $post->post_name;
    if($slug == 'sample-page') {
        global $woocommerce;
        $woocommerce->cart->empty_cart();
    }
}

更新

如果您不喜欢对页塞进行硬编码,那么还有一种更好的方法.

If you don't like hard coding the page slug, there is also a better method.

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
    if ( isset( $_GET['clear-cart'] ) ) {
        global $woocommerce;
        $woocommerce->cart->empty_cart();
    }
}

在主题的 functions.php 文件中添加以上代码.

Add the above code in your theme's functions.php file.

然后通过在您的URL&中添加 clear-cart 查询字符串来重定向到页面这将清除所有购物车中的物品.

Then redirect to a page by adding clear-cart query string in your URL & that will clear all cart items.

我们可以在任何URL中使用此功能.

We can use this function in any URL.

http://example.com?clear-cart

http://example.com/sample-page/?clear-cart

这篇关于当访问特定页面(在WordPress之外)自定义PHP页面时,如何清除woocommerce购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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