簡體   English   中英

如何使用我的自定義插件更改 Woocommerce 中的訂單狀態

[英]How to change order status in Woocommerce with my custom plugin

我想知道如何使用我的自定義插件而不是 woocommerce 的插件來更改 WooCommerce 中的訂單狀態。

function changeStatus($order_id)
{
   $order = wc_get_order($order_id);  
   $order->set_status('pending');
   $order->save();
}

如果我使用wc_get_order() function,我得到了那個錯誤:

Fatal error: Call to undefined function wc_get_order() 

如何包含或要求 woocommerce wc_get_order() function 以便正確使用。

更新

您應該首先在您的插件中檢查 woocommerce 插件是否處於活動狀態,例如:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
     // Your plugin code
}

此外,您不需要在插件中使用這種 function,因為您可以從有效的訂單 ID 中獲取WC_Order Object 的插件代碼,並更好地使用WC_Order方法update_status() ,其中已經包含save()方法。

您還可以使用WC_Order class 在 class 存在之前從有效的訂單 ID 檢查(然后設置或更新訂單狀態) ,使用:

if( class_exists('WC_Order') && $order_id > 0 ) {
    $order = new WC_Order( $order_id );

    if ( is_a($order, 'WC_Order') ) {
        // Set or update order status
        $order->update_status('pending'); // save() method is already included
    }
}

相關文檔: WooCommerce 插件開發者手冊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM