【问题标题】:For specific products on WooCommerce orders with completed status, perform an action对于具有已完成状态的 WooCommerce 订单上的特定产品,执行操作
【发布时间】:2019-10-09 19:32:22
【问题描述】:

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

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

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;

【问题讨论】:

  • 你的问题是什么?有什么不能与该代码一起使用的吗?
  • 此代码正在运行,但尚未完成。我还需要一个条件(如果订单已完成),否则客户可以执行该操作并且付款未完成。

标签: php wordpress woocommerce product orders


【解决方案1】:

尝试以下每次订单获得“已完成”状态时触发的操作,检查当前订单是否包含您定义的 Id 中的特定产品,从而允许您执行操作:

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 文件中。经过测试和工作。


相关:How to get WooCommerce order details

【讨论】:

  • 我试过这段代码,但问题是我必须显示自定义页面的一部分,我无法从该页面访问 $found 变量。我试图将该变量声明为全局变量。但是还是不行,在那个页面插入这段代码,结果还是一样。
  • 我尝试使用此代码来回显/ print_r/ var_dump 操作,但什么也没有。我仔细检查了产品 ID,订单完成。 :(
  • @EusebiuBalan 该代码不输出任何内容,它只允许执行诸如更新某些数据、触发另一个功能之类的操作……按照我的建议提出一个新问题,包括指向此的链接回答并解释你需要做更多的细节和上下文。然后在这里通知我。
  • 好的,谢谢!我在这里发布了另一个问题:/questions/56274545/for-specific-products-on-woocommerce-orders-with-completed-status-change-inform
猜你喜欢
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 2021-05-23
  • 2016-04-17
相关资源
最近更新 更多