如果新客户运送到 WooCommerce 中的不同地址,则更改订单状态

Change order status if new customer ship to different address in WooCommerce

我想让 woocommerce 在使用 Ship to different address 并且客户之前没有成功订购时更改订单的订单状态。 Woocommerce 需要区分过去失败的订单,因为客户可能之前尝试过但失败了,这意味着他们将在系统中有订单,但它们将失败、取消或挂起。

我已经在名为"verify"的系统中创建了一个新的订单状态,我现在想在下订单时运行一个脚本使用 "ship to different address" 选项将检查客户之前是否下过订单。如果不是,则将订单状态更改为 "verify".

我尝试的第一件事是,如果客户只有一个订单显示“您的订单在验证之前不会发货”,则使用以下代码向客户发出警报。但是无论你有多少订单它都会显示。

我尝试在 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“收到订单”页面 (感谢页面).

上完成

如果是新客户,如果检测到账单和运输细节之间存在差异,它将更改订单状态,您将显示一个自定义通知,其中包含一个链接到客户我的帐户页面的按钮。

在“我的帐户”页面中,它会显示类似的通知,您可以进行不同的自定义(或者您可以在内容中插入带有说明的文本)

我制作了一个单独的函数,将在两个挂钩函数中计算客户的订单。

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 文件或任何插件文件。

已测试并有效