Add element to Order Details - php

We are using the "User Switching" plugin and I am looking to add an element or a simple bit of text (Placed by Rep: YES) to the WooCommerce Order details which identifies if the order was placed by the customer or by a sales rep that logged in as the customer. This can be achieved with the current_user_switched() function like so:
if ( function_exists( 'current_user_switched' ) ) {
$switched_user = current_user_switched();
if ( $switched_user ) {
// User is logged in and has switched into their account.
// $switched_user is the WP_User object for their originating user.
}
}
But I am stuck on how to add the additional code that would show the actually element on the order details. Hope you can help.

Related

Get and use the user group name in Woocommerce

I am only using Wordpress with Woocommerce. Is there a way to get user group name based on logged in user's info?
I have group of users called 'Group1' and assigned 2 users to it - user1, user12
I have the second group of users called 'Group2' and assigned 2 users to it - user2 and user23.
Now I need to check from which user group is the logged in user (all users have the same role - Customers).
Here is the sample of the code of what I am trying to do:
$user_logged = wp_get_current_user();
$user_group_name = // how to get user_logged's group name ?
if ($user_group_name == 'Group1') // do 1st
if ($user_group_name == 'Group2') // do 2nd
Names of the groups will be not changed so I will put them as strings into the code. However I need to get dynamically after user loggs in the name of the group he is assigned to.
How can I do it ?
Any help is appreciated.
You almost made it. Based on documentation, wp_get_current_user() return WP_User object that has properties roles (the roles the user is part of).
$roles = wp_get_current_user()->roles;
if (in_array("Group1", $roles)) {
// do 1st
}
if (in_array("Group2", $roles)) {
// do 2nd
}
Hope it helps
If you mean that you want to know what the role of an online user is?
Finally, you should use the following code.
$get = wp_get_current_user();
if ( in_array( 'administrator', $get->roles ) )
// do 1st
if ( in_array( 'editor', $get->roles ) )
// do 2st
if ( in_array( 'group1', $get->roles ) )
// do 3st
if ( in_array( 'groupN', $get->roles ) )
// do N st
You can use easily and directly current_user_can() WordPress dedicated function with a specific user role (or a user capability) like:
if ( current_user_can( 'Group1' ) ) {
// do 1st
} elseif ( current_user_can( 'Group2' ) ) {
// do 2st
}
WordPress and Woocommerce don't provide any Usergroup functionality by default. What related plugin are you using for those Usergroups.
You need to search in the database table wp_usermeta to see if 'Group1' or 'Group2' are assigned to users and give us the meta_key used for it.
Use wp_get_current_user()->roles to retrieve the groups the user is a member of.
$roles = wp_get_current_user()->roles;
if (in_array('group1',$roles)) {
echo 'Do something here for group 1';
}

Woocommerce Subscription access from external php

I have a Wordpress website with Woocommerce and Woocommerce Subscription plugins.
I need to write some PHP script that will check if a user has active subscription status.
From the database, I would like to get information: user id, status (if it's active - if payment was made and the subscription was renewed), end date of subscription...
The problem is that I don't even know where subscription info is saved?
Can somebody write me a snippet of code, that includes a query that will give me a list of users that subscribed, with the necessary information mentioned earlier?
I will post an update with code later, for now, I just need help with query and guidance where to look in the database tables.
I think what you meant is you want to access the Wordpress/Woocommerce Subscriptions API from a PHP file within your Wordpress installation. To do this I think the following will help:
Create a folder in wp-content/plugins called woocommerce-subscriptions-status-checker
Create a file called woocommerce-subscriptions-status-checker.php inside the above new folder.
Add this code in the file:
/*
Plugin Name: Woocommerce Subscriptions Status Checker
Plugin URI: http://www.XXXXXX.com
Description: XXXXXXXXX
Author: XXXXXX
Version: 2.0
Author URI: http://www.XXXXX.com
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Subscriptions_Status_Checker {
function __construct() {
// Avoids the function firing if we are in wp-admin backend
if ( !is_admin() ) {
// Fires on every page load after WordPress, all plugins, and the theme are fully loaded and instantiated
add_action( 'wp-loaded', array( $this, 'check_current_user' ) );
}
}
function check_current_user() {
// Check if user is even logged in, if not exit
if ( !is_user_logged_in() ) return;
$current_user = wp_get_current_user(); // get current WP_User
$user_id = $current_user->ID; // get user id
$is_subscription = wcs_user_has_subscription( $user_id ); // check if user has subscription
if ( $is_subscription )
{
$subscriptions = wcs_get_users_subscriptions( $user_id ); // get array of all subscriptions
// Check if there is one subscription or multiple subscriptions per user
if ( count( $subscriptions ) > 1 ) {
// Example if you wanted to loop through all subscriptions, in the case of the user having multiple subscriptions
foreach ( $subscriptions as $sub_id => $subscription ) {
if ( $subscription->get_status() == 'active' ) {
// Do something
}
}
} else { // Only 1 subscription
$subscription = reset( $subscriptions ); // gets first and only value
if ( $subscription->get_status() == 'active' ) {
// Do something
}
}
}
}
}
new WC_Subscriptions_Status_Checker();
Visit the plugins section of wp-admin and Activate your new plugin.
Here is my solution (not saying that it's the best, but for me, it solved the problem that I had).
Code in function.php (WP child theme)
add_action('init','getWPuser');
function getWPuser(){
$current_user = wp_get_current_user();
return $current_user;
}
Custom website
Code in my custom site that needs information about the user.
require_once '../wp-load.php';
require_once '../wp-content/themes/h-code-child/functions.php';
require_once '../wp-includes/pluggable.php';
require_once '../wp-blog-header.php';
$current_user = getWPuser();
Saving user ID information.
$logged_user_id = $current_user->ID;
Checking on specific subscription if it's active.
$has_free = wcs_user_has_subscription( $logged_user_id, $product_id, 'active' );
You can try Woocommerce REST API.
https://docs.woocommerce.com/document/woocommerce-rest-api/
Basically, Woocommerce provide a restful API to access the data and interact with the ecommerce system outside of the traditional woocommerce workflow.

Add a new field to woocommerce_email_customer_details or any other part of emails

Ok so I am building a woocommerce site for a client and they love it but the only issue is they need a customer number to come through with the email for all new orders. They are willing to go in and fill in this info for each user, but I cannot find out how to get this info to automatically send with the new order email.
I am using WP - Members plugin to add custom information to the users profile, and have created a field there called Customer # (option name: customer_number) I cannot figure how to get woocommerce to add this number to the email that is sent when they place the order (to the admin)
I have tried many different things, it seems like it should just be a hook but I dont know how to get the values or have not been able to figure this out yet in any way.
I have looked and looked and this is my last resort, my minimal understanding of php and hooks is probably my downfall here, so please be very clear if instructing me in that way.
I will not give up on this issue, so I hope someone can help!
We can adapt from my tutorial on customizing the checkout fields to display some extra info in the email:
function kia_display_email_order_user_meta( $order, $sent_to_admin, $plain_text ) {
$user_id = $order->customer_user;
if( ! empty( $user_id ) && ( $data = get_user_meta( $user_id, '_some_user_meta', true ) ) != '' ){
if( $plain_text ){
echo 'The user data for some field is ' . $data;
} else {
echo '<p>The user data for <strong>some field</strong> is ' . $data . '</p>';
}
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_user_meta', 30, 3 );
In this case we'll replace the get_post_meta() of my tutorial with get_user_meta().... and knowing that the user Id is stored in the $order->customer_id property of the $order object.

On WooCommerce My account page, display My Addresses section based on specific user role

On the WooCommerce My Account page I am trying to hide a couple of the sections based on the user role.
At the moment, all people who register directly with the WooCommerce registration form are assigned user role 'Customer'. However, only users with role 'Employer' are actually able to make purchases... so effectively I want to hide the My Addresses section to users who are 'Customers'.
Any ideas if I can do this with a function?
Miro
This is possible easily with templates.
Add this function in your functions.php file so you can reuse it:
function isEmployer(){
$currentUser = wp_get_current_user();
return in_array('employer', $currentUser->roles);
}
Grab the my-account.php template from woocommerce > templates > myaccount and copy over to your theme's WooCommerce directory (YOURTHEME > woocommerce > myaccount).
From there go to line 36. THIS is where the address gets loaded in.
Wrap the address with a PHP if statement like so:
<?php if( isEmployer() ){
wc_get_template( 'myaccount/my-address.php' )
}?>
You would need to override the my-account.php template in your theme and then wrap the call to the address template in some conditional logic. Specifically current_user_can() which checks for WordPress capabilities.
<?php
if( current_user_can( 'place_order' ) ){
wc_get_template( 'myaccount/my-address.php' );
} ?>
Ideally, you would do this based on a capability that the Employer role has that the Customer role does not, but in the worst case you could use the role name à la current_user_can('employer')
Update 2021-02-16
Given the restructuring of my-account.php that is no longer the ideal template to modify and I believe you could remove sections entirely via hooks/filters without overriding a template.
5.0 is out right now, and I would probably filter woocommerce_account_menu_items to remove items from the account menu navigation. And then for security purposes remove the callback from the endpoint too... As an example this adds the address content to the address endpoint: add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );
So to update my example, if you want to completely remove the Edit Addresses tab for certain users, you could use the following snippet to 1. remove the item from the My Account navigation and 2. completely disable that endpoint.
/**
* Conditionally remove address menu item from My Account.
*
* #param array $items the My Account menu items
* #return array
*/
function so_31342804_remove_address_from_my_account_menu( $items ) {
// Remove menu item for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $items['edit-address'] );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'so_31342804_remove_address_from_my_account_menu' );
/**
* Conditionally remove address endpoint from My Account area.
*
* #param array $items the My Account menu items
* #return array
*/
function so_31342804_remove_address_endpoint( $endpoints ) {
// Remove endpoint content for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $endpoints['edit-address'] );
}
return $endpoints;
}
add_filter( 'woocommerce_get_query_vars', 'so_31342804_remove_address_endpoint' );

How can I detect if user has subscribed Memberpress product already

How can I detect if user has already bought a memberpress product.
I'm searching something like this:
if(have_subscription){
//code to add button
}else{
// code to add something else
}
This should be pretty straight forward using MemberPress' built in capabilities:
if(current_user_can('memberpress_authorized')) {
// Do authorized only stuff here
}
else {
// Do unauthorized stuff here
}
MemberPress also adds capabilities for each MemberPress Membership so you could also do something like this:
if(current_user_can('memberpress_product_authorized_123')) {
// Do authorized only stuff here
}
else {
// Do unauthorized stuff here
}
In the second example the number 123 is the id of the MemberPress membership.
The answer from 2015 didn't work for me but is a top search result. I thought I should share the result of my search here for others in the future.
Also, I think that "product_authorized" capability only checked if a purchase was made, not verifying the expiration date.
So here is how MemberPress determines if active, inactive, or none:
$mepr_user = new MeprUser( $user_id );
if( $mepr_user->is_active() ) {
// Active
}else if($mepr_user->has_expired()) {
// Expired
}else {
// Never a member
}
has_expired() can return true even if the user is active from a separate transaction so don't rely on that alone.
If you need to check a specific membership you can use the following:
$user_id = 123;
$membership_id = 5000;
$mepr_user = new MeprUser( $user_id );
$mepr_user->is_already_subscribed_to( $membership_id ); // true or false
// OR
$mepr_user->is_active_on_membership( $membership_id ); // true or false
is_already_subscribed_to() accepts only a product id
is_active_on_membership() accepts any of: product id, MeprProduct, MeprTransaction, or MeprSubscription
You can also get all of a user's active subscriptions with:
$mepr_user->active_product_subscriptions( 'ids' ); // return array of ids
Open your WordPress website and:
Select Plugins > Add New
Click on Upload Plugin > Choose file
Select the MemberPress plugin from your saved files
Click Install Plugin > Activate
You will now find MemberPress has been added to your WordPress dashboard.
This should help.

Categories