【问题标题】:Setting woocommerce customer note through functions.php not working通过functions.php设置woocommerce客户注释不起作用
【发布时间】:2019-08-08 21:26:27
【问题描述】:

在 Woocommerce 中,当客户帐单/送货地址是特定城市时,我正在尝试修改客户订单备注以显示“与此提供商一起发货”。

WordPress 5.2.2 WooCommerce 3.6.5

我正在使用woocommerce_thankyou钩子,通过订单ID和get_customer_note()set_customer_note()获取订单数据。

add_action('woocommerce_thankyou','route_mail_on_customer_location', 30, 1);

function route_mail_on_customer_location($order_id){
  $CP_cities = ["City 1", "City 2", "City 3"];

  $order = wc_get_order( $order_id );
  $curr_note = $order->get_customer_note();
  echo "<p>Customer note: " . $curr_note . "</p>";
  if((strpos($curr_note, "Ship with Canada Post.") == false) && (in_array($order->get_shipping_city(), $CP_cities, true))){
    $note = __($curr_note . ". Ship with Canada Post.");
    $order->set_customer_note($note);
  }
  echo "<p>Customer note: " . $order->get_customer_note() . "</p>";
}

回显结果正确显示。

Customer note: Test Note

Customer note: Test Note Ship with Canada Post.

当我查看订单页面时,订单备注只是原始客户备注。

Customer provided note:
Test Note

看起来 setter 没有将更改发送到数据库。我需要调用一种方法来确保将我的更改添加到数据库中,还是应该直接通过 wpdb 查询来完成?

编辑:更正回显结果以反映代码。

【问题讨论】:

    标签: php mysql wordpress woocommerce


    【解决方案1】:

    只需在任务中添加如下代码sn-p即可实现-

    function modify_woocommerce_checkout_posted_data( $posted_data ){
    
        $CP_cities = array( 'city1', 'city2', 'city3' ); // make sure to replace with proper city data
    
        $curr_note = $posted_data['order_comments'];
        if( strpos($curr_note, 'Ship with Canada Post.') == false  && in_array( $posted_data['shipping_city'], $CP_cities ) ){
            $note = $curr_note . __(' Ship with Canada Post.', 'textdomain' );
            $posted_data['order_comments'] = $note;
        }
        return $posted_data;
    }
    add_filter( 'woocommerce_checkout_posted_data', 'modify_woocommerce_checkout_posted_data', 99 );
    

    【讨论】:

    • 嘿,我使用了这个答案,但我必须解决两件事。 if( !isset( $posted_data['order_comments'] ) ) return $posted_data; 我删除了这一行,因为即使用户将评论字段留空,我仍然希望发表评论。 add_filter( 'woocommerce_checkout_posted_data', 'woocommerce_checkout_posted_data', 99 );我不认为你可以有相同的函数名,我这里把第二个函数名和自定义声明的函数改成一样了。如果你解决了这两件事,我会勾选这个答案。谢谢!这帮了大忙!
    • @aoa,是的,您可以这样做,但为了您的信息,您可以使用相同的函数名称,直到,除非该名称未被具有该优先级的其他人使用。为了您的需要,我正在更改上面的代码。
    【解决方案2】:

    请使用以下代码

    add_action( 'woocommerce_email_after_order_table', 
    'customer_note_email_after_order_table', 10, 4 ); 
    function customer_note_email_after_order_table( $order, $sent_to_admin, 
    $plain_text, $email ){
    
    // Only on some email notifications
    if ( in_array( $email->id, array('new_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order') ) ) :
    
    // Get customer Order note
    $customer_note = $order->get_customer_note();
    
    // Display the Customer order notes section
    echo '<h2>' . __("Order notes", "woocommerce") . '</h2>
    <div style="margin-bottom: 40px;">
    <table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
        <tr><td><p>' . $customer_note . '</p></td></tr>
    </table></div>';
    
    endif;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多