Woocommerce - set role of customer by coupon - wordpress

In woocommerce: I would like to group customers when they make their first order.
I installed "user role editor" plugin to create specific role (Example: student, teacher, ...)
At his first order/register, the customer has to complete the coupon field to register.
The coupon content is for example "student-coupon"
How to set the role of the customer depending of the coupon?
I tried this method:
Group Woocommerce users by Coupon
... but i couldn't make it work...
Here is my code:
// ASSIGN GROUP FROM COUPON
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'student' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id();
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
$add_user_to_coupon_group = add_user_meta($user_id, 'student', 'student_users');
}
}
}
The coupon title is "student".
The "User Role editor" role is Student (ID student).
... Not sure if i have to change both 'custom_user_group' AND 'cricket_users' or only 'cricket_users' on the "$add_user_to_coupon_group" line
maybe somebody see a better solution?
Thank's

Related

Changing Roles in WooC based on Order Status

I want to be able to manually confirm offers --> Thereby changing the order status before a user gets assigned a new role on my page. I tried to combine two functions I found here but when I launched them the page crashed.
The manual confirmation is important since we have to check each person that wants to purchase a membership. The Roles are important because I use them to limit access to other parts of the website.
A users Journey:
A user comes to the website. Then has to log in or create a profile. Then they are forwarded to the site where you can get the membership. (Woocomerce and Stripe) I want to be able to manually confirm if somebody buys a membership or not. If I confirm the user gets a new role. If I don't confirm the user gets no role or is deleted ( the payments should obviously not happen in that case.
What is the problem can somebody please help?
function uiwc_change_role( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
$user = $order->get_user();
$order_status = $order->get_status();
if ('complete' == $order_status) {
if( false != $user && !user_can($user, 'administrator') ){
// our new role name
$role = 'aktives_mitglied2022';
//set the new role to our customer
$user->set_role($role);
}
//return $order_status;
}
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'uiwc_change_role', 100, 1 );
function custom_uiwc_change_role( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
$user = $order->get_user();
$order_status = $order->get_status();
if ('complete' == $order_status) {
if( false != $user && !user_can($user, 'administrator') ){
// our new role name
$role = 'aktives_mitglied2022';
//set the new role to our customer
$user->set_role($role);
}
//return $order_status;
}
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'custom_uiwc_change_role', 100, 1 );

How to add a price list for a specific user wordpress

I want to create a price list for a specific user for example :
user 1 see the price list 1
user 2 see the price list 2
If you use woocomerce one approach is too hook with the woocommerce_get_price filter like below (use this filter in your theme functions.php):
add_filter('woocommerce_get_price', 'mlnc_price_user_based', 10, 2);
function mlnc_price_user_based($price, $product) {
if (!is_user_logged_in()) return $price;
//check if the user has a role of dealer using a helper function, see bellow
$current_user = wp_get_current_user();
if ($current_user->roles[0] == 'administrator'){
//give user 10% of
$price = $price * 0.5;
}
return $price;
}
In this example I filtered users by user roles
Anyway you have to provide more info like #Damocles mentioned

Get the order ID on the final checkout page when a customer purchased a product in WooCommerce

I'm developing a plugin that needs to get the order ID on the final checkout page when a customer purchased a product. How do I do that?
I tried
global $order
var_dump($order);
And all I see is "DESC". I need to access the current order of the customer who is looking at the page.
I need to get the order id in the frontend and add that ID to a script, also on the front end, so that when a client orders a product and pays, the page will load, show them the "Thank you, here is your order number: xxx". At that moment I need my script to, for example, execute a console.log("The oder ID is:", order_id);
To get the order ID on the final checkout page when a customer purchased a product you can use the woocommerce_thankyou hook, where the callback function already contains the order ID.
// define the woocommerce_thankyou callback
function action_woocommerce_thankyou( $order_id ) {
echo "Thank you, here is your order ID: " . $order_id;
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Customer billing country
$billing_country = $order->get_billing_country();
// etc..
};
// add the action
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

How to get ACF data from WooCommerce customers for order export

On a Wordpress-Shop I use WooCommerce (WC) with Advenced-Custom-Fields (ACF)
and WP-All-Import (WPAI) + WP-All-Export (WPAE).
I added a ACF field CustomerNumber to the WC-Customer (which enhanced the WP-User).
On the WPAI-XML-Import I set the CustomerNumber with a value from a ERP.
So all customers have a unique CustomerNumber.
I now need to export the WC-Orders (to import them in the ERP again).
The Order-XML must include the CustomerNumber from the Customer belongs to the Order.
As I see, the other standard fields from the customer – like name and address – are copied automatically to the order (by WooCommerce itself).
My question is now: How I have to do this for the ACF’s?
Did I have to do this by code on my own? Adding the same AC-fields to the WC-Order and hook into the order checkout and copy the values from the customer to the order?
Or is there some kind of setup which do that and which I did not recognize?
Thx
I did not really found an answer.
My current solution is now as I described it in my question.
I added a new rule for these acf to also make them available on the orders.
Then I added a hook to new created orders, determine the acf from the user and copied the necessary values into the order acf.
function di_woocommerce_new_order( $order_id ) {
$order = new WC_Order( $order_id );
if ( !$order ) return;
$customer_id = $order->get_customer_id();
if ( !$customer_id ) return;
$customer = new WC_Customer($customer_id);
if ( !$customer ) return;
$customer_number = $customer->get_meta('customernumber');
if ( !$customer_number ) return;
// add customer number
$order->add_meta_data('customernumber', $customer_number);
// update order modified timestamp
$order->add_meta_data('order_last_updated', current_time( 'mysql' ));
$order->save_meta_data();
}
add_action( 'woocommerce_checkout_order_processed', 'di_woocommerce_new_order', 10, 1);

Woocommerce "No Shipping methods" message: Customise message based on the Zipcode customer enters

Our store has been setup to process orders only within Sydney city. We manage this in Woocommerce by setting the allowed postcodes for a Flat Rate Delivery.
We are now extending deliveries to other other areas but only via telephone (no online orders for these new postcodes). Woocommerce displays a standard No Shipping message but we would to customise such that
Customer enters a postcode within the city, allow online orders. No
change to current behaviour.
Customer enters a postcode for which
telephone orders are allowed, show a customised message asking the
customer to make the call.
All other postcodes, disallow orders.
Any technical direction will be greatly appreciated.
Thanks.
Below code should help you. Change the value of array variable $zip_array in both the functions as a comma separated list of zip codes, which you want to show a custom message. Also, change the string value of $custom_msg to your custom message. More details, please refer this article.
// For Cart Page.
add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
$zip_array = array(
'30031',
);
if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
$custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
if( empty( $custom_msg ) ) {
return $default_msg;
}
return $custom_msg;
}
return $default_msg;
}
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 8, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$zip_array = array(
'30031',
);
if ( in_array( $woocommerce->customer->get_shipping_postcode() , $zip_array) ) {
$rates = array();
}
return $rates;
}

Resources