加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 以编程方式设置送货方式Woocommerce

发布时间:2020-12-13 16:58:05 所属栏目:PHP教程 来源:网络整理
导读:我刚刚创建了一个id为custom_shipping的自定义Woocommerce运输方法.该选项现在出现在Woocommerce上 – 设置 – 送货管理页面. 我想根据用户的凭据动态设置送货方式.现在默认方法是flat_rate. 题: 如何为满足要求的用户在整个会话期间将送货方式设置为custom
我刚刚创建了一个id为custom_shipping的自定义Woocommerce运输方法.该选项现在出现在Woocommerce上 – >设置 – >送货管理页面.

我想根据用户的凭据动态设置送货方式.现在默认方法是flat_rate.

题:
如何为满足要求的用户在整个会话期间将送货方式设置为custom_shipping?

尝试:

$chosen_methods = $woocommerce->session->set('chosen_shipping_methods',array('purolator_shipping');

这会在我的购物车页面上正确设置会话变量’chosen_shipping_methods’,但在转到结帐时,会话将返回使用flat_rate送货方式.

必须有一个钩子或过滤器,我可以插入以改变购物车会话创建时的运输方法或其他东西. (当用户第一次向购物车添加内容时).

理想情况下,我希望在装载任何其他东西之前设置新的送货方式,以便装运方法在购物车和结帐摘要中看起来正确.

指导赞赏.

解决方法

使用woocommerce_before_checkout_shipping_form挂钩

add_action( 'woocommerce_before_checkout_shipping_form','before_shipping');

function before_shipping( $checkout ) {

    // check the user credentials here and if the condition is met set the shipping method

    WC()->session->set('chosen_shipping_methods',array( 'purolator_shipping' ) );

}

请注意,在购物车页面上,如果用户将送货方式从默认更改为其他方法,则上述代码将再次将其恢复为结帐页面上的默认方法,这对用户来说会有点混乱.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读