对于 WooCommerce 订单上具有已完成状态的特定产品,执行操作 [英] For specific products on WooCommerce orders with completed status, perform an action

查看:57
本文介绍了对于 WooCommerce 订单上具有已完成状态的特定产品,执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,如果购买了列表中的至少一种产品并且该产品的当前订单状态已完成,我想执行一项操作.

IN WooCommerce, I would like to perform an action if at least one product from a list is bought and if the current order status for that product is completed.

例如我只能验证产品是否被购买:

For instance I can only verify if the product is bought:

global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$product_list = array('11', '12', '13', '14', '15','16');
$text= false;
  foreach ($product_list as $value):
    if (wc_customer_bought_product( $customer_email, $user_id, $value) ) {
        $text = true;
     }
  endforeach;

推荐答案

尝试以下每次订单完成"时触发的操作;状态,检查当前订单是否具有您定义的 ID 中的特定产品,允许您执行操作:

Try the following that will be triggered each time an order gets "completed" status, checking if the current order has specific products from your defined Ids, allowing you to perform an action:

add_action('woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 );
function action_on_order_status_completed( $order_id, $order ){
    // Here below define your product id(s)
    $products_ids = array('11', '12', '13', '14', '15','16');
    $found = false; // Initializing
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if ( array_intersect([$item->get_product_id(), $item->get_variation_id()], $products_ids) ) {
            $found = true;
            break; // Stop the loop
        }
    }
    
    if ( $found ) {
        // HERE do your action
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经过测试和工作.

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

相关:如何获取 WooCommerce 订单详情

这篇关于对于 WooCommerce 订单上具有已完成状态的特定产品,执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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