How to hide save order button in woocommerce edit orders? - php

How to hide save order button from admin in woocommerce edit orders if specific role is logged in?

Orders menu is protected by ‘edit_shop_orders’ capability. It’s possible to prohibit new order creation if you activate ‘create’ capability at User Role Editor Settings.
But ‘edit_shop_orders’ capability is enough to ‘process’ and ‘complete’ the order. The only way to allow a read-only access to the WooCommerce orders is to use this filter from WooCommerce code:
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $the_order );
Your function for this filter should return empty $actions array for ‘editor’ role in order to make for them read-only access.
Let me know if you need further help.
Add This piece of code to function which gets executed ONLY for "specific" role:
function add_my_action($actions, $the_order){
return array();
}
add_filter( 'woocommerce_admin_order_actions', 'add_my_action', 10, 2);**

Related

Allow specific user manage & install plugins - WooCommerce/Wordpress

I try to allow specific user to install and manage plugins. His role is Store Manager (WooCommerce), and this is my functions:
function add_theme_caps() {
// Get & Set user ID
$shopmanager_id = 7;
$shopmanager = new WP_User( $shopmanager_id );
// Let him manage and install plugins
$shopmanager->add_cap( 'edit_plugins', true );
$shopmanager->add_cap( 'install_plugins', true );
}
add_action( 'admin_init', 'add_theme_caps');
But it's not working. How to achieve that?
OK, so I made this in different way. There was some problem with Manager role from WooCommerce. So I decide to add this user ADMIN role, and then lower its privileges with custom code, if/else and custom redirections via functions.php.

Adding data in a custom database table for paid orders in WooCommerce

When the user buy a product, I want to save a series of data in custom tables in database. This data will be the product id, custom fields I have, and some other data.
My idea is that this should be done when the payment of the product has been made correctly, that is to say, at the time of payment.
I wanted you to give me advice, I have created a way but I don't know if it is the right one or if you would recommend any other way.
I've edited the thankyou page, and I have inserted this code:
$order = new WC_Order ($order->get_id ();
$check_payment = $order->payment_complete ();
if ($check_payment) {
global $wpdb;
wpdb->insert (/* CODE DATABASE*/);
}
As woocommerce Order-received (thankyou) page can be reloaded, it's not really the good way.
The correct hook to be used that you can find inside WC_Order payment_complete() method is woocommerce_payment_complete. So your code should be for most payment gateways:
add_action('woocommerce_payment_complete', 'action_payment_complete', 30, 1 );
function action_payment_complete( $order_id ){
global $wpdb;
// Get an instance of the WC_Order object (if needed)
$order = wc_get_order( $order_id );
// Your database actions code
wpdb->insert (/* CODE DATABASE*/);
}
Code goes in function.php file of the active child theme (or active theme).
For payment methods (CHEQUE HERE) as 'cheque', 'bacs' and 'cod' that needs to be "completed" by shop manager, you will use instead:
add_action( 'woocommerce_order_status_completed', 'action_order_status_completed', 20, 2 );
function action_payment_complete( $order_id, $order ){
// The specific payment methods to be target
$payment_methods = array('bacs','cheque','cod');
// Only for specific payment methods
if( ! in_array( $order->get_payment_method(), $payment_methods ) return;
global $wpdb;
// Your database actions code
wpdb->insert (/* CODE DATABASE*/);
}
So when order status will change to completed for this specific payment methods, this hook will be triggered…
You can also use instead woocommerce_order_status_processing if you target Processing order status or woocommerce_order_status_on-hold if you target On Hold order status
Code goes in function.php file of the active child theme (or active theme).
This should works.

How can i customize Wordpress list_users capability?

I am trying to find a way to customize the list_users capability in Wordpress,
I have created a new role called my_admin and i added some capabilities to it.
The problem is that the list_users capability displays all users while i want to display only a group of users according to a user meta field.
Another question about the edit_users capability can i limit this capability to edit the users without promoting them, this capability enables editing the users and also promoting them which means that it enables changes to user roles also. Is there any way i can remove this promoting feature from it, can i also select only one input field to be edited and remove other fields?
function my_new_service_capabilities() {
// Get the role object.
$editor = get_role( 'my_admin' );
// A list of capabilities to add.
$addcaps = array(
'read',
'edit_posts',
'list_users', //I need to list users according to a user meta value.
'edit_users', //I need to enable editing users in only one input field
'moderate_comments',
);
foreach ( $addcaps as $acap ) {
// Add the capability.
$editor->add_cap( $acap );
}
}
add_action( 'init', 'my_new_service_capabilities' );
I think this should work for you →
https://codex.wordpress.org/Class_Reference/WP_User_Query
steps →
Retrieve all users.
User For each loop.
Use meta_query parameter, because WP stores capabilities as json
string in user_meta.
I am giving you a clue how to do this →
$entire_user_list = get_users();
$somse_users= array();
foreach($entire_user_list as $user){
if($user->has_cap('specific_capability')){
$somse_users[] = $user;
}
}
I had solved it using the DOMDocument class.

Cannot assign user roles in wordpress

I was unable to login to wp-admin "You don't have sufficient privileges" so I added this code to my themes functions.php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
function codelight_all_permissions( $allcaps, $cap, $args ) {
$allcaps[$cap[0]] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'codelight_all_permissions', 0, 3 );
Then I was able to get in as site admin, I went to users and noticed there are no roles assigned. In the database I have level 10 and a:1:{s:13:"administrator";s:1:"1";} assigned.
If I create a new user I cannot choose a role for that user from the dropdown. I only get the option "No role for this site". Does anyone know how I can troubleshoot this issue?

How to remove unwanted roles from admin's "New User" Page?

Basiclly, I must Remove the next roles whenever the admin is creating a new user and clicks on the dropdown menu tu display all the available roles:
Contributor
Editor
Unapproved User
Author
It should only display Subscriber and Administrator.
Also I must change the name of Subscriber to something different, where is this code? This problem would seem to me like there was a plugin to it.
To remove a role you can use
<?php remove_role( 'editor'); ?>
and to rename a role add the following php t your functions.php file
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Owner';
$wp_roles->role_names['administrator'] = 'Owner';
}
add_action('init', 'change_role_name');
(The above is from here
There is a function called remove_role that WordPress has in its PHP API. you use it like this:
<?php remove_role('author'); ?>
author could be replaced with any role you want. The way you want to use this command is different than most WP commands. This is because it removes the role from the DB so any future times, the role is already removed. I would recommend making a custom WP plugin that just removes the roles you want on activation.WP Plugin Help

Categories