【问题标题】:Change Woocommerce Order status based on Custom Field value根据自定义字段值更改 Woocommerce 订单状态
【发布时间】:2022-12-15 07:11:16
【问题描述】:

我们目前使用 Autonami 插件来完成此操作,但我觉得它已经过时了,我想删除该插件并通过函数文件来完成。

当 WC 订单设置为 Status“Processing”(不确定是否为大写)时,检查自定义字段“Schedule”的值是否包含单词“Every”。如果是,将状态更改为“我的自定义状态”

那会很容易完成吗?
谢谢!

/J

我不知道从哪里开始。

【问题讨论】:

  • 我使用 ChatGPT AI 来生成代码!惊人! ibb.co/HCgt0RJ add_action( 'woocommerce_new_order', 'change_order_status_to_done' ); function change_order_status_to_done( $order_id ) { // Get the custom field value $custom_field = get_post_meta( $order_id, 'your_custom_field_name', true ); // Check if the custom field contains the word "Every" if ( strpos( $custom_field, 'Every' ) !== false ) { // Set the order status to "Done" $order = wc_get_order( $order_id ); $order->update_status( 'done' ); } }

标签: woocommerce


【解决方案1】:
    add_action( 'woocommerce_new_order', 'change_order_status_to_done_if_schedule_contains_every' );

function change_order_status_to_done_if_schedule_contains_every( $order_id ) {
  $order = wc_get_order( $order_id );

  if ( $order->get_status() == 'processing' ) {
    $schedule = $order->get_meta( 'schedule' );

    if ( strpos( $schedule, 'every' ) !== false ) {
      $order->update_status( 'done' );
    }
  }
}

由 chatGPT AI 生成!它有效! 在此代码中,我们使用 woocommerce_new_order 操作挂钩在创建新订单时触发我们的函数。然后该函数使用挂钩提供的 $order_id 获取订单对象。

接下来,它检查订单的状态是否为“处理中”。如果是,该函数使用 $order 对象的 get_meta() 方法检索“schedule”自定义字段的值。

如果“schedule”字段包含单词“every”,该函数将使用 $order 对象的 update_status() 方法将订单的状态更新为“done”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 2021-08-16
    • 1970-01-01
    • 2021-04-08
    • 2022-10-05
    • 1970-01-01
    • 2019-08-27
    • 2017-05-24
    相关资源
    最近更新 更多