Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果新客户发送到WooCommerce中的不同地址,则更改订单状态_Php_Wordpress_Woocommerce_Checkout_Account - Fatal编程技术网

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

Php 如果新客户发送到WooCommerce中的不同地址,则更改订单状态,php,wordpress,woocommerce,checkout,account,Php,Wordpress,Woocommerce,Checkout,Account,我想让woocommerce在使用“发送到其他地址”时更改订单的订单状态&客户以前没有成功订购过。 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“收到订单”页面(谢谢页面)上完成

如果是新客户,并且如果检测到账单和发货详细信息之间存在差异,它将更改订单状态,并且您将显示一个自定义通知,其中带有链接到“客户我的帐户”页面的按钮

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

我做了一个单独的函数,它将统计两个挂钩函数中的客户订单

WooCommerce有一个专用的计数功能
wc\u get\u customer\u order\u 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' );
    }
}
//统计未失败的客户订单(轻sql查询)
函数get\u customer\u orders\u action($user\u id,$status=true){
全球$wpdb;
//如果参数$status设置为“false”,我们将检查状态“wc verify”
$status_arg=$status?“不像‘wc failed’”:“像‘wc verify’”;
//对有效订单进行计数的轻型SQL查询(计数中没有失败的订单)
$result=$wpdb->get\u col(“
从{$wpdb->prefix}帖子中选择COUNT(p.ID)作为p
内部连接{$wpdb->prefix}postemta作为p.ID=pm.post\u ID上的pm
其中p.post_类型如“%shop_order%”和p.post_status$status_arg
和pm.meta_键,如“_customer_user”和pm.meta_value=$user_id
" );
返回重置($result);
}
//根据订单数量有条件地更改订单状态并显示自定义通知
添加行动(“woocommerce\u Thankyu”、“new\u customer\u shipping\u verification”,15,1);
新功能\u客户\u发货\u验证($order\u id){
$order=wc\u get\u order($order\u id);//获取订单对象
//勾选1-仅当客户只有1份订单(当前订单)时
if(获取客户订单操作($order->get\u user\u id())!=1)
return;//我们退出
//获取一些装运和账单详细信息以进行比较
$b_firstname=$order->get_billing_firstname();
$s_firstname=$order->get_shipping_firstname();
$b_address1=$order->get_billing_address_1();
$s_address1=$order->get_shipping_address_1();
//勾选2-仅当装运和账单明细之间存在差异时
如果($b_firstname==$s_firstname&$b_address1==$s_address1)
return;//我们退出
##----现在我们可以更新状态并显示通知----##
//变更单状态
$order->update_status('verify');
//完整的帐单名称
$user_name=$order->get_billing_first_name();
$user\u name.=$order->get\u billing\u last\u name();
//短信
$text=_u;('嘿%s%s因为这是您第一次向我们订购,我们需要在发货前验证一些信息');
$message=sprintf($text,$user_name,&&x1f600;);
//按钮和链接
$link=esc_url(wc_get_page_permalink('myaccount'));
$button='';
//显示自定义通知
wc_打印通知($message.$按钮,“通知”);
}
//在我的帐户页面中有条件地显示自定义通知
添加操作('woocommerce\u account\u content','my\u account\u shipping\u verification',2);
功能我的账户装运验证(){
//获取当前用户ID
$user\u id=get\u current\u user\u id();
$user\u data=get\u userdata($user\u id);
//仅当客户几乎有一个状态为“验证”的订单时
if(获取客户订单操作($user\u id,false)==0)
return;//我们退出
//完整的帐单名称
$user\u name=$user\u data->first\u name.';
$user\u name.=$user\u data->last\u name;
//短信(待填写)
$text=_uuu('嘿%s%s,这是您第一次向我们订购,我们需要…');
$message=sprintf($text,$user_name,&&x1f600;);
//显示自定义通知(也可以是普通文本)
wc_打印通知($message,'notice');
}
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


经过测试且有效

感谢Loic的帮助。当我测试这段代码时,我收到了以下致命错误。致命错误:在/home/********/public\u html/wp content/themes/********/functions.php中在线调用布尔值上的成员函数get_user_id()1422@305David…我更新了答案…有点小错误。请再试一次,这次应该可以用了。谢谢你,Loic,我不知道没有你的支持这个wordpress论坛会是什么样子。我可以确认这个代码有效。它检查选择了s的新客户的订单