如何从购物车页面中的输入字段获取价值到管理订单详细信息页面?

问题描述 投票:2回答:2

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

如何在订单详细信息页面中获取值,并且还希望将该详细信息保存在数据库中。

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

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

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

woocommerce hook-woocommerce woocommerce-rest-api woocommerce-theming
2个回答
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').':&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文件中。它应该现在有效。


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

所以你可以交替尝试:

// 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文件中。它应该现在有效。


在后端的Order页面中,您将获得如下内容:

enter image description here


要从$order检索数据,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
投票

您可以使用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' );
     }

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

它们将显示在管理部分的订单屏幕底部

© www.soinside.com 2019 - 2024. All rights reserved.