I want a code that the password generated by woocommerce will be taken from the phone field on the checkout page when the user fills in the details for the billing
I tried the following code and it did not work
I would love a code that works. thanks.
<?php
//Use user phone number as auto-generated password
function wcs_filter_password_phone_number( $args ) {
$args['user_pass'] = $args['dbem_phone'];
return $args;
}
add_filter( 'woocommerce_new_customer_data', 'wcs_filter_password_phone_number' );
It's not really secure setting the password based on the customer's phone number. Anyone could find a way in to be honest.
Having said that, the task is interesting, and based on WooCommerce: Update Order Field Value After a Successful Order it is possible indeed to alter an order/user field after a successful checkout.
You could hook into "woocommerce_thankyou" as per the article, or even into "woocommerce_payment_complete" if you wanted to do that in the background and only for paid/completed orders.
Also, you should look into https://developer.wordpress.org/reference/functions/wp_set_password/ which is the WP function to set a new password programmatically.
Here's my attempt:
add_action( 'woocommerce_thankyou', 'bbloomer_alter_password_after_order' );
function bbloomer_alter_password_after_order( $order_id ) {
$order = wc_get_order( $order_id );
$phone = $order->get_billing_phone();
$user_id = $order->get_user_id();
if ( $phone && $user_id ) {
wp_set_password( $phone, $user_id );
}
}
Related
When a visitor buys a new product in my WooCommerce shop, I'm using the woocommerce_created_customer hook to create a new user on an external database with some information I get from the $customer_id, $new_customer_data, $password_generated arguments.
For example:
function action_woocommerce_created_customer($customer_id, $new_customer_data, $password_generated) {
// Create a new user on external database
}
add_action('woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3);
Well. What I need is a connection to the current order to get the order id. In my shop, a user account can only be created in combination with an order. Is there a way to get order information inside the woocommerce_created_customer hook? Or is this do_action just called before the order is done?
I thought about using another hook after the payment is done. But this is not possible in my case because this is the only hook I found which I can get the unhashed user password, which I very need.
Do you have an idea how to get the order information inside the customer creation hook?
The order is created after the customer.
$this->process_customer( $posted_data );
$order_id = $this->create_order( $posted_data );
$order = wc_get_order( $order_id );
Use this hook to do the stuff.
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
$posted_data contains the username and password
$username = ! empty( $posted_data['account_username'] ) ? $posted_data['account_username'] : '';
$password = ! empty( $posted_data['account_password'] ) ? $posted_data['account_password'] : '';
I have made a simple webshop with woocommerce, with three payment methods. iDeal and by direct bank transfer and on account. The order ID is created based on the payment method. for example, if payment is made with iDEAL, the order id becomes ID190100; if payment is made on account, the order id becomes RK190100. I get this working with the plugin
"Sequential Order Numbers for WooCommerce" from BeRocket but these are already created before the payment is complete. The order ID must only be finalized once the payment has been made. Now orders that have not yet paid, and may not be paying, will receive a fixed order ID. So is it possible to create a temporary order id and when the order is completed change the order id based on payment method?
Woocommerce by default will use the post ID of the order for the order ID. This is evident when viewing the WC_Order::get_order_number() method. If you want to use a custom order number to display, you'll need to add a filter on woocommerce_order_number to load in a different value.
An example script would be:
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->update_meta_data('_new_order_number', $number );
}
}
add_filter('woocommerce_order_number', function($default_order_number, \WC_Order $order) {
//Load in our meta value. Return it, if it's not empty.
$order_number = $order->get_meta('_new_order_number');
if(!empty($order_number)) {
return $order_number;
}
// use whatever the previous value was, if a plugin modified it already.
return $default_order_number;
},10,2);
Try this. It's very quick example. Hope help.
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->set_id( $number );
$order->save();
}
}
I am writing a WooCommerce Plugin that takes care of payment and delivery.
Right now I am at the point of creating an order based on the current shopping cart.
That all works fine getting the items and costs correct, the only problem is that the order shows as being made by "Guest", instead of by the currently logged on user (even though the correct email address for that user is on the order).
Here is my code :
$cart = WC()->cart;
$checkout = WC()->checkout();
$order_id = $checkout->create_order();
$order = wc_get_order( $order_id );
$order->user_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
$order->calculate_totals();
$order->payment_complete();
$cart->empty_cart();
Here is what I see in the backend after running this :
Why is the order placed as "Guest" instead of the user that placed the order?
And how do I get to have the correct user attached?
You will need to add _customer_user to post_meta table with order ID and meta value should be the user ID which you want to link to this order.
I hope that this will help you:
update_post_meta($order_id, '_customer_user', get_current_user_id());
OR
use user/customer id(create a customer/user before place order) in wc_create_order();
eg: $order = wc_create_order(array('customer_id' => $user));
this code fine for me!
I'd like to indicate in WooCommerce "new order" email notification, if it's a repeat customer.
It seems simple, but I've tried about 5 different methods and none worked. I've tried putting this into 2 different hooks:
woocommerce_email_after_order_table
woocommerce_email_subject_new_order.
Seems like wc_get_customer_order_count($user->ID) should work but it appears that the $user object is not passed into those hook's functions, right?
I'm also wondering if this is possible when it's a guest and not a registered user, maybe by comparing the email address?
Thanks
WooCommerce Email notifications are related to orders.
In woocommerce_email_after_order_table hook, you have the Order object as an argument in your hooked custom function and also the $email object.
With that $order object, you can get the user ID this way:
$user_id = $user_id = $order->get_user_id();
From the $email object you can target the new order email notification.
So the working code is going to be:
add_action( 'woocommerce_email_after_order_table', 'customer_order_count', 10, 4);
function customer_order_count( $order, $sent_to_admin, $plain_text, $email ){
if ( $order->get_user_id() > 0 ){
// Targetting new orders (that will be sent to customer and to shop manager)
if ( 'new_order' == $email->id ){
// Getting the user ID
$user_id = $order->get_user_id();
// Get the user order count
$order_count = wc_get_customer_order_count( $user_id );
// Display the user order count
echo '<p>Customer order count: '.$order_count.'</p>';
}
}
}
You can also use instead the woocommerce_email_before_order_table hook for example…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
I have written this function and placed it in my functions.php but for some reason it doesn't trigger, I have tried it as an action and a filter but no luck.
// ADD £40 ON SUCCESFUL SUBSCRIPTION PAYMENT (EXAMPLE 2)
function custom_add_funds_two($user_id) {
// get current user's funds
$funds = get_user_meta( $user_id, 'account_funds', true );
// add £40
$funds = $funds + 40;
// add funds to user
update_user_meta( $user_id, 'account_funds', $funds );
}
add_filter('processed_subscription_payment','custom_add_funds_two');
WooCommerce Docs suggest that the hook you might be after could be:
woocommerce_subscription_payment_complete
or
woocommerce_subscription_renewal_payment_complete
Source
So I would try:
add_action('woocommerce_subscription_payment_complete','custom_add_funds_two');