Actually, I'm working on woocommerce customization...
I've read this question about the title change in metabox:
Customizing WooCommerce Short Description Metabox title
It help me a lot. But, in the product, how can I find the "metabox tag" callback name?
function epptm_rename_meta_boxes(){
remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );
add_meta_box( 'tagsdiv-product_tag', __( 'This metabox is awesome', 'your-plugin' ), 'CALLBACK?', 'product', 'side' );
}
add_action( 'add_meta_boxes', 'epptm_rename_meta_boxes', 40 );
here is another solution found:
/**
* hook to the 'add_meta_boxes' action to modify title
*/
function change_meta_box_titles() {
global $wp_meta_boxes; // array of defined meta boxes
// cycle through the array, change the titles you want
$wp_meta_boxes['product']['side']['core']['tagsdiv-product_tag']['title']= 'Here is my new title';
}
add_action('add_meta_boxes', 'change_meta_box_titles');
Related
I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages
I am using a wordpress theme which supports woocommerce,
when adding a user with shop manager role i don't want to show the woocommerce menu.
Just need the products menu only.
please help.
You can use WordPress's 'remove_menus()' function to do this.
Store Managers have a capability: 'manage_woocommerce'
You can see that they are allowed to see the WooCommerce admin menu here:
'/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-menus.php'
Look for: $main_page = add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce' , array( $this, 'settings_page' ), null, '55.5' );
So much for the theory. To stop this admin menu item from displaying for anyone but an Administrator, add this to your functions.php file or plugin:
add_action( 'admin_menu', 'remove_menus' );
function remove_menus(){
// If the current user is not an admin
if ( !current_user_can('manage_options') ) {
remove_menu_page( 'woocommerce' ); // WooCommerce admin menu slug
}
}
Don't have the rep points to add a comment, but needs to change the hooked action from:
add_action( 'admin_menu', 'remove_menus' );
to:
add_action( 'admin_init', 'remove_menus' );
and then you can do something like:
function remove_menus(){
// If the current user is not an admin
if ( !current_user_can('manage_options') ) {
remove_submenu_page('woocommerce', 'wc-status');
}
}
if you are trying to remove core woocommerce submenu items.
(responding to Do Xuan Nguyen's comment)
I would like to add few input fields for the orders in WooCommerce/Wordpress. The fields will be only visible on the admin page. How to do that? Is it same like the pages/posts?
For the post types above I used add_post_meta. Is it the best way for that, or is there any hook for order fields?
Yes, just add_meta_box() to the post type shop_order and proceed as normal.
add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes()
{
add_meta_box(
'woocommerce-order-my-custom',
__( 'Order Custom' ),
'order_my_custom',
'shop_order',
'side',
'default'
);
}
function order_my_custom()
{
echo '<h1>Sample meta box</h1>';
}
Because you're asking specifically about WooCommerce, I'd like to expand on brasofilo's accepted answer by showing how you'd get the relevant WooCommerce order object (WC_Order) for the page the meta box is on.
Here's how you'd display the billing email from the order:
add_action('add_meta_boxes', function() {
add_meta_box('woocommerce-order-meta-24179529',
__( 'My Meta Box Title' ),
function() {
global $post;
// wc_get_order() returns one of bool|WC_Order|WC_Order_Refund
$order = wc_get_order($post->ID);
if($order instanceof WC_Order) {
echo "Billing email: ";
echo $order->get_billing_email();
}
},
'shop_order',
'side',
'default'
);
});
I want to remove some meta boxes like:
Product Short description,
Reviews
I can remove default metaboxes:
function remove_metaboxes() {
remove_meta_box( 'postcustom' , 'product' , 'normal' );
remove_meta_box( 'postexcerpt' , 'product' , 'normal' );
remove_meta_box( 'commentsdiv' , 'product' , 'normal' );
remove_meta_box( 'tagsdiv-product_tag' , 'product' , 'normal' );
}
add_action( 'admin_menu' , 'remove_metaboxes' );
But I cant remove "postexcerpt" - Product Short description and "commentsdiv" - Reviews, because they are loaded in add_filter - add_meta_boxes
Is there any other hook after this to apply my script ? Or maybe there is another method ?
Thank you!
WooCommerce removes the default postexcerpts and replaces it with its own version (the 'Product Short Description' meta box) (class-wc-admin-meta-boxes.php)
So like user1139767 said, you have to alter the priority. However when I tried 11, it didn't work, neither did 20. But 50 seems to do the trick:
function remove_metaboxes() {
remove_meta_box( 'postcustom' , 'product' , 'normal' );
remove_meta_box( 'postexcerpt' , 'product' , 'normal' );
remove_meta_box( 'commentsdiv' , 'product' , 'normal' );
remove_meta_box( 'tagsdiv-product_tag' , 'product' , 'normal' );
}
add_action( 'add_meta_boxes' , 'remove_metaboxes', 50 );
function remove_my_metaboxes() {
remove_meta_box( 'categorydiv','post','normal' ); // Categories Metabox
remove_meta_box( 'submitdiv','post','normal' ); // Categories Metabox
remove_meta_box( 'postcustom','page','normal' ); // Custom Fields Metabox
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'commentstatusdiv','page','normal' ); // Comments Metabox
remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','page','normal' ); // Talkback Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
remove_meta_box( 'authordiv','page','normal' ); // Author Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'postexcerpt','page','normal' ); // Excerpt Metabox
remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
remove_meta_box( 'slugdiv','page','normal' ); // Slug Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'formatdiv','post','normal' ); // Formats Metabox
remove_meta_box( 'postimagediv','post','normal' ); // Featured Image Metabox
remove_meta_box( 'tagsdiv-post_tag','post','normal' ); // Tags Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
}
add_action('admin_menu','remove_my_metaboxes');
just comment out "remove_meta_box" what you want to display in your page/posts.
Also we able to remove the meta boxes by changing on your custom post types name into the remove_meta_box function instead of "post" or "page".
remove_meta_box( 'tagsdiv-product_tag' , 'product' , 'normal' );
isn't correct, use instead :
remove_meta_box( 'tagsdiv-product_tag','product','side' );
to remove the box 'keywords product'
just added priority to add_action:
add_action( 'add_meta_boxes' , 'remove_metaboxes', 11 );
The default priority is 10, so I added 11 to make action after 10.
To remove WooCommerce product categories metabox, add this to functions.php:
add_action('add_meta_boxes_product', 'bbloomer_remove_metaboxes_edit_product', 9999);
function bbloomer_remove_metaboxes_edit_product()
{
// e.g. Remove WooCommerce product categories metabox
remove_meta_box('product_catdiv', 'product', 'normal');
}
I spent hours on this to remove the WooCommerce short description metabox (and then add it up above the main content editor). I could ONLY get rid of the postexcerpt by using the add_meta_boxes hook. admin_menu and adminhead ran too early. So I removed everything else at the same time.
function WH_remove_meta_boxes() {
remove_meta_box( 'postexcerpt', 'product', 'normal' );
remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );
remove_meta_box( 'tagsdiv-yith_shop_vendor', 'product', 'side' );
remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );
remove_meta_box( 'wpseo_meta', 'product', 'normal');
}
add_action( 'add_meta_boxes', 'WH_remove_meta_boxes', 99 );
I'm working on a plugin and I'm trying to:
1- move the publish metabox from side to the bottom of the page in the normal section.
2- force 1 column layout for the plugin custom post type edit page.
3- remove the screen options tab for the custom post type.
I'm using the next code block but it dont work:
function sds_do_meta_boxes() {
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'core' );
}
add_action('do_meta_boxes', 'sds_do_meta_boxes');
function SDS_init() { //The current user is already authenticated by this time
add_filter( 'screen_layout_columns', 'so_screen_layout_columns' );
add_filter( 'get_user_option_screen_layout_dashboard', 'so_screen_layout_dashboard' );
add_filter( 'screen_options_show_screen', 'SDS_remove_screen_options_tab');
}
add_action( 'init', 'SDS_init' );
function so_screen_layout_columns( $columns ) {
$columns['dashboard'] = 1;
return $columns;
}
function so_screen_layout_dashboard() {
return 1;
}
function SDS_remove_screen_options_tab() {
return false;
}
So, the 'publish' metabox is removed but it is not re-added. Also, the 1 column layout filters don't work. I need help:)
Changing the $priority parameter from core to high and setting priority of add_action to 0 should do the trick. I would also suggest using the dynamic add_meta_boxes_{$post_type} hook:
add_action( 'add_meta_boxes_sliding_panel', 'sds_do_meta_boxes', 0, 1 );
function sds_do_meta_boxes( $post )
{
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'high', null );
}