php - 如何从购物车页面的输入字段到管理订单详细信息页面获取值(value)?

标签 php wordpress session woocommerce hook-woocommerce

在购物车页面中,我已经创建了两个文本字段,但我无法获取用户在该字段中输入到管理订单详细信息页面的值。

如何获取订单详细信息页面中的值,我还想将该详细信息保存在数据库中。

下面是我的购物车页面的代码

<?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' ); ?>

显示文本字段 1 和 2 的代码是我想从中获取用户输入的值并希望在完成付款后将其显示并存储在管理订单详细信息页面中的文本框

最佳答案

尝试以下将向 Woocommerce session 显示您发布的字段值。下订单时,它会将自定义 session 数据保存为自定义订单元数据,并将其显示在管理订单中:

// 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 ( isset(WC()->session) && ! 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').':&nbsp;</th><td>&nbsp;' . $ig_username . '</td></tr>
            <tr><th align="left">'.__('Email').':&nbsp;</th><td>&nbsp;' . $ig_useremail . '</td></tr>
        </table>
    </div>';

    endif;
}

代码进入事件子主题(或事件主题)的 function.php 文件。它现在应该可以工作了。


如果它不适用于 session ,您可以使用以下方法将发布数据添加到结帐页面的隐藏字段,并在提交订单时再次发布该数据……其他一切都与上面相同……

所以你可以换个方式试试:

// 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').':&nbsp;</th><td>&nbsp;' . $ig_username . '</td></tr>
            <tr><th align="left">'.__('Email').':&nbsp;</th><td>&nbsp;' . $ig_useremail . '</td></tr>
        </table>
    </div>';

    endif;
}

代码进入事件子主题(或事件主题)的 function.php 文件。它现在应该可以工作了。


在后端的订单页面中,您会得到如下内容:

enter image description here


$order WC_Order 对象 (或 $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');

关于php - 如何从购物车页面的输入字段到管理订单详细信息页面获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55619806/

相关文章:

php - 第 5 行 : Call to undefined function link_to_route()

php - 缓存数据与写入内存表

php - session 变量不在页面之间传输

对象数组的PHPDoc类型提示?

php - mysql 列的最大长度并确保我不会使用 utf8_unicode_ci - PHP 超过该限制?

javascript - 从同一 ID 名称分配和获取值

php - 尝试列出产品类别及其产品的价格范围

jquery - 为来自 jQuery 的伪元素应用 CSS 样式

Wordpress 密码保护存档和自定义帖子类型的单个帖子

session - JMeter登录负载测试序列不等待请求返回?