簡體   English   中英

對於 WooCommerce 訂單上具有已完成狀態的特定產品,執行操作

[英]For specific products on WooCommerce orders with completed status, perform an action

在 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;

嘗試每次訂單獲得“已完成”狀態時將觸發的以下操作,檢查當前訂單是否具有您定義的 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 文件中。 測試和工作。


相關: 如何獲取 WooCommerce 訂單詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM