draw62188 2019-08-08 21:26
浏览 49
已采纳

通过functions.php设置woocommerce客户备注不起作用

In Woocommerce I'm trying to modify the Customer Order note to say "Ship with this provider" when a customer billing/shipping address is a specific city.

WordPress 5.2.2 WooCommerce 3.6.5

I'm using the woocommerce_thankyou hook, getting the order data via order ID and 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>";
}

The echo results display correctly.

Customer note: Test Note

Customer note: Test Note Ship with Canada Post.

When I check the order page the order note is only the original customer note.

Customer provided note:
Test Note

It looks like the setter isn't sending the changes to the database. Is there a method I need to call to make sure that my changes are added to the DB, or should I just do it directly via a wpdb query?

EDIT: Corrected echo results to reflect code.

  • 写回答

2条回答 默认 最新

  • donglin4636 2019-08-09 10:26
    关注

    Just add the follows code snippet to achieve to task -

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。