I want to remove the moderate_comments capability from the Shop Manager Role.
I am using the below code, which is not working at all.
add_action( 'init', 'add_hgkb_caps');
function add_hgkb_caps() {
$shop_manager = get_role( 'shop_manager' );
$shop_manager->remove_cap('moderate_comments');
}
However the same is working fine with other roles, like editor, and author.
You could try a different Method:-
add_filter( 'comment_row_actions', 'add_hgkb_caps', 15, 2 );
function add_hgkb_caps( $actions, $comment )
{
$roles=array('shop_manager'); //You can add more roles
$current_user = wp_get_current_user();
$current_user_role=$current_user->roles[0];
if(in_array($current_user_role,$roles))
{
unset( $actions['quickedit'], $actions['edit'], $actions['spam'], $actions['unspam'], $actions['trash'], $actions['untrash'], $actions['approve'], $actions['unapprove'], $actions['delete'] );
}
return $actions;
}
Related
Our scenario is:
The guest user enters the site and places one or more orders without the need to register. And after a while, he decides to register on the site.
Now how to link guest orders to customer account after registeration?
I use the following code, but this code only works for users who have already registered but did not log in at the time of purchase. Any advice?
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
You can use the woocommerce_created_customer action hook and the wc_update_new_customer_past_orders() function
So you get:
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
// Link past orders to this newly created customer
wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
I am developing a ecommerce theme.
I have installed the WooCommerce PayPal Checkout Payment Gateway plugin for payment, and I want to change the location of this checkout button, I tried to remove_action display checkout button but it didn't work, How can I remove action in this case?
Hook in plugin file:
plugins/woocommerce-gateway-paypal-express-checkout/includes/class-wc-gateway-ppec-with-spb.php
This is my code in functions.php file, it not works:
function remove_anon_filters( $name, $functionname, $priority ) {
global $wp_filter;
foreach ( $wp_filter[ $name ][ $priority ] as $key => $data ) {
if ( stripos( $key, $functionname ) ) {
remove_action( $name, $key, $priority );
}
}
}
add_action( 'plugins_loaded', 'demo_init', 999 );
function demo_init() {
remove_anon_filters( 'woocommerce_review_order_after_submit', 'display_paypal_button', 10 );
}
// or.
add_action( 'init', 'remove_init', 999 );
function remove_init() {
remove_action( 'woocommerce_review_order_after_submit', array( 'WC_Gateway_PPEC_With_SPB', 'display_paypal_button' ), 10 );
}
Any help is appreciated, thank you.
To remove a non-static method, you should instantiate the class, and remove the action in the wp_head https://developer.wordpress.org/reference/functions/remove_action/
Additionally, the remove_action should be the same priority as the add action, which in this case is not specified.
add_action( 'wp_head', 'remove_ppec_with_spb', 10);
function remove_ppec_with_spb() {
$class = new WC_Gateway_PPEC_With_SPB;
remove_action( 'woocommerce_review_order_after_submit', array( $class , 'display_paypal_button' ) );
}
Any ideas would be much appreciated.
I am trying to reorder my woocommerce products by most viewed using the post-views-counter plugin.
Followed these examples which did not seem to work Sort products by most viewed
function my_view_filter($query){
if ($query->is_main_query() && ( $query->is_home() || $query- >is_archive()
)
) {
$query->set('post_type', 'product');
$query->set('suppress_filters', false);
$query->set('orderby', 'post_views');
$query->set('order', 'asc');
$query->set('fields', '');
}
}
add_action( 'pre_get_posts', 'my_view_filter' );
First of all, the plugin you're trying to use is outdated. Secondly, it seems to create a table to hold the views and this would require changing the actual MySQL query to retrieve posts ordered by date which is a lot of work looking what you need to do.
You could simply just save views on visit to post meta and use that to order products on the catalog like this:
/**
* Setting post count on each visit
*
*/
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
function prefix_save_product_views( ) {
$product_id = get_the_ID();
$increment = 1;
$current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
}
/**
* Change the display order based on visit count only in Catalog
*
*/
add_filter('woocommerce_get_catalog_ordering_args', 'prefix_woocommerce_catalog_orderby');
function prefix_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'product_visit_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
You'd run into multiple issues using pre_get_posts
// Remove product category/tag meta from its original position
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
// Add product meta in new position
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 5 );
I want to hide Downloads from Woocommerce menu when user type is Artist.
Add this code to your theme's 'functions.php'.
function custom_my_account_menu_items( $items ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if($role[0]=="artist") // change role name if different
unset($items['downloads']);
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
In edit-user.php of wordpress dashboard before updating meta information I am checking one condition and upon failure of that condition I want to display a error message. I tried to echo div with updated class and also tried WP admin_notices hook but no luck
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
global $wpdb;
if(CONDITION TRUE) {
update_usermeta( ........... );
}
else {
WANT TO DISPLAY ERROR MESSAGE
}
}
There is a hook for validating user extra fields. This hook will call before updating user details.
You can display error message like this:-
add_action( 'user_profile_update_errors', 'validate_extra' );
function validate_extra(&$errors, $update = null, &$user = null)
{
if (!$_POST['YOUR_FIELD'])
{
$errors->add('YOUR_FIELD', "<strong>ERROR</strong>: YOUR ERROR MESSAGE.");
}
}
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id )
{
global $wpdb;
update_usermeta( ........... );
}