我希望woocommerce在使用Ship到不同的地址时更改订单的订单状态&客户之前没有成功订购。 Woocommerce需要区分失败的过去订单,因为客户可能曾经尝试但失败了,这意味着他们将在系统中有订单,但他们将失败,取消或待决。
我已经在系统中创建了一个名为“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' );
我所缺少的是一种只有在选择发送到不同地址及其第一个订单时才触发此警报的方法。
还需要在那里触发更新要验证状态的代码。
任何帮助将不胜感激。
答案 0 :(得分:2)
这必须先在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, '😀' );
// Display the custom notice (or it can be a normal text)
wc_print_notice( $message, 'notice' );
}
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作