Add a new block to Woocommerce admin order page - wordpress

I want to create a custom table block in Woocommerce admin order page.
As you can see in a screenshot, I have used :
add_action( 'woocommerce_admin_order_data_after_order_details', 'vp_admin_order_table' );
What I want is to create a separate block, with this table inside it.
Is there any action to trigger gap between Order details and Products list?

Try this code:
function op_register_menu_meta_box() {
add_meta_box(
'Some identifier of your custom box',
esc_html__( 'Box Title', 'text-domain' ),
'render_meta_box',
'shop_order', // shop_order is the post type of the admin order page
'normal', // change to 'side' to move box to side column
'low' // priority (where on page to put the box)
);
}
add_action( 'add_meta_boxes', 'op_register_menu_meta_box' );
function render_meta_box() {
// Metabox content
echo '<strong>Your awesome content goes here</strong>';
}
Remember to set to box as visible under "Screen Options" on the order page.
Further reading:
https://developer.wordpress.org/reference/functions/add_meta_box/
https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes

Related

Wordpress change metabox title

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');

Change woocommerce cart link in menu [duplicate]

On Woocommerce, how can we change the URLs on "View cart" and "Checkout" links on the drop down menu that show up on hover over the shopping cart icon on the the home page?
I have the "cart" and "checkout" pages setup but they are not linked to these.
I can view these pages directly with urls. http://mysite/cart and http://mysite/checkout
It seems that there is a problem somewhere with your theme (or in a plugin), as the minicart button links always point to the right cart and checkout pages.
The minicart buttons are hooked in woocommerce_widget_shopping_cart_buttons action hook (in the cart/mini-cart.php WooCommerce template). You will find the details HERE on includes/wc-template-hooks.php core file. It calls 2 functions that are displaying the buttons.
First you should try to refresh WordPress Permalinks, going on WP Settings > Permalinks:
Just at the end of the page click on "save". Empty your cart, and try it again to see if it changes something.
In the code below I remove first the original buttons and I replace them by the same ones where the links are customized. For each you can change the link to feet your needs (I have added in the links ?id=1 (at the end) just for testing purpose, to check changes):
add_action( 'woocommerce_widget_shopping_cart_buttons', function(){
// Removing Buttons
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
// Adding customized Buttons
add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_widget_shopping_cart_button_view_cart', 10 );
add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_widget_shopping_cart_proceed_to_checkout', 20 );
}, 1 );
// Custom cart button
function custom_widget_shopping_cart_button_view_cart() {
$original_link = wc_get_cart_url();
$custom_link = home_url( '/cart/?id=1' ); // HERE replacing cart link
echo '' . esc_html__( 'View cart', 'woocommerce' ) . '';
}
// Custom Checkout button
function custom_widget_shopping_cart_proceed_to_checkout() {
$original_link = wc_get_checkout_url();
$custom_link = home_url( '/checkout/?id=1' ); // HERE replacing checkout link
echo '' . esc_html__( 'Checkout', 'woocommerce' ) . '';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works.

Woocommerce shop manager role, hide woocommerce menu

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)

How to add custom fields for WooCommerce order page (admin)?

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'
);
});

Displaying Meta Box for Categories

I am trying to just display the wordpress categories in a metabox on a different page, the page is custom.
Like a blank page and I am trying to add the wordpress metabox on that page, and be able to add new categories.
Screencast example of what I want to achieve.
It's very simple, just call the following function
add_action( 'init', 'add_cat_meta' );
function add_cat_meta()
{
register_taxonomy_for_object_type( 'category', 'custom_page' );
}
Notice the custom_page, replace custom_page with your original custom post name, for example if you have following to register a custom post
register_post_type( 'restaurant',
array(...);
);
Now to add the category meta box for that custom post you can use
register_taxonomy_for_object_type( 'category', 'restaurant' );
Also, if you want to add the category meta box in the page section then you can use
register_taxonomy_for_object_type('category','page');
But pages are different than posts and don't require a category.

Resources