亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改包含重復商品的 WooCommerce 訂單的狀態

更改包含重復商品的 WooCommerce 訂單的狀態

PHP
ABOUTYOU 2023-07-21 16:05:01
客戶支付了一次,但有時商品在訂單中顯示兩次,這是隨機發生的。通常每周兩次。在這種情況下,我需要一個函數來在發生這種情況時更改訂單的狀態(例如當訂單至少具有重復的商品名稱時)。這是我的代碼嘗試:add_filter( 'woocommerce_cod_process_payment_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );add_filter( 'woocommerce_payment_complete_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );function prefix_filter_wc_complete_order_status( $status, $order_id, $order ) {if( ! $order_id ) return;$order = wc_get_order( $order_id );$all_products_id = array();foreach ($order->get_items() as $item_key => $item ){    $item_name  = $item->get_name();        $all_products_id[] = $item_name;}$o_num = count($all_products_id);if($o_num == 1){    return 'processing';    }else{        $standard = 0;    for($i=1;$i<$o_num;$i++){        if($all_products_id[0] == $all_products_id[i]){            $standard++;        }       }    if($standard > 0){        return 'on-hold';       }else{        return 'processing';    }   }當我測試它時,我收到此錯誤:SyntaxError: Unexpected token < in JSON at position 18任何建議將不勝感激。
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

您的代碼中存在一些錯誤和復雜性。此外,您不能使用具有相同函數的兩個鉤子,因為它們沒有相同的參數。


您可以做的是在每個掛鉤的單獨函數內使用自定義條件函數,如下所示:


// Custom conditional function

function has_duplicated_items( $order ) {

    $order_items = $order->get_items();

    $items_count = (int) count($order_items);

    

    if ( $items_count === 1 ) {

        return false;

    }

    

    $items_names = array();

    

    // Loop through order items

    foreach( $order_items as $tem_id => $item ){

        $product_id  = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

        $items_names[$product_id] = $item->get_name();

    }

    

    return absint(count($items_names)) !== $items_count ? true : false;

}


add_filter( 'woocommerce_cod_process_payment_order_status', 'filter_wc_cod_process_payment_order_status', 10, 2 );

function filter_wc_cod_process_payment_order_status( $status, $order ) {

    return has_duplicated_items( $order ) ? 'on-hold' : 'processing';

}


add_filter( 'woocommerce_payment_complete_order_status', 'filter_wc_payment_complete_order_status', 10, 3 );

function filter_wc_payment_complete_order_status( $status, $order_id, $order ) {

    return has_duplicated_items( $order ) ? 'on-hold' : 'processing';

}

這次應該可以解決錯誤:“SyntaxError: Unexpected token < in JSON at position 18”


代碼位于活動子主題(或活動主題)的 function.php 文件中。它應該有效。


查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號