繁体   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