问题描述
我有一个WooCommerce商店,顾客在这里结账。我现在想出售一些虚拟产品,在这种情况下,我想自动创建一个帐户。
所以我有两个工作代码段。
- 检查购物车中的产品是否在特定类别(在线)中的人
- 一个通过来宾结帐自动创建帐户的人。
但是我不知道如何将这两者结合在一起才能使它们一起工作。还是对此有更好的解决方案?有什么想法吗?
这是我开始使用的两个代码段,然后尝试合并为一个功能。
来源:Check for Product Category in cart items with WooCommerce
add_action('woocommerce_before_cart','action_before_cart');
function action_before_cart() {
$categories = array('online');
$has_category = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $categories,'product_cat',$cart_item['product_id'] ) ) {
$has_category = true;
break;
}
}
// Testing output (display a notice)
if ( $has_category ) {
wc_print_notice( sprintf( 'Product category "%s" is in cart!',reset($categories)),'notice' );
}
}
来源:Send the reset password email notifications programatically in Woocommerce
function wc_register_guests( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
//get the user email from the order
$order_email = $order->billing_email;
// check if there are any users with the billing email as user or email
$email = email_exists( $order_email );
$user = username_exists( $order_email );
// if the UID is null,then it's a guest checkout
if( $user == false && $email == false ){
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email,$random_password,$order_email );
//WC guest customer identification
update_user_meta( $user_id,'guest','yes' );
//user's billing data
update_user_meta( $user_id,'billing_address_1',$order->billing_address_1 );
update_user_meta( $user_id,'billing_address_2',$order->billing_address_2 );
update_user_meta( $user_id,'billing_city',$order->billing_city );
update_user_meta( $user_id,'billing_company',$order->billing_company );
update_user_meta( $user_id,'billing_country',$order->billing_country );
update_user_meta( $user_id,'billing_email',$order->billing_email );
update_user_meta( $user_id,'billing_first_name',$order->billing_first_name );
update_user_meta( $user_id,'billing_last_name',$order->billing_last_name );
update_user_meta( $user_id,'billing_phone',$order->billing_phone );
update_user_meta( $user_id,'billing_postcode',$order->billing_postcode );
update_user_meta( $user_id,'billing_state',$order->billing_state );
// user's shipping data
update_user_meta( $user_id,'shipping_address_1',$order->shipping_address_1 );
update_user_meta( $user_id,'shipping_address_2',$order->shipping_address_2 );
update_user_meta( $user_id,'shipping_city',$order->shipping_city );
update_user_meta( $user_id,'shipping_company',$order->shipping_company );
update_user_meta( $user_id,'shipping_country',$order->shipping_country );
update_user_meta( $user_id,'shipping_first_name',$order->shipping_first_name );
update_user_meta( $user_id,'shipping_last_name',$order->shipping_last_name );
update_user_meta( $user_id,'shipping_method',$order->shipping_method );
update_user_meta( $user_id,'shipping_postcode',$order->shipping_postcode );
update_user_meta( $user_id,'shipping_state',$order->shipping_state );
// link past orders to this newly created customer
wc_update_new_customer_past_orders( $user_id );
}
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou','wc_register_guests',10,1 );
解决方法
我不会自动创建一个帐户。我会确保只要没有帐户就不能下订单。 (如果购物车中存在该特定产品)。
WooCommerce中已经提供了帐户创建和相关操作(例如电子邮件等)。
否则,您将不得不为其编写其他代码,并且您很快会发现总是缺少某些选项(例如,发送带有用户密码的电子邮件),这需要您编写更多的代码。简而言之,请使其尽可能简单。
但是因为您的问题是关于自己创建帐户,因此您可以使用以下代码进行操作
- 通过代码中添加的注释标签进行解释
- wp_insert_user()-将用户插入数据库
function wc_register_guests( $order_id ) {
// Determines whether the current visitor is a logged in user.
if( is_user_logged_in() ) return;
// Get $order object
$order = wc_get_order( $order_id );
// Get the user email from the order
$order_email = $order->billing_email;
// Check if there are any users with the billing email as user or email
$email = email_exists( $order_email );
$user = username_exists( $order_email );
// If the UID is null,then it's a guest checkout (new user)
if( $user == false && $email == false ) {
// Check on category ( multiple categories can be entered,separated by a comma )
$categories = array( 'online','categorie-1' );
// Flag
$has_category = false;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
if ( has_term( $categories,'product_cat',$item['product_id'] ) ) {
$has_category = true;
break;
}
}
// True
if ( $has_category ) {
// Random password with 12 chars
$random_password = wp_generate_password();
// Firstname
$first_name = $order->get_billing_first_name();
// Lastname
$last_name = $order->get_billing_last_name();
// Role
$role = 'customer';
// Create new user with email as username,newly created password and userrole
$user_id = wp_insert_user(
array(
'user_email' => $order_email,'user_login' => $order_email,'user_pass' => $random_password,'first_name' => $first_name,'last_name' => $last_name,'role' => $role,)
);
// (Optional) WC guest customer identification
update_user_meta( $user_id,'guest','yes' );
// User's billing data
update_user_meta( $user_id,'billing_address_1',$order->billing_address_1 );
update_user_meta( $user_id,'billing_address_2',$order->billing_address_2 );
update_user_meta( $user_id,'billing_city',$order->billing_city );
update_user_meta( $user_id,'billing_company',$order->billing_company );
update_user_meta( $user_id,'billing_country',$order->billing_country );
update_user_meta( $user_id,'billing_email',$order->billing_email );
update_user_meta( $user_id,'billing_first_name',$order->billing_first_name );
update_user_meta( $user_id,'billing_last_name',$order->billing_last_name );
update_user_meta( $user_id,'billing_phone',$order->billing_phone );
update_user_meta( $user_id,'billing_postcode',$order->billing_postcode );
update_user_meta( $user_id,'billing_state',$order->billing_state );
// User's shipping data
update_user_meta( $user_id,'shipping_address_1',$order->shipping_address_1 );
update_user_meta( $user_id,'shipping_address_2',$order->shipping_address_2 );
update_user_meta( $user_id,'shipping_city',$order->shipping_city );
update_user_meta( $user_id,'shipping_company',$order->shipping_company );
update_user_meta( $user_id,'shipping_country',$order->shipping_country );
update_user_meta( $user_id,'shipping_first_name',$order->shipping_first_name );
update_user_meta( $user_id,'shipping_last_name',$order->shipping_last_name );
update_user_meta( $user_id,'shipping_method',$order->shipping_method );
update_user_meta( $user_id,'shipping_postcode',$order->shipping_postcode );
update_user_meta( $user_id,'shipping_state',$order->shipping_state );
// Link past orders to this newly created customer
wc_update_new_customer_past_orders( $user_id );
}
}
}
add_action( 'woocommerce_thankyou','wc_register_guests',10,1 );