在Woocommerce管理订单编辑页面中以编程方式添加自定义订单

时间:2018-02-07 15:24:18

标签: php wordpress woocommerce backend orders

在woocommerce中,我试图通过php在管理订单编辑页面中添加自定义订单备注(以编程方式)。我还没找到路。

任何帮助将不胜感激。

WooCommerce order note in the order admin page

3 个答案:

答案 0 :(得分:10)

从动态订单ID中,您可以这样使用WC_Order add_order_note()方法:

// If you don't have the WC_Order object (from a dynamic $order_id)
$order = wc_get_order(  $order_id );

// The text for the note
$note = __("This is my note's text…");

// Add the note
$order->add_order_note( $note );

经过测试和工作。

答案 1 :(得分:1)

谢谢你们,我试图找到一种方法来将新笔记添加到新订单中。我正在寻找使用@LoicTheAztec发布的解决方案的正确钩子。这个解决方案对我有用,希望能帮助其他人。

将其添加到Functions.php文件

add_action( 'woocommerce_new_order', 'add_engraving_notes',  1, 1  );

function add_engraving_notes( $order_id ) {
 //note this line is different 
 //because I already have the ID from the hook I am using.
 $order = new WC_Order( $order_id ); 

 // The text for the note
 $note = __("Custom Order Note Here");

 // Add the note
 $order->add_order_note( $note );

 // Save the data
 $order->save();
}

答案 2 :(得分:0)

此代码将为您在functions.php

中添加代码提供技巧

                    add_action('woocommerce_after_order_notes', 'customise_checkout_field');

                    function customise_checkout_field($checkout)
                    {
                    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
                    woocommerce_form_field('customised_field_name', array(
                    'type' => 'text',
                    'class' => array(
                    'my-field-class form-row-wide'
                    ) ,
                    'label' => __('Customise Additional Field') ,
                    'placeholder' => __('Guidence') ,
                    'required' => true,
                    ) , $checkout->get_value('customised_field_name'));
                    echo '</div>';
                    }

对于自定义字段的数据验证,请使用下面给出的代码:

                    add_action('woocommerce_checkout_process', 'customise_checkout_field_process');

                    function customise_checkout_field_process()
                    {
                        // if the field is set, if not then show an error message.
                        if (!$_POST['customised_field_name']) wc_add_notice(__('Please enter value.') , 'error');
                    }

更新字段值

                    add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

                    function customise_checkout_field_update_order_meta($order_id)
                    {
                        if (!empty($_POST['customised_field_name'])) {
                            update_post_meta($order_id, 'Some Field', sanitize_text_field($_POST['customised_field_name']));
                        }
                    }
相关问题