在Woocommerce中将自定义字段添加为订单记录

时间:2018-07-24 08:57:11

标签: php wordpress woocommerce orders user-roles

我正在尝试添加一个自定义字段jckwds_date作为订单记录。我一辈子都无法弄清楚为什么这段代码在functions.php中不起作用?

该代码还仅允许在具有特定角色类型的情况下添加注释。

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $product->get_id(), 'jckwds_date', true );

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}

基本上我需要THIS

THIS

要添加HERE

HERE

6 个答案:

答案 0 :(得分:1)

更新:

以下代码将从订单自定义字段'jckwds_date'(或结帐发布的字段值'jckwds_date')中添加一个订单注释,该订单注释将在后端显示,用于已定义的用户角色:

add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 );
function product_custom_field_to_custom_order_notes( $order_id, $data ){
    // HERE define allowed user roles
    $allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer');

    $user_id = get_post_meta( '_customer_user', 'jckwds_date', true );
    $user = new WP_User( $user_id );

    // Exit if no matched user roles
    if( ! array_intersect( $allowed_roles, $user->roles ) ) return;

    // Get the date custom field (or checkout field)
    if( get_post_meta( $order_id, 'jckwds_date', true ) ){
        $note = get_post_meta( $order_id, 'jckwds_date', true );
    } elseif( isset( $_POST['jckwds_date'] ) ){
        $note = sanitize_text_field( $_POST['jckwds_date'] );
    }

    // The order note
    if( isset($note) && ! empty($note) ){
        $order = wc_get_order( $order_id ); // The WC_Order Object
        $order->add_order_note( $note );  // Add the note
        $order->save(); // Save the order
    }
}

代码进入活动子主题(或活动主题)的function.php文件中。应该可以。

答案 1 :(得分:0)

add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_add_checkout_fields( $fields ) {
     unset($fields['order']['order_comments']);
     $fields['order']['order_note']['priority'] = 5;
     $fields['order']['order_note'] = array(
        'label'     => __('Order Notes', 'woocommerce'),
    'placeholder'   => _x('Order Notes', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );  
     return $fields;
}

答案 2 :(得分:0)

尝试

$order = wc_get_order(  $order_id );


$note = __("This my custom note...");


$order->add_order_note( $note );

$order->save();

答案 3 :(得分:0)

Try this

add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta');

                    function checkout_field_update_order_meta($order_id)
                    {
                        if (!empty($_POST['field_name'])) {
                            update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name']));
                        }
                    }

答案 4 :(得分:0)

尝试此代码。

 add_action( 'woocommerce_thankyou', 'my_note_custom' );

    function my_note_custom( $order_id ) {

          $order = new WC_Order( $order_id );
          $note = __("This my custom note...");
          $order->add_order_note( $note );

          $order->save();

    }

答案 5 :(得分:0)

发现这很简单,只需将$product更改为$order,因为这是我要检索的订单自定义字段值。

下面的完整代码:

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $order->get_id(), 'jckwds_date', true );

        $note = '<b>Wholesale</b>';

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}