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 文件中。它應該有效。
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報