【问题标题】:How to Get Value from input fields in cart page to admin order details page?如何从购物车页面中的输入字段获取价值到管理订单详细信息页面?
【发布时间】:2019-09-01 08:10:18
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 在购物车页面显示/保存字段的相关代码在哪里?即使它不起作用,您也应该始终在答案中提供相关代码。没有它,没有人可以帮助你。请记住,人们可以通过魔术猜测事物,而且您应该提供一些上下文......
  • @LoicTheAztec 现在可以请您检查一下吗?

标签: php wordpress session woocommerce hook-woocommerce


【解决方案1】:

您可以使用订单元将任何内容保存到特定订单。

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

将您的文本框值添加到元键的值部分,它们将在保存订单时存储在数据库中。

它们将出现在管理部分的订单屏幕底部

【讨论】:

  • 不工作可以给我完整的代码吗?目前,我将这些名称用于输入字段 iguser、useremail。
【解决方案2】:

尝试以下操作,将您发布的字段值显示到 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 ( 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 文件中。它现在应该可以工作了。


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

所以你可以试试alternatvelly:

// 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 文件中。它现在应该可以工作了。


在后端的订单页面中,你会得到类似的东西:


要从$orderWC_Order 对象(或$order_id 订单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');

【讨论】:

  • 嗨@LoicTheAztec 感谢您的回答,但代码不起作用,因为它在计费部分下没有显示任何内容。我在functions.php中添加了代码,代码中没有单一错误。但它没有显示数据...prntscr.com/nbqain
  • 我的字段工作正常我尝试 $_POST['igusername'];结帐页面中的代码,它工作正常。
  • @Ramansaab 出了点小错误……请再试一次。如果它不起作用,我还有另一种方法。
  • 一切都很好,感谢这段代码,但最后一个用于在管理订单中显示数据的 if 语句不起作用,因为我在没有 if 的情况下尝试过它,它工作正常。你能检查一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
相关资源
最近更新 更多