WooCommerce:根据送货方式自动完成付款订单

时间:2018-01-17 14:40:43

标签: php wordpress woocommerce shipping orders

我有一个人们可以直接打印的产品(送货方法1)或选择通过送货服务(送货方式2)获得。因此,如果订单选择直接打印(发货方法2),订单应自动完成。

是否可以从WooCommerce扩展该代码片段?

我发现的文档 this

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

这是工作解决方案。非常感谢LoicTheAztec:

add_action( 'woocommerce_thankyou', 
'wc_auto_complete_paid_order_based_on_shipping_method', 20, 1 );
function wc_auto_complete_paid_order_based_on_shipping_method( $order_id ) {
if ( ! $order_id ) return;

// HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
$allowed_shipping_methods = array( '5' );

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// Get the shipping related data for this order:
$shipping_item = $order->get_items('shipping');
$item = reset($shipping_item);
$item_data = $item->get_data();

// Get the shipping method name, rate ID and type slug
$method_rate_id = $item_data['instance_id'];  // Shipping method ID

// No updated status for orders delivered with Bank wire, Cash on 
delivery and Cheque payment methods.
$avoided_statuses = array( 'bacs', 'cod', 'cheque');
if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
return;
}
// update status to "completed" for paid Orders and a defined shipping 
method ID
elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
$order->update_status( 'completed' );
}
}

3 个答案:

答案 0 :(得分:0)

我认为您正在寻找的是$order->has_shipping_method('name_of_method')

来自:https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method

答案 1 :(得分:0)

  

首先,get_post_meta($order_id, '_payment_method', true )$order->get_payment_method() 完全相似执行相同的操作
  区别在于第一个使用Wordpress函数从 wp_postmeta 表中访问此数据,第二个是 WC_Order 方法也可以从 wp_postmeta 表中访问相同的数据...
  没有人比另一个好。

新增强的重访代码(有关说明,请参阅this thread

什么是实例ID:
例如,如果送货方式费率ID为 flat_rate:14,则实例ID为14 (唯一ID)

新版本代码:

add_action( 'woocommerce_payment_complete_order_status', 'auto_complete_paid_order_based_on_shipping_method', 10, 3 );
function auto_complete_paid_order_based_on_shipping_method( $status, $order_id, $order ) {
    // HERE define the allowed shipping methods instance IDs
    $allowed_shipping_methods_instance_ids = array( '14', '19' );

    // Loop through order "shipping" items
    foreach ( $order->get_shipping_methods() as $shipping_method ) {
        if( in_array( $shipping_method->get_instance_id(), $allowed_shipping_methods_instance_ids ) ) {
            return 'completed';
        }
    }
    return $status;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试和工作。

原始答案:

下面的答案来自我前一段时间的女佣这个类似的答案:
WooCommerce: Auto complete paid orders

add_action( 'woocommerce_thankyou', 'auto_complete_paid_order_based_on_shipping_method', 10, 1 );
function auto_complete_paid_order_based_on_shipping_method( $order_id ) {
    if ( ! $order_id ) return;

    // HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
    $allowed_shipping_methods = array( 'flat_rate:14', 'flat_rate:19' );

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Get the shipping related data for this order:
    $shipping_item = $order->get_items('shipping');
    $item = reset($shipping_item);
    $item_data = $item->get_data();

    // Get the shipping method name, rate ID and type slug
    $shipping_name = $item_data['name']; // Shipping method name
    $method_rate_id = $item_data['method_id'];  // Shipping method ID
    $method_arr = explode( ':', $method_rate_id );
    $method_type = $method_arr[0];  // Shipping method type slug

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    $avoided_statuses = array( 'bacs', 'cod', 'cheque');
    if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
        return;
    }
    // update status to "completed" for paid Orders and a defined shipping method ID
    elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
        $order->update_status( 'completed' );
    }
}

代码放在活动子主题(或活动主题)的function.php文件中。

经过测试和工作。

答案 2 :(得分:0)

这是我的工作解决方案(感谢LoicTheAztec)

add_action( 'woocommerce_thankyou', 
    'wc_auto_complete_paid_order_based_on_shipping_method', 20, 1 );
function wc_auto_complete_paid_order_based_on_shipping_method( $order_id ) {
    if ( ! $order_id ) return;

    // HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
    $allowed_shipping_methods = array( '5' );

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Get the shipping related data for this order:
    $shipping_item = $order->get_items('shipping');
    $item = reset($shipping_item);
    $item_data = $item->get_data();

    // Get the shipping method name, rate ID and type slug
    $method_rate_id = $item_data['instance_id'];  // Shipping method ID

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    $avoided_statuses = array( 'bacs', 'cod', 'cheque');
    if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
        return;
    }
    // update status to "completed" for paid Orders and a defined shipping method ID
    elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
        $order->update_status( 'completed' );
    }
}