Add the Shop Manager username to Woocommerce Admin Order notes - php

I have a problem right now. I have a plugin that allows me to quickly change the status of my orders from the admin order list.
Unfortunately the name of the shop manager is not transmitted.
I think I've found the right code, but I don't know exactly how to do it.
Would be grateful for any help.
public function save_comment($order, $status_comment) {
$order->add_order_note("[[" . wc_get_order_status_name($order->post_status) . "|" . $status_comment . "]]");
}
Right now it looks like this :
I'd like to see which user changed the status as shown in this picture:

To add the username of the shop manager that has updated the Order to the order note, use the following:
add_filter( 'woocommerce_new_order_note_data', 'filter_woocommerce_new_order_note_data', 10, 2 );
function filter_woocommerce_new_order_note_data( $args, $args2 ) {
if( ! $args2['is_customer_note'] && is_user_logged_in() && current_user_can( 'edit_shop_order', $args2['order_id'] ) ){
$user = get_user_by( 'id', get_current_user_id() );
$args['comment_author'] = $user->display_name;
$args['comment_author_email'] = $user->user_email;
}
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works..

Related

Enable 'Read only' on order page (WooCommerce) for order created & customer email for shop manager only

I'm using WooCommerce: Disable Date on Edit Orders anwer code
According to this only applies to admin, how about for only shop manager?
I wonder is there any way where I can disable on edit 'Order created & 'Customer:'? for shop manager only?
Because the code provided does not work for me and only apply to admin. The way I'm doing is using plugin 'Simple Custom CSS and JS' and add accordingly using the code provided. But it doesn't work.
Is there any tips or code that can be directly be added to functions.php?
As in the post provided to disable edit on 'order created', how about for 'Customer:'? which is right below the 'Status'.
First create js file in your active parent/child theme
Add the code:
jQuery(function($) {
$('.order_data_column .date-picker').attr("disabled", true);
$('.order_data_column .hour').attr("disabled", true);
$('.order_data_column .minute').attr("disabled", true);
$('.wc-customer-user select').attr("disabled", true);
});
In your functions.php file add this:
function prevent_editing_shop_order_fields( $current_screen ) {
$user = wp_get_current_user();
if ( 'shop_order' == $current_screen->post_type && 'post' === $current_screen->base && isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Fix accordingly with your file
wp_enqueue_script('myscript', get_stylesheet_directory_uri().'/assets/js/scripts.js',array('jquery'));
}
}
add_action( 'current_screen', 'prevent_editing_shop_order_fields' );
$user->roles[0] == 'shop_manager' - is a condition to check if current user is shop_manager role

AffiliateWP + WooCommerce - Need Method of Pulling Nickname from ID

So, this one has my head in for a loop. I've contacted the Support of the plugin and they basically told me I'm SOL, but I refuse to believe there isn't a way to do this.
I'm using AffiliateWP to attribute sales to a single location. Each Affiliate feeds off of a Wordpress User, and for their nickname I've used their address/location.
Unfortunately, the plugin doesn't include this in the Order Details screen or Order Emails, which is a BIG problem. I've frankensteined some code together but keep getting all kinds of undefined errors because they do everything inside classes, which I guess makes sure that people like me can't fiddle in there?
Function for assigning an affiliate to an order: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/integrations/class-woocommerce.php#L78
Function for retrieving the Affiliates User ID: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L204
Function for retrieving the Affiliates Name: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L132
[Edit] And finally, my Frankenstein:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
This doesn't throw me an error, but it does throw me a blank area...
Try this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
$order_id = $order->get_id();
$referrals = affiliate_wp()->referrals->get_by( 'reference', $order_id);
$affiliate_id = $referrals->affiliate_id;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
the affwp_get_affiliate_name method returns blank on three conditions:
if ( ! $affiliate = affwp_get_affiliate( $affiliate ) ) {
return '';
}
if ( ! $user_info = get_userdata( $affiliate->user_id ) ) {
return '';
}
...
// If neither are set, return an empty string.
if ( empty( $first_name ) && empty( $last_name ) ) {
return '';
}
Start by manually passing an affiliate id you know to be associated with a record that contains a $first_name and/or $last_name, for example:
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name(5) . '</p>';
This will rule out if the problem is with the $affiliate_id that you're currently passing it. If you get values returned, it should be a simple matter to fix.
After trying that I would experiment with changing the priority of the add_action(), raising it to 999 or something because it could possibly be executing before the relevant hooks in the plugin.

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.

Redirect customers after leaving woocommerce product review

I am trying to redirect users to a thank you page after leaving a product review in woocommerce. I know the code
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location)
{
return $_SERVER["HTTP_REFERER"];
}
will redirect all comments to a location, but I can't seem to find a filter or hook to specify only woocommerce product reviews. Has anyone done this before? Ideally I would just like to redirect to a standard wordpress page so I can add options and features to that pages template file in the future.
The comment_post_redirect filter has a second parameter. This parameter is the $comment object from which we can grab the post's ID. With the post's ID you can test for post_type and adjust the returned variable accordingly.
add_filter( 'comment_post_redirect', 'redirect_after_comment', 10, 2 );
function redirect_after_comment( $location, $comment ){
$post_id = $comment->comment_post_ID;
// product-only comment redirects
if( 'product' == get_post_type( $post_id ) ){
$location = 'http://www.woothemes.com';
}
return $location;
}

How to change woocommerce product image upload path

I have a wordpress multisite with one main and four child website. i have done plugin for share woocommerce product to childsite. now child site can add or update mainsite product from child site. but when displaying in front end the media path is wrong. i want use all child site product image path to same path. how it possible ?
In woocommerce plugin and in file class-wc-admin-post-types.php
woocomerce override the WordPress upload filter through the filter 'upload_dir'
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
public function upload_dir( $pathdata ) {
// Change upload dir for downloadable files
if ( isset( $_POST['type'] ) && 'downloadable_product' == $_POST['type'] ) {
if ( empty( $pathdata['subdir'] ) ) {
$pathdata['path'] = $pathdata['path'] . '/woocommerce_uploads';
$pathdata['url'] = $pathdata['url']. '/woocommerce_uploads';
$pathdata['subdir'] = '/woocommerce_uploads';
} else {
$new_subdir = '/woocommerce_uploads' . $pathdata['subdir'];
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
}
}
return $pathdata;
}
so if you want to override it, you can create filter to this 'upload_dir' with higher priorites.
I would call this a try and error thing, I had done something similar but it takes some time customizing as your desired.
So here it's what I believe could solve your main problem:
add_action('pre_get_posts', '_my_pre_get_posts', 10, 1);
function _my_pre_get_posts( $wp ) {
global $typenow, $blog_id;
if ( 'product' == $typenow && $blog_id != 1) {
switch_to_blog(1);
}
}
if the $blog_id is not the main site (reads ID = 1) you should switch to blog ID 1, all this is happening before all post are pulled from the database.
product is the current post_type being queried, if you want this to work with orders as well you probably will check for 'shop_order'.
To know which post_type is being queried you should check the url, Ex: /edit.php?post_type=shop_order.
This probably would bring some issues such as the title of the blog in the adminbar is the one from the main site, and it's missing that at some point it needs to restore to the main site back, but at least this should do the trick.
check this Solution and Let me know how end up

Categories