【问题标题】:Update order status on form submission在提交表单时更新订单状态
【发布时间】:2019-11-11 13:42:32
【问题描述】:

我正在尝试创建一个在提交表单后自动更新订单状态的表单。该表单位于订单详细信息页面,因此我假设当前页面 ID 等于 orderID。当我尝试提交表单时,它只是卡住了,没有任何反应。我假设获取 orderID 以及更新状态的订单存在问题。

我找到了 gform_after_submission 挂钩并将其链接到放置在订单详细信息页面上的表单(表单 ID 7)。我一直在尝试使用 global $wpdb;但不太确定这样做是否正确。

add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $order_id ) {
global $wpdb;

    //getting orderID
    $order = wc_get_order( $order_id );

    //changing order status
                $order = array();
                $order['ID'] = $order->ID;
                $order['post_status'] = 'wc-completed';

    //updating order
    wp_update_post( $order );
}

我预计,一旦提交了表单,当前订单 ID(提交表单的页面)的订单状态将随着订单状态完成而更新。

【问题讨论】:

    标签: php wordpress woocommerce hook gravity-forms-plugin


    【解决方案1】:

    用以下代码替换你的代码 -

    add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
    function update_order_submission( $entry, $form ) {
        global $post;
    
        $order_id = get_the_ID(); // getting orderID
        $order = wc_get_order( $order_id );
        if( $order ) {
            //changing order status
            $order->update_status( 'completed' );
        }
    }
    

    【讨论】:

    • 谢谢!出于某种原因,它似乎不起作用。我试图将您的代码更改为 updater_status('wc-completed');和 add_action('gform_after_submission', 'update_order_submission', 10, 2);但两者都不起作用:(
    • 无需更改状态“wc-completed”,update_status 已经完成了部分。但是首先要确保上面的钩子回调被命中并且你得到了 get_the_ID() 值。
    • 谢谢你,@itzmekhokan。我注意到 orderID 在 URL 中。我设法通过使用 $_GET["id"] 来获取 url 参数来解决它。
    【解决方案2】:

    我设法通过使用 $_GET["id"] 从 URL 获取 orderID 来获取 url 参数来解决它。 functions.php 中的以下代码解决了这个任务。

    add_action( 'gform_after_submission_7', 'update_order_submission', 10, 2 );
    function update_order_submission( $entry, $form ) {
    global $post;
    $order_id = $_GET["id"]; // getting orderID
    $order = wc_get_order( $order_id );
    if( $order ) {
    //changing order status
    $order->update_status( 'completed' );
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多