【问题标题】:Change order status if new customer ship to different address in WooCommerce如果新客户发货到 WooCommerce 中的不同地址,请更改订单状态
【发布时间】:2017-12-13 20:08:35
【问题描述】:

我想让 woocommerce 在使用 Ship to different address 且客户之前未成功订购时更改订单的订单状态。 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' );

我缺少的是一种仅在他们选择运送到不同地址及其第一笔订单时才触发此警报的方法。

还需要在此处触发更新状态以进行验证的代码。

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce checkout account


    【解决方案1】:

    这必须首先在 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 文件或任何插件文件中。

    经过测试并且可以工作

    【讨论】:

    • 感谢 Loic 的帮助。当我测试此代码时,我收到以下致命错误。致命错误:在第 1422 行的 /home/******/public_html/wp-content/themes/*******/functions.php 中的布尔值上调用成员函数 get_user_id()
    • @305David ...我已经更新了答案...这是一个小错误。请再试一次,这次应该可以了。谢谢
    • 谢谢 Loic,如果没有您的支持,我不知道这个 wordpress 论坛会是什么样子。我可以确认此代码有效。它检查来自选择运送到不同地址的新客户的订单,然后将其状态设置为验证。我已经手动向“验证”类添加了一些 CSS,它扩展了订单管理订单屏幕上状态按钮的宽度和高度,并为其提供了红色背景,因此商店管理员可以直观地看到有订单需要验证。再次感谢。
    • @305David 我和大家一样开始了……帮助其他人回答可以提供更多知识,提高技能和解释或教学的能力……根据您的需要自定义代码是一个很好的做法……谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 2017-05-24
    • 1970-01-01
    • 2013-09-10
    • 2018-07-14
    • 2015-09-10
    • 1970-01-01
    • 2014-08-30
    相关资源
    最近更新 更多