[英]Saving data to WooCommerce session
I'm attempting to save a simple text input to the WooCommerce session. 我正在尝试将简单的文本输入保存到WooCommerce会话中。 The session is created when a user adds something to their cart. 当用户向购物车添加内容时会创建会话。
My input field exists in custom page template that will be placed in the user flow after the cart but before the checkout: cart > my template > checkout. 我的输入字段存在于自定义页面模板中,该模板将在购物车之后但在结帐之前放置在用户流中:cart> my template> checkout。
Simple form to capture data (custom template file) 用于捕获数据的简单表单(自定义模板文件)
<form name="group" method="post" class="checkout woocommerce-checkout" action="http://localhost:/site.dev/my-template">
<div class="group-order">
<p class="form-row form-row woocommerce-validated" id="create_new_group_field">
<label for="create_new_group" class="">Join an existing group</label>
<input type="text" class="input-text " name="create_new_group" id="create_new_group">
</p>
</div>
</form>
Receiving and setting data (I'm having trouble figuring out when/how to run this.
in my custom page
) 接收和设置数据(我无法确定何时/如何运行此数据。
在我的自定义页面中
)
UPDATE I've added the code below to the top of my page template so the page processes itself and then re-directs to the checkout. 更新我已将下面的代码添加到页面模板的顶部,以便页面自行处理,然后重定向到结帐。
function set_and_save_input_to_session() {
function set_and_save_input_to_session(){
if( !is_admin( ) ) {
// User input
if( ! empty( $_POST['create_new_group'] ) ) {
$group_input_value = $_POST['create_new_group'];
// Set session and save data
WC()->session->set( 'group_order_data', $group_input_value );
wp_redirect( 'http://localhost:28/site.dev/checkout' );
exit();
}
}
get_header();
add_action('woocommerce_checkout_process', 'set_and_save_input_to_session');
add_action('woocommerce_checkout_process','set_and_save_input_to_session');
Retrieving and saving data 检索和保存数据
function retrieve_and_save_group_input_value_to_order_meta() {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
I'm currently working my way through what are to me, more complex solutions and therefore I'd appreciate if anyone could point out any major flaws with my process so far. 我目前正在努力解决对我来说更复杂的解决方案,因此,如果有人能指出我的流程到目前为止存在任何重大缺陷,我将不胜感激。
I can confirm that the form is receiving data and that the WC()->session->set
is setting data. 我可以确认表单正在接收数据,并且WC()->session->set
是设置数据。 (Thanks to @Firefog for suggesting the use the $_SESSION
global) (感谢@Firefog建议使用$_SESSION
全局)
After further investigation and finding the right place to var_dump
the session data I found that the data was being set to the session with my original method. 在进一步调查并找到var_dump
会话数据的正确位置后,我发现数据正在使用我的原始方法设置为会话。
The data is set, but I can't see why the data won't save to the order. 数据已设置,但我无法确定为什么数据不会保存到订单中。
It's more saying Thank you for solving my problem . 更多的是说谢谢你解决我的问题 。 But here is an answer, too: 但这也是一个答案:
The post meta could not been updated because there is no $order_id
parameter in your callback function. 由于回调函数中没有$order_id
参数,因此无法更新post meta。 This should do the trick: 这应该做的伎俩:
function retrieve_and_save_group_input_value_to_order_meta( $order_id ) {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
Here is another approach. 这是另一种方法。
1st page: 第1页:
session_start();//place this at the top of all code
$data = $_POST['create_new_group'];
$_SESSION['custom_create_new_group']=$data;
Now in another page write the following to receive the value: 现在在另一个页面中写下以下内容来接收值:
session_start(); //optional
$retrive_price = $_SESSION['custom_create_new_group'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.