WooCommerce show orders per user role on page with shortcode - woocommerce

I would like to show a list of orders, of a specific user role, on a page at the frontend.
It could be the same list of order as is being shown in the orders tab on the my account page but with all the orders that equals a specific user role.
If possible I would like to use it as a shortcode.
I have been looking on this page to get orders, but I couldn't find out how to implement the role of the user.
Neither am I sure of the creation of the shortcode.
add_shortcode('your-shortcode', 'yourFunction');
function yourFunction(){
$args = array(
'customer_id' => 12,
);
$orders = wc_get_orders( $args );
}

Related

Add alternate customer role to WordPress / WooCommerce

I am trying to add another customer role to WordPress and WooCommerce. I will use this new customer role to assign alternate prices when the user is logged in. My code works but I cannot find what permissions a customer has in WordPress / WooCommerce by default. I want this new role to have identical permissions to the default customer account. The code below is located in my child functions.php file.
/* Custom user roles */
add_role('distributor', __(
'Distributor'),
array(
'read' => true, // Allows a user to read
'create_posts' => true, // Allows user to create new posts
'edit_posts' => true, // Allows user to edit their own posts
'edit_others_posts' => true, // Allows user to edit others posts too
'publish_posts' => true, // Allows the user to publish posts
'manage_categories' => true, // Allows user to manage post categories
)
);
You can use the capabilies of another role and use this as the "capabilities array" when you create the new user role with the add_role() function of wordpress. I assume the role you want the capabilities to be copied over is called customer. You can adjust this.
add_role( 'distributor', 'Distributor', get_role( 'customer' )->capabilities );
The function accepts an array of capabilites. With get_role() you get an role object and access the capabilities of that role.
So we create a new role with the capabilities of an existing role.

WordPress - Hide table rows on condition

I have a custom CPT called Topics and only an editor or admin can create one. There are custom columns and custom meta box. It is only made for the backend purpose, not for publishing. So, it has a meta box and it will have a list of authors and selecting one will assign it to the author and will display in the name will be displayed in the table that has a custom column name Author. So, I want authors to see only those topics that has been assigned to them via meta box. Other Topics should be hidden. Is there a way to hide specific rows with the condition. 'cruiser_writer_value_key' will save the name of the author. So my condition is.
$writer_list_value = get_post_meta( $post_id, 'cruiser_writer_value_key', true );
$current_user = wp_get_current_user();
if($current_user->display_name == $writer_list_value){
//display post row
}
else{
//hide post row of the column
}
I'm really stuck in this. And Yeah I have custom columns too. It's just that I don't want to the author to edit or read Topics that have not been assigned to them.
So, my custom CPT is like this and the custom columns are there. See the picture for more details.
Use filter_posts_list to filter the posts
add_action('pre_get_posts', 'filter_posts_list');
function filter_posts_list($query)
{
//$pagenow holds the name of the current page being viewed
global $pagenow, $typenow;
$user = wp_get_current_user();
$allowed_roles = array('author');
//Shouldn't happen for the admin, but for any role with the edit_posts capability and only on the posts list page, that is edit.php
if(array_intersect($allowed_roles, $user->roles ) && ('edit.php' == $pagenow) && $typenow == 'your_custom_post_type')
{
//global $query's set() method for setting the author as the current user's id
$query->set(
'meta_query', array(
array(
'key' => 'yout_meta_key',
'value' => $user->display_name,
'compare' => '==',
),
)
); // here you can set your custom meta field using meta_query.
}
}

How to find Uncatogerized posts and reattach it to default category?

I changed the default category from uncategorized to another one, then, I have accidentally deleted uncategorized category, which has more than 500 posts attached to it before deletion (check below image)
https://i.postimg.cc/4yGs5vLj/1.jpg
How to find these categories and reattach it to the default category?
P.S.: These posts are not attached to any category.
EDIT: To do this with code, this query will give you all the Posts without a Category:
$postsWithoutCategories = new WP_Query(array(
'post_type' => 'post',
'category__not_in' => get_terms('category', array(
'fields' => 'ids'
)),
));
$required_category_ID = 123;
if ( $postsWithoutCategories->have_posts() ) {
while ( $postsWithoutCategories->have_posts() ) {
$postsWithoutCategories->the_post();
$post_ID = get_the_ID();
wp_set_post_categories( $post_ID, $required_category_ID );
}
}
In simple terms, the above code is looking for all posts, excluding all that are assigned to any of the categories. Therefore, you will end up with the posts which are not assigned to a category.
You will need to modify the post type and term if you're using a custom post type or custom taxonomy.
Then use wp_set_post_categories() to assign them to your required default category.
I have not tested the above code so use with caution.
ORIGINAL ANSWER: Honestly, the fastest way to do this is Bulk Edit:
From Screen Options (in the top right), maximize the number of listings displayed on that page.
Check each of the uncategorized posts.
Select "Edit" from Bulk Actions, then click Apply.
Select the Category you want to attach them to, then click Update.
For 500 posts, it shouldn't take you more than a couple of minutes.

Add a user along with one custom post type

I am developing this plugin that admin can add a user in the backend and when user is created, plugin can automatically generate one custom post which I have added to the theme. The custom post will store user ID that is just created (or if it is possible make that user an author of the post)
I wonder if what I have mentioned above is possible practically. If anybody has any better advice, I am open for any suggestions.
Thank you in advance
I'm not sure that a unique custom post type per user is the best way to implement what you're wanting to achieve. If you have 100 users, you will have 100 custom post types making the wp-admin a nightmare as the left menu would grow with so many menu-items.
Wouldn't it be easier to just use a normal post type and then have the page that shows the user's dashboard filter the posts to only show posts where the user is the post_author? You could then add a hook to catch when a user registers and create the example post, you could modify the code below and add it to your functions.php:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$userPostsCategory = 3;
// Create post object
$my_post = array(
'post_title' => 'Sample Story' ),
'post_content' => 'You can edit this or create a new story',
'post_status' => 'publish',
'post_author' => user_id,
'post_category' => array( $userPostsCategory )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
This method will lower the number of customisations you'd have to do to your themes and make management of the posts a little easier.
Further reading on this:
User registration hook
Inserting a post using wp_insert_post

WordPress plugin combination

I'm using the pie registration plugin and I want to know how can I automatically define a role (in my case I want to define as subscriber) when people register and login. I want to do this because I want to restrict some zones of the website and redirect those zones for the register or login page. Since the free version of the plugin doesn't have restricted areas, I'm also using the plugin called members that does 50% of what I want. So I want to combine these 2 plugins' functionalities.
tldr: how can I give the role subscriber on pie-registration?
When you are going to register user you have assign it a role for ex.
if you are using wp_insert_user( $userdata ) function than you can assign user role in $userdata as follows.
$userdata = array(
'user_login' => 'login_name',
'user_pass' => 'pass',
'role' => 'role' // When creating an user, `user_pass` is expected.
);
There are some default roles in wordpress , you can also creates new roles by using following code.
<?php add_role( $role, $display_name, $capabilities ); ?>
And you can assign capabilities, so by using this two function you can achieve your goal.
You can read more about:wp_insert_user , add_role

Resources