在购物车页面中,我已经创建了两个文本字段,但是无法获取用户在该字段中输入的值以进入管理订单详细信息页面。
如何获取订单详细信息页面中的值,我还希望将该详细信息保存在数据库中。
下面是我的购物车页面的代码
<?php
do_action( 'woocommerce_before_cart' ); ?>
<section class="checkout_display">
<div class="container">
<div class="row">
<form action="<?php echo esc_url( wc_get_checkout_url() );?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="col-lg-6 col-md-8 col-sm-12 inset">
<div class="checkout_title">get started</div>
<div class="first_form">
<!--Code which display text field one -->
<div class="form-group" >
<label>Instagram username</label>
<input type="text" name="igusername" required>
</div>
<!--Code which display text field second -->
<div class="form-group">
<label>Email</label>
<input type="email" name="useremail" required>
</div>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<div class="form-group">
<label>Your package</label>
<select disabled>
<option> <?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );?> For
<span><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</span>
</option>
</select>
</div>
<div class="checkbox"><input type="checkbox" /> Yes! send me special promotion and discounts</div>
<div class="btn">
<input type="submit" value="next">
</a>
</div>
<?php
}
}
?>
<?php do_action( 'woocommerce_cart_contents' ); ?>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_after_cart_table' ); ?>
</form>
</div>
</div>
</section>
<?php do_action( 'woocommerce_after_cart' ); ?>
显示文本字段一和二的代码是我想从中获取用户输入值并希望在付款后将其显示并存储在管理员订单详细信息页面中的文本框
答案 0 :(得分:3)
尝试以下操作,以显示您在Woocommerce会话中发布的字段值。下订单后,它将把该自定义会话数据另存为自定义订单元数据,并在管理订单中显示:
// Save the posted data to Woocommerce session
add_filter( 'init', 'set_instagram_posted_data_to_wc_sessions', 10, 3 );
function set_instagram_posted_data_to_wc_sessions() {
if ( ( is_cart() || is_checkout() ) && isset($_POST['igusername']) && isset($_POST['useremail']) ) {
// Enable Woocommerce sessions (if not done yet)
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
$session_data = []; // initializing
if( isset($_POST['igusername']) && ! empty($_POST['igusername']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_username'] = sanitize_text_field($_POST['igusername']);
}
if( isset($_POST['useremail']) && ! empty($_POST['useremail']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_useremail'] = sanitize_email($_POST['useremail']);
}
// Set the data to custom wc_sessions
if( sizeof($session_data) > 0 ) {
WC()->session->set('ig_data', $session_data);
}
}
}
// Save the session data as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if( $session_data = WC()->session->get('ig_data') ) {
$order->update_meta_data( '_ig_username', wc_clean($session_data['ig_username']) );
$order->update_meta_data( '_ig_useremail', wc_clean($session_data['ig_useremail']) );
// remove the data from Woocommerce session
WC()->session->__unset('ig_data'):
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。现在应该可以使用了。
如果不适用于会话,则可以使用以下命令将发布数据添加到结帐页面的隐藏字段中,并在提交订单时再次发布该数据……其他与上面相同……
因此您可以尝试:
// Display the posted data values in checkout hidden fields
add_filter( 'woocommerce_after_checkout_billing_form', 'set_instagram_posted_data_in_hidden_field', 10, 3 );
function set_instagram_posted_data_in_hidden_field() {
if ( isset($_REQUEST['igusername'])|| isset($_REQUEST['useremail']) ) {
// Display hidden fields with the Instagram posted values
?><input type="hidden" name="ig_username" value="<?php echo $_REQUEST['igusername']; ?>">
<input type="hidden" name="ig_useremail" value="<?php echo $_REQUEST['useremail']; ?>"><?php
}
}
// Save checkout hidden fields values as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['ig_username']) {
$order->update_meta_data( '_ig_username', sanitize_text_field($_POST['ig_username']) );
}
if ( isset($_POST['ig_useremail']) {
$order->update_meta_data( '_ig_useremail', sanitize_email($session_data['ig_useremail']) );
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。现在应该可以使用了。
在后端的“订购”页面中,您将获得类似以下内容的信息:
要从$order
对象{em>(或WC_Order
订单ID)的$order_id
中检索数据,请使用:
$order = wc_get_order( $order_id ); // (optionally if required) with the Order ID
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
答案 1 :(得分:1)
您可以使用order meta将任何内容保存到特定订单。
add_action('woocommerce_checkout_create_order',
'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data )
{
$order->update_meta_data( '_custom_text1', 'value' );
$order->update_meta_data( '_custom_tex2', 'value' );
}
将您的文本框值添加到meta键的值部分,保存订单时它们将存储在数据库中。
它们将出现在“管理”部分的订单屏幕底部