在 Woocommerce 的自动完成订单流程中排除特定产品 [英] Exclude specific products on auto-complete orders process in Woocommerce

查看:97
本文介绍了在 Woocommerce 的自动完成订单流程中排除特定产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用代码来自这个回答线程来自动完成订单,以便添加新的 WooCommerce 预订到日历,(只有在标记为完成后才会添加).

I’m currently using the code from this answer thread to auto-complete orders so that new WooCommerce bookings are added to the calendar, (They only get added once marked as complete).

这对预订非常有用,但是,我想从自动完成功能中排除一些其他产品.

This works great for bookings, however, I’d like to exclude some other products from auto-completing.

你知道这是否可行吗?

推荐答案

遍历订单项是一个繁重的过程,而不是制作更轻量级的 SQL 查询.

Iterating through order items is a heavier process, than making a much more lighter SQL query.

这是一个自定义条件函数,用于检查 Woocommerce 订单中的产品 ID(返回 true 或 false):

Here is a custom conditional function that will check for products Ids in a Woocommerce Order (returning true or false):

function order_has_products( $order_id, $product_ids ){
    $product_ids = implode( "','", $product_ids );
    global $wpdb;
    $result = $wpdb->get_var( "
        SELECT COUNT(woim.meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
        INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
        WHERE woi.order_id = $order_id AND woim.meta_key IN ('_product_id','_variation_id')
        AND woim.meta_value IN ('$product_ids')
    " );
    return $result > 0 ? true : false;
}

然后你会这样使用它:

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

    // HERE define your Product Ids to exclude in the array 
    $product_ids = array( 37, 53 );

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

    $found  = order_has_products( $order_id, $product_ids );

    if ( in_array( $order->get_payment_method(), array('cod', 'cheque', '') ) || $found ) {
        return;
    }
    else {
        $order->update_status('completed');
    }

}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

WooCommerce:自动完成付费订单(取决于付款方式)

这篇关于在 Woocommerce 的自动完成订单流程中排除特定产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆