Why it doesn´t work? - php

I´m writing that to give permission automatically an user to post when user have finished the purchase in woocommerce, but not before to buy,
but I don´t know how to do it, Wordpress is something new for me and php too. The following code doesn´t fail, but it doesn´t work. Can you help me, please?? By the way,
I´m writing this code on functions.php, I guess that´s the proper file.
Thank you very much in advance.
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
$user = $order->get_user();
if( isset($user) ){
$user = new WP_User( $user_id );
$user->add_cap( 'publish_posts' );
}
}

$user_id is not defined.
WC_Order::get_user() returns a WP_User so a new WP_User is not necessary.

Related

WordPress - remove capabilities based on user id

I current have this code written:
add_action( 'init', 'hide_user_caps' );
function hide_user_caps() {
$user_id = 1;
$user = new WP_User( $user_id );
$user->remove_cap( 'edit_posts' );
}
However, this doesn't seem to be doing anything. Any ideas?
Thank you
If you want to specifically deny a capability from a user (not a role), you need to use the add_cap()[1] method and specify that the $grant parameter is false. [2].
$user_id = 1;
$user = new WP_User( $user_id );
$user->add_cap( 'edit_posts', false );
[1]: WP_User::add_cap()
[2]: The answer

WooCommerce custom plugin: Error around WC_Customer Object instance

I've built a plugin and I have the following issue:
The WooCommerce Dashboard (in the admin side) will not load the data. It hangs and fails. I have tracked the problem code:
The issue in the
if ( is_admin() ) {
//removed
} else if ( !$this->is_login_page() && !wp_doing_ajax() ) {
$public = new Public();
}
It's the public side code that's causing the issue! and neither is_admin or wp_doing_ajax prevent it from happening.
In the public side, I'm calling
add_action( 'init', array('Dynamic_Rules', 'dynamic_rule_tax_exemption') );
Inside the tax exemption function, I have this code in particular which causes the problem:
$woocommerce = WC();
$user_country = $woocommerce->customer->get_billing_country();
$woocommerce->customer->set_is_vat_exempt(true);
So I can only speculate about what happens, perhaps the WC() is somehow sending everything into an infinite loop, which is why the Dashboard does not load the data. Why is_admin() and wp_doing_ajax() don't prevent this from happening I don't know.
Perhaps it's wrong that I'm calling that function on init, but where else could I call it?
Any help is appreciated
Is difficult to find out what your problem can be… Note that "your question should be updated to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem."
What you can try instead, may be:
$customer = WC()->customer;
if( ! is_a( $customer, 'WC_Customer' ) {
global $current_user;
if( $current_user > 0 ) {
$customer = new WC_Customer( $current_user->ID );
}
}
if( is_a( $customer, 'WC_Customer' ) {
$billing_country = $customer->get_billing_country();
if( ! $customer->is_vat_exempt() ) {
$customer->set_is_vat_exempt( true );
}
} else {
// Some code to throw an error or debug trace
}
I hope this will solve your issue. If not you need to pass the User ID to your code in some way.
Maybe useful: Debugging WooCommerce PHP with Javascript console.log doesn't work

Access the order object from woocommerce_order_details_after_order_table hook

In WooCommerce, I am making a custom merchant plugin and I have added this hook:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
Inside the track_me() function, I want to retrieve the order to do some things.
The function does what I want when I want, but I'm currently getting the order_id from the URL which seems janky.
I cannot figure out how to properly retrieve the $order object or the $order_id which would be enough.
It's almost certainly something obvious as hours of searching the Internet has been fruitless. I just don't know what the obvious something is...
UPDATE 1: Following the advice of #LoicTheAztec, I did the following:
do_action( 'woocommerce_order_details_after_order_table_items', $order );
class Order_MY extends WC_Order
{
function __construct()
{
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
}
function track_me( $order )
{
// My code here
}
}
The first place the script fails is on the do_action line where PHP complains $order is an undefined variable.
The script also fails at extending WC_Order: Class 'WC_Order' not found
New question... is there something I need to do to make sure I have access to the woocommerce classes in my plugin?
The WC_Order Object instance $order variable is included in the hook. You see that on order/order-details.php template file (line 76):
do_action( 'woocommerce_order_details_after_order_table_items', $order );
So you can use it in your hooked function this way:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
Now in a plugin, inside a class, you will replace:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
by:
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
Addition:
As you can't get the $order object from your plugin you should also try:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
global $order;
}
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
I cant tell you anything more
I am not an expert but maybe sth. like this could work:
global $order_id;
$order = wc_get_order($order_id);

Remove user when subscription is cancelled by the customer in Woocommerce

When customer click cancel subscription, they still can logged in the my account section, but I want to let user cannot access anything on my account section. From this point, I would like to make the function on "if the user cancels payment, it also delete the account too" to make them cannot access, so I try to investigate and write the code below.
add_action( 'woocommerce_order_status_cancelled',
'custom_woocommerce_auto_delete_user' );
function custom_woocommerce_auto_delete_user( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ( !$order_id )
return false;
if ('cancelled' == $order_status) {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
However, I'm just the beginner of woocommerce and I don't know which solution is the best for this issue. I'm so glad for anyone that come to response to me :)
I want to delete the users who are created when trying to buy a subscription but the payment gets failed although the user anyhow creates. I am just to confirm if this code works for Me or not?
add_action('woocommerce_subscription_status_failed', 'custom_woocommerce_subscription_status_failed');
function custom_woocommerce_subscription_status_failed($order_id)
{
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ($order_status`enter code here` == 'failed') {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}

Avoid sending completed order status email notification for a specific user role

I have a user role for wholesale customers (wholesale_customer). When I mark an order as completed a notification is send to customers. This is ok for my regular customers but I would like to disable/remove the notification for wholesale customers.
What I've got so far:
function do_not_send_some_email_notifications(WC_Emails $wc_emails) {
$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
$user_id = $order->user_id;
$get_user_data = get_userdata($user_id);
$user_roles = $get_user_data->roles;
if (in_array('wholesale_customer', $user_roles)) {
remove_action('woocommerce_order_status_completed_notification', array($wc_emails->emails['WC_Email_Customer_Completed_Order'], 'trigger'));
}
}
}
add_action('woocommerce_email', 'do_not_send_some_email_notifications');
I have tested this but it's not working.
It would be very nice if someone can point me in the right direction.
Thanks.
Updated 2: I finally find the right hook to make it work. I have revisited your code a little bit using a very similar custom function hooked in the woocommerce_order_status_completed action hook.
Here is the code:
function custom_conditional_email_notifications( $order_id ) {
// Set HERE the targetted user role
$targeted_user_role = 'wholesale_customer';
// Get the order object, the user ID, and the user role.
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$user_info = get_userdata($user_id);
if ( in_array( $targeted_user_role, $user_info->roles ) && $user_id > 0 )
remove_action( 'woocommerce_order_status_completed_notification', array(
$wc_emails->emails['WC_Email_Customer_Completed_Order'],
'trigger'
) );
}
add_action( 'woocommerce_order_status_completed', 'custom_conditional_email_notifications' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories