在Woocommerce结帐页面中重置先前选择的送货方式 [英] Reset previous chosen shipping method in Woocommerce checkout page

查看:51
本文介绍了在Woocommerce结帐页面中重置先前选择的送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用此过滤器清除默认的运送选择方法:

Currently, I am clearing the default shipping selection method using this filter:

add_filter( 'woocommerce_shipping_chosen_method', '__return_false', 99);

但是,这仅在客户的初始会话期间将其清除.一旦客户选择了一个选项,便会记住将来的选择.

However, this only clears it during the initial session of the customer. Once the customer chooses an option even once, it remembers the selection for the future.

我正在尝试获取结帐单,以迫使客户即使在同一会话中每次访问结帐单,也要选择运输选项.每次结帐页面加载后是否可以运行此过滤器?

I am trying to get the checkout to force the customer to pick a shipping option every time they visit the checkout even in the same session. Is it possible to run this filter every time the checkout page is loaded?

推荐答案

您可以使用结帐页面(针对已登录的客户)重置上次选择的送货方式:

You can reset the last chosen shipping method using in checkout page (for logged in customers):

 delete_user_meta( get_current_user_id(), 'shipping_method' );

还要从会话数据中删除所选的送货方式:

And also remove the chosen shipping method from session data:

WC()->session->__unset( 'chosen_shipping_methods' );

在一个类似钩子的函数中:

In a hooked function like:

add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
    if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in() 
    && get_user_meta( get_current_user_id(), 'shipping_method', true ) ) {
        delete_user_meta( get_current_user_id(), 'shipping_method' );
        WC()->session->__unset( 'chosen_shipping_methods' );
    }
}


或者您也可以在结帐页面上为所有人设置默认的送货方式:


Or you can also set a default shipping method for everybody in checkout page:

add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
    if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in() ) {
        WC()->session->set( 'chosen_shipping_methods', array('flat_rate:14') );
    }
}

要了解要使用的送货方式费率编号,您可以使用浏览器检查器检查购物车或结帐页面中的送货方式单选按钮,例如:

To find out the shipping methods rate Ids to be used, you can inspect the shipping method radio buttons in cart or checkout pages, with your browser inspector like:

代码在您的活动子主题(或活动主题)的function.php文件上.应该可以.

Code goes on function.php file of your active child theme (or active theme). It should works.

这篇关于在Woocommerce结帐页面中重置先前选择的送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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