在订单中添加其他详细信息。 Woocommerce

时间:2016-09-09 07:00:54

标签: wordpress woocommerce

我在checkout page上添加了下拉列表,我想将下拉列表值添加到订单明细中。我试图将值更新为元数据,但我的代码不起作用。

这是我的代码:(在主题上)

     <from>
     <select class = "drop-down-list" name = "drop-down-list"  id="drop-down-list" >
    <?php 
      $age = array(..,...,...);
      $wp_user_query = new WP_User_Query($args);

     // Get the results
     $authors = $wp_user_query->get_results();

     // Check for results
     if (!empty($authors)) {

     // loop through each author
     foreach ($authors as $author)
     {
        // get all the user's data
        $author_info = get_userdata($author->ID);

        //Print out this <option value ='myName'>

        echo '<option value =\'' . $author_info->display_name . '\'>' 
          . $author_info->display_name . '</option>';
    }

    } else {
       echo 'No authors found';
    }

?>
</select>
</from>

这是我的插件:

add_action( 'woocommerce_checkout_order_processed', 'my_custom_checkout_field_update_order_meta_lmc' );

function my_custom_checkout_field_update_order_meta_lmc_ao($post_id) {
    global $woocommerce, $post;
    $order = new WC_Order($post_id);

    //to escape # from order id
    echo 'test this function';
    //$order_id = trim(str_replace('#', '', $order->get_order_number()));
    if ( ! empty( $_POST['drop-down-list'] ) ) {
       update_post_meta( $order_id, 'drop-down-list', sanitize_text_field(     $_POST['drop-down-list'] ) );

    }
}
 do_action('woocommerce_checkout_order_processed');

我使用了这些代码,但数据没有更新。我尝试将所有代码放在同一个文件中,但它仍然无效。我怎么能这样做?

这是<select> enter image description here

测试功能: enter image description here

由于

1 个答案:

答案 0 :(得分:1)

我已经修改了你的插件代码了一下。试试这个:

add_action( 'woocommerce_checkout_order_processed', 'my_custom_checkout_field_update_order_meta_lmc', 10, 2 );
function my_custom_checkout_field_update_order_meta_lmc_ao($post_id, $posted) {
    global $woocommerce, $post;
    $order = wc_get_order( $post_id );

    //to escape # from order id

    //$order_id = trim(str_replace('#', '', $order->get_order_number()));
    if ( ! empty( $posted['drop-down-list'] ) ) {
       update_post_meta( $order->id, 'drop-down-list', sanitize_text_field( $posted['drop-down-list'] ) );

    }
}

还有一件事,您使用<from>...</from>代替<form>...</form>。如果您的下拉框显示在结帐表单中,则无需<form>代码。