【问题标题】:Add a custom order note programmatically in Woocommerce admin order edit pages在 Woocommerce 管理订单编辑页面中以编程方式添加自定义订单备注
【发布时间】:2018-07-17 22:54:11
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案1】:

    这段代码可以帮你在functions.php中添加代码

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

    【讨论】:

      【解决方案2】:

      谢谢你们,我正试图找到一种将笔记添加到新订单的方法。我正在使用@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 );
      
      }
      

      【讨论】:

      • 我认为你不需要$order-&gt;save(),因为 add_order_note 方法已经为你更新了数据库。
      • 该方法有两个额外的参数:add_order_note( $note, $is_customer_note = 0, $added_by_user = false )
      【解决方案3】:

      从动态订单 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 );
      

      经过测试并且有效。

      【讨论】:

      • 我不需要保存订单。它在 add_order_note(); 中处理
      • 是否可以在手动和编程两种方式创建新客户通知时使用挂钩?
      • 我可以改用set_customer_note 吗?您知道客户备注与非客户备注有何不同吗?
      • 顺便说一句。您是如何获得 order_id 或与订单号相同的?我正在使用 Woocommerce v3.9.1,请您用简单的代码指导我。
      • @Saviomenezes 它比这更复杂,因为我无法通过提供的评论猜测背后的事情。您应该提出一个新问题,清楚地详细说明您实际拥有的代码,解释您正在尝试做什么以及什么不起作用。
      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2021-10-28
      • 2018-10-01
      相关资源
      最近更新 更多