I'm at wits end with this now and could really use a hand.
I've added a form on the front end that asks the customer to register with their IRL user account number, I've managed to get that to store in the back end as "morello_account_number". Now I want to echo that account number on the order page in Woocommerce so that I can process orders easier without searching the customers username etc manually. I'm not really a PHP programmer, but here's my code so far:
add_filter('manage_edit-shop_order_columns', 'morello_account_number_column' );
function morello_account_number_column( $order_columns ) {
$order_columns['morello_account_number'] = "Morello Account Number";
return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'morello_placeholder' );
function morello_placeholder( $colname ) {
global $the_order; // the global order object
if( $colname == 'morello_account_number' ) {
$morello_account_number = $order->get_morello_account_number();
echo morello_account_number;
}
}
Apologies if this is super trivial - still learning. And thanks so much in advance.
Assuming you've used update_post_meta before in your previous code?
Then you could apply the following
/**
* Add columns
*/
function morello_account_number_column( $columns ) {
$columns['morello_account_number'] = "Morello Account Number";
return $columns;
}
add_filter('manage_edit-shop_order_columns', 'morello_account_number_column', 10, 1 );
/**
* Populate columns
*/
function morello_placeholder( $column, $post_id ) {
if( $column == 'morello_account_number' ) {
// https://developer.wordpress.org/reference/functions/get_post_meta/
$m_a_n = get_post_meta( $post_id, 'morello_account_number', true );
// Value is found
if ( !empty($m_a_n) ) {
echo $m_a_n;
} else {
echo 'something else';
}
}
}
add_filter( 'manage_shop_order_posts_custom_column', 'morello_placeholder', 10, 2 );
Related
I am trying to add a tab to the My Account page if a specific user exists.
I created a check_user_role function that checks if a specific role exists.
If the premium_member role exists an extra tab should be added to the My Account page.
Right now when I add the check_user_role function it breaks my website.
Everything is in the functions.php file
function check_user_role($roles, $user_id = null) {
if ($user_id) $user = get_userdata($user_id);
else $user = wp_get_current_user();
if (empty($user)) return false;
foreach ($user->roles as $role) {
if (in_array($role, $roles)) {
return true;
}
}
return false;
}
// ------------------
// 1. Register new endpoint (URL) for My Account page
// Note: Re-save Permalinks or it will give 404 error
function vehicle_intake_form_endpoint() {
add_rewrite_endpoint( 'vehicle-intake-form', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'vehicle_intake_form_endpoint' );
// ------------------
// 2. Add new query var
function vehicle_intake_form_query_vars( $vars ) {
$vars[] = 'vehicle-intake-form';
return $vars;
}
add_filter( 'query_vars', 'vehicle_intake_form_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
if(check_user_role(array('premium_member'))) {
function vehicle_intake_form_link_my_account( $items ) {
$items['vehicle-intake-form'] = 'Vehicle Intake Form';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
}
// ------------------
// 4. Add content to the new tab
function vehicle_intake_form_content() {
echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
//echo do_shortcode( ' /* your shortcode here */ ' );
}
add_action( 'woocommerce_account_vehicle-intake-form_endpoint', 'vehicle_intake_form_content' );
Your If statement needs to be inside the function, never outside it.
You can use current_user_can() function, but inside your hooked function like:
add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
function vehicle_intake_form_link_my_account( $items ) {
if( current_user_can('premium_member') ) {
$items['vehicle-intake-form'] = __('Vehicle Intake Form');
}
return $items;
}
Or use check_user_role() revisited function to be used inside your hooked function like:
function check_user_role( $roles, $user_id = null ) {
$user = $user_id ? new WP_User( $user_id ) : wp_get_current_user();
if ( is_a($user, 'WP_User') && count( array_intersect($roles, $user->roles) ) > 0 ) {
return true;
}
return false;
}
add_filter( 'woocommerce_account_menu_items', 'vehicle_intake_form_link_my_account' );
function vehicle_intake_form_link_my_account( $items ) {
if( check_user_role( array('premium_member') ) ) {
$items['vehicle-intake-form'] = __('Vehicle Intake Form');
}
return $items;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
i'm using WP User Frontend Pro plugin
i want to echo the pack title using a shortcode to put it in bakery visual.
all what i know is : this is the title $pack->post_title;
$pack is coming from here :
public function current_pack() {
global $pack;
$pack = $this->pack;
if ( ! isset( $this->pack['pack_id'] ) ) {
$pack_page = get_permalink( wpuf_get_option( 'subscription_page', 'wpuf_payment' ) );
return new WP_Error( 'no-pack', sprintf( __( 'You must purchase a subscription package before posting', 'wp-user-frontend'), $pack_page ) );
}
// seems like the user has a pack, now check expiration
if ( $this->expired() ) {
return new WP_Error( 'expired', __( 'The subscription pack has expired. Please buy a pack.', 'wp-user-frontend' ) );
}
return $pack;
}
i try to do something like this :
function wpc_shortcode_pack_title() {
global $pack;
return $pack->post_title;
}
add_shortcode( 'sub_name', 'wpc_shortcode_pack_title' );
to explain more
the slected code in line 5 is working correctly in the plugin pages
but i want it as a shortcode
but it didn't work
any help please ?
The callback function of add_shortcode() should return the content, not print it.
Meaning, you have to return $pack->post_title instead of echo $pack->post_title.
Like so:
function wpc_shortcode_pack_title() {
global $pack;
return $pack->post_title;
}
add_shortcode( 'sub_name', 'wpc_shortcode_pack_title' );
Edit: After taking a look at the source of “WP User Frontend Pro”:
$pack seems to be getting its value from WPUF_Subscription::get_subscription() passing the subscription id, which basically gets the post with that id.
The subscription id seems to be getting its value from WPUF_Subscription::get_user_pack() passing the user id.
So, I guess you could call get_current_user_id() and try something like this:
function wpc_shortcode_pack_title() {
$user_id = get_current_user_id();
if ( ! class_exists( 'WPUF_Subscription' ) ) {
return 'WP User Frontend Pro is not installed/activated';
}
$user_sub = WPUF_Subscription::get_user_pack( $user_id );
$pack = WPUF_Subscription::get_subscription( $user_sub['pack_id'] );
return $pack->post_title;
}
add_shortcode( 'sub_name', 'wpc_shortcode_pack_title' );
Edit #2: To get the expire date as well, you would do something similar:
function wpc_shortcode_pack_title() {
$user_id = get_current_user_id();
if ( ! class_exists( 'WPUF_Subscription' ) ) {
return 'WP User Frontend Pro is not installed/activated';
}
// Get WPUF subscription/pack
$user_sub = WPUF_Subscription::get_user_pack( $user_id );
$pack = WPUF_Subscription::get_subscription( $user_sub['pack_id'] );
// Get expiration date
$expire = ( $user_sub['expire'] == 'unlimited' ) ? ucfirst( 'unlimited' ) : wpuf_date2mysql( $user_sub['expire'] );
return sprintf(
'Subscription name: %1$s | Expire date: %2$s',
$pack->post_title,
wpuf_get_date( $expire )
);
}
add_shortcode( 'sub_name', 'wpc_shortcode_pack_title' );
I have problem with woocommerce order in admin I want the billing_address_2 show at the end of the page as exmple bellow.
can any one please help me.
The core file that is responsible to displayinng that fields is located in WooCommerce plugin under: includes/admin/meta-boxes/class-wc-meta-box-order-data.php.
The only available and efficient hook is: woocommerce_admin_shipping_fields.
But you will only be able to change the admin billing fields order using something like:
add_filter( 'woocommerce_admin_billing_fields' , 'change_order_admin_billing_fields' );
function change_order_admin_billing_fields( $fields ) {
global $the_order;
$address_2 = $fields['address_2'];
unset($fields['address_2']);
$fields['address_2'] = $address_2;
return $fields;
}
Which will give you something like:
So as you can see you will not get the billing address_2 field to be displayed after the transaction ID as you wish, but only under the billing phone field.
Addition - Showing the billing_address_2 field before billing_country field:
add_filter( 'woocommerce_admin_billing_fields' , 'change_order_admin_billing_fields' );
function change_order_admin_billing_fields( $fields ) {
global $the_order;
$sorted_fields = [];
$address_2 = $fields['address_2'];
unset($fields['address_2']);
foreach ( $fields as $key => $values ) {
if( $key === 'country' ) {
$sorted_fields['address_2'] = $address_2;
}
$sorted_fields[$key] = $values;
}
return $sorted_fields;
}
Alright, I've been pulling my hair out the last couple of days trying to figure this out.
I have a wholesale plugin in wordpress install with woocommerce. It gives the user "wholesale_customer" special rates over everyone else. I want to be able to offer local delivery to only the "wholesale_customer" user role but can't seem to figure out how to do it.
I've gotten this code from #mcorkum but it's still not working.
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
I know this achievable with a plugin, but I'd rather not use plugins or pay for it for that matter.
Does anyone see anything that I'm not seeing?
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
Try the above code by pasting it in your theme's functions.php file. And let me know if this worked for you.
I'm not a wordpress dev, but that code doesn't look like it is providing a user with role "wholesale_customer" the "local_delivery" option. On the contrary in fact, it looks to be removing the local delivery option if the user role IS "wholesale_customer":
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
If I was to take this code simply at face value (As I am not a wordpress dev) I would re-write this function to be easier to understand and read:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
Hope someone else with experience in woocomerce, can give a better answer. But in the meantime, you can try this re-write and see if it works for you.
Goodluck.
I have created a special form within my site that allows my users to enter a key. I am using add_user_meta() to add the meta data into the database. I want to be able to see this key when I click on users in the admin center.
How would I go about adding to this column?
Below is the meta data info im using
add_user_meta($userId,'code','12345');
We just want to be able to add it to the view on users.php in the table displaying username email and role.
I have used code like this to display the user id but I can not figure out how to display their meta.
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
$columns['user_id'] = 'User ID';
return $columns;
}
add_action('manage_users_custom_column', 'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
if ( 'user_id' == $column_name )
return $user_id;
return $value;
}
This example was created with the help of these two pages from the WordPress codex.
https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile
https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update
It is for displaying and updating the custom user meta data.
<?php
// Hooks near the bottom of profile page (if current user)
add_action('show_user_profile', 'custom_user_profile_fields');
// Hooks near the bottom of the profile page (if not current user)
add_action('edit_user_profile', 'custom_user_profile_fields');
// #param WP_User $user
function custom_user_profile_fields( $user ) {
?>
<table class="form-table">
<tr>
<th>
<label for="code"><?php _e( 'Custom Meta' ); ?></label>
</th>
<td>
<input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'code', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
</table>
<?php
}
// Hook is used to save custom fields that have been added to the WordPress profile page (if current user)
add_action( 'personal_options_update', 'update_extra_profile_fields' );
// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user)
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
function update_extra_profile_fields( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) )
update_user_meta( $user_id, 'code', $_POST['code'] );
}
?>
The answer above from Mordred worked for me after changing the second add_filter to add_action. Here's the modified code:
function yoursite_manage_users_columns( $columns ) {
// $columns is a key/value array of column slugs and names
$columns[ 'custom_field' ] = 'Subscription';
return $columns;
}
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
switch ( $column_key ) {
case 'custom_field':
$value = get_user_meta( $user_id, 'custom_field', true );
return $value;
break;
default: break;
}
// if no column slug found, return default output value
return $output;
}
add_action( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
To add custom user_meta fields to users.php you need to do the following:
function yoursite_manage_users_columns( $columns ) {
// $columns is a key/value array of column slugs and names
$columns[ 'custom_field' ] = 'Subscription';
return $columns;
}
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
switch ( $column_key ) {
case 'custom_field':
$value = get_user_meta( $user_id, 'custom_field', true );
return $value;
break;
default: break;
}
// if no column slug found, return default output value
return $output;
}
add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
Realising this is a bit of an old thread, however I was stuck on a very similar problem, and thought I would share what I found which proved to be a very simple solution.
<?php
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
$columns['user_id'] = 'User ID';
return $columns;
}
add_action('manage_users_custom_column', 'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
if ( 'user_id' == $column_name )
return $user_id;
return $value;
}
?>
Credit: https://pippinsplugins.com/add-user-id-column-to-the-wordpress-users-table/
To make the additional woocommerce fields editable you can use the following filter (in the example a custom field is added in the billing section).
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields)
{
if (isset($fields['billing']['fields'])) {
$fields['billing']['fields']['your_custom_meta'] = array(
'label' => __('Friendly name', 'woocommerce'),
'description' => ''
);
}
return $fields;
}