2017-12-13 35 views
1

다른 주소로 배송 된 & 고객이 이전에 주문하지 않은 경우 woocommerce에서 주문의 주문 상태를 변경하고 싶습니다. Woocommerce는 실패한 과거 주문을 구별해야 할 수 있습니다. 고객이 이전에 시도했지만 실패했을 수 있으므로 시스템에 주문이 있지만 실패, 취소 또는 보류 중임을 의미합니다.신규 고객이 WooCommerce의 다른 주소로 배송하는 경우 주문 상태 변경

나는 "확인"이라는 시스템의 새로운 주문 상태를 이미 만들었습니다. 고객에게 "다른 주소로 배송"옵션이있는 주문이 접수되면 스크립트를 실행하고 싶습니다. 전에 주문했는지 여부. 그렇지 않으면 주문 상태가 "확인"으로 변경됩니다.

내가 시도한 첫 번째 사항은 고객이 주문이 확인 될 때까지 배송되지 않는다고 말하는 주문이 하나 뿐인 경우 고객에게 아래 코드를 알려주는 것이 었습니다. 하지만 주문이 많을지라도 아무리 표시됩니다. 내가 functions.php 파일에이 코드를 설정하는 것입니다 시도 무엇

는 :

function wc_get_customer_orders() { 

// Get all customer orders 
$customer_orders = get_posts(array(
    'numberposts' => 1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(), 
    'post_type' => wc_get_order_types(), 
    'post_status' => array_keys(wc_get_order_statuses()), 
)); 

$customer = wp_get_current_user(); 

// Text for our message 
$notice_text = sprintf('Hey %1$s 😀 As this is your first order with us, we will need to verify some info before shipping.', $customer->display_name); 

// Display our notice if the customer has no orders 
if (count($customer_orders) == 1) { 
    wc_print_notice($notice_text, 'notice'); 
} 
} 
add_action('woocommerce_before_my_account', 'wc_get_customer_orders'); 

내가 놓치고 만 서로 다른 주소와의 첫 순서로 배를 선택한 경우이 경고를 트리거하는 방법입니다.

또한 상태를 확인하기 위해 상태를 업데이트하는 코드가 트리거되어야합니다.

도움이 될 것입니다.

이 반드시 WooCommerce에 먼저 수행해야합니다

답변

2

페이지 (감사합니다 페이지) "주문을 받았다."

신규 고객이고 청구서 발송 세부 사항간에 차이점이 발견되면 주문 상태가 변경되고 고객 계정 페이지에 링크 된 사용자 정의 알림이 표시됩니다.

내 계정 페이지에는 과 다르게 사용자 정의 할 수있는 유사한 알림이 표시됩니다 (또는 지침에 따라 콘텐츠에 텍스트를 삽입 할 수도 있음).

나는 두 가지 기능 모두에서 고객의 주문을 계산하는 분리 된 함수를 만들었습니다.

WooCommerce에는 전용 카운팅 기능 wc_get_customer_order_count()이 있지만이 경우 편리하지 않습니다.

코드 :

// Counting customer non failed orders (light sql query) 
function get_customer_orders_action($user_id, $status = true){ 
    global $wpdb; 

    // if argument $status is set to "false" we check for status 'wc-verify' instead 
    $status_arg = $status ? "NOT LIKE 'wc-failed'" : "LIKE 'wc-verify'"; 

    // The light SQL query that count valid orders (no failed orders in count) 
    $result = $wpdb->get_col(" 
     SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts as p 
     INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id 
     WHERE p.post_type LIKE '%shop_order%' AND p.post_status $status_arg 
     AND pm.meta_key LIKE '_customer_user' AND pm.meta_value = $user_id 
    "); 

    return reset($result); 
} 

// Conditionally Changing the order status and displaying a custom notice based on orders count 
add_action('woocommerce_thankyou', 'new_customer_shipping_verification', 15, 1); 
function new_customer_shipping_verification($order_id){ 
    $order = wc_get_order($order_id); // Get the order OBJECT 

    // CHECK 1 - Only if customer has only 1 Order (the current one) 
    if(get_customer_orders_action($order->get_user_id()) != 1) 
     return; // we exit 

    // Get some shipping and billing details to compare 
    $b_firstname = $order->get_billing_first_name(); 
    $s_firstname = $order->get_shipping_first_name(); 
    $b_address1 = $order->get_billing_address_1(); 
    $s_address1 = $order->get_shipping_address_1(); 

    // CHECK 2 - Only if there is a difference beetween shipping and billing details 
    if($b_firstname == $s_firstname && $b_address1 == $s_address1) 
     return;// we exit 

    ## --- --- Now we can update status and display notice --- --- ## 

    // Change order status 
    $order->update_status('verify'); 

    // The complete billing name 
    $user_name = $order->get_billing_first_name().' '; 
    $user_name .= $order->get_billing_last_name(); 

    // The text message 
    $text = __('Hey %s %s As this is your first order with us, we will need to verify some info before shipping.'); 
    $message = sprintf($text, $user_name, '😀'); 
    // The button and the link 
    $link = esc_url(wc_get_page_permalink('myaccount')); 
    $button = '<a href="'.$link.'" class="button" style=float:right;>'.__('Check your info').'</a>'; 

    // Display the custom notice 
    wc_print_notice($message.$button, 'notice'); 
} 

// Conditionally Displaying a custom notice in my account pages 
add_action('woocommerce_account_content', 'my_account_shipping_verification', 2); 
function my_account_shipping_verification(){ 
    // Get the current user ID 
    $user_id = get_current_user_id(); 
    $user_data = get_userdata($user_id); 

    // Only if customer has almost an Order with status like 'verify' 
    if(get_customer_orders_action($user_id, false) == 0) 
     return; // we exit 

     // The complete billing name 
     $user_name = $user_data->first_name.' '; 
     $user_name .= $user_data->last_name; 

     // The text message (to be completed) 
     $text = __('Hey %s %s As this is your first order with us, we will need to...'); 
     $message = sprintf($text, $user_name, '&#x1f600;'); 

     // Display the custom notice (or it can be a normal text) 
     wc_print_notice($message, 'notice'); 
    } 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 또한 어떤 플러그인 파일입니다.

시험 작동

+0

감사합니다. 이 코드를 테스트 할 때 다음과 같은 치명적인 오류가 발생했습니다. 치명적 오류 :/home/******/public_html/wp-content/themes/*******/functions의 boolean에있는 get_user_id() 멤버 함수를 호출하십시오.PHP on line 1422 – 305David

+0

@ 305David ... 답변을 업데이트했습니다 ... 약간의 실수였습니다. 다시 시도해주세요. 이번엔 효과가 있습니다. 감사합니다 – LoicTheAztec

+0

고마워요 Loic,이 WordPress 포럼이 귀하의 지원 밖으로 될 줄 알아요. 이 코드가 작동하는지 확인할 수 있습니다. 선박을 다른 주소로 선택한 신규 고객의 주문을 확인한 다음 상태를 확인하도록 설정합니다. 관리자 주문 화면에서 상태 버튼의 너비와 높이를 확장하는 클래스 "확인"에 수동으로 CSS를 추가했습니다. 또한 저장소 관리자가 주문이 있다는 것을 시각적으로 볼 수 있도록 빨간색 배경을 제공합니다 확인이 필요한 다시 한 번 감사드립니다. – 305David