custom plugin bombing Edit Order page when trying wc_get_order - woocommerce

I'm writing a tiny custom plugin to add a meta box so I can freely interface with EasyPost. It's included in the Edit Order page. I'm trying to use wc_get_order to get basic order details and the page is bombing. This looks like it should work flawlessly and be very simple, but I can't find the answer anywhere.
I'm using this code in:
/wp-content/plugins/custom-easypost/jens-custom-easypost.php
Sorry if this is a dumb question, but I'm stumped.
This code is NOT WORKING:
$order = wc_get_order( $order_id );
This code IS working:
// Add meta box
add_action( 'add_meta_boxes', 'jens_epbox' );
function jens_epbox() {
add_meta_box(
'jens-epbox-modal',
'Jens EP Box',
'jens_meta_box_callback',
'shop_order',
'side',
'high'
);
}
// Callback
function jens_meta_box_callback( $post )
{
echo 'Get Label ' . $post->ID. '';
}

Related

How to update Order meta data, from a WooCommerce Action

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

Removing the my account-dashboard and redirect to another endpoint like downloads

I don't want the customers to get linked to the myaccount-dashboard with the annoying Hello xy, from here you can blabla, when they click on "My Account".
Is there an endpoint for the my account dashboard? I didn't find it on the backend in woocommerce>Settings>Accounts...
What's working is: I set up a custom link under menu/navigation... called it "My Account" and set the link to /myaccount/downloads for example.
So, when customers are logged in and they click on My Account, they get redirected to Downloads.
I'm wondering, is there another way to get rid of the dashboard?
Or a redirect solution? Thanks.
Remove it using below function. change downloads to your requirement.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Looking more closely it seems like there are a handful of ways to do this. Take a look at the my-account.php template. You could override this, but I found that if you remove the woocommerce_account_content hook, you get a deprecation notice and WooCommerce thinks you have an outdated template and adds the content anyway.
Looking at the template you will see two hooks. The side menu navigation is added to the woocommerce_account_navigation and the content is added to woocommerce_account_content function. You can remove the defaults from their hooks and add back just the downloads content.
function so_41983566_remove_account_dadshboard(){
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
remove_action( 'woocommerce_account_content', 'woocommerce_account_content' );
add_action( 'woocommerce_account_content', 'so_41983566_download_content' );
}
add_action( 'woocommerce_account_navigation', 'so_41983566_remove_account_dadshboard', 1 );
function so_41983566_download_content(){
do_action( 'woocommerce_account_downloads_endpoint' );
}
Or woocommerce_account_content() and woocommerce_account_navigation() are both pluggable functions and you can just define new versions in your theme/plugin.
This link explains everything:
https://github.com/woocommerce/woocommerce/wiki/Customising-account-page-tabs
First you need to create an endpoint:
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
Then create the menu item:
function my_custom_my_account_menu_items( $items ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
Add content to the endpoint
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );

Adding author info outside the loop in Genesis

I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.
I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.
// Remove entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
// Remove post info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Add page title
add_action( 'hero-info', 'genesis_do_post_title' );
// Add page info
add_action( 'hero-info', 'genesis_post_info', 12 );
To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info
function author_shortcode() {
global $post;
$author_id=$post->post_author;
the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
This shortcode [author] is then added into
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
if ( is_archive() || is_home() ) {
$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );
return $post_info;
}
}
This is the result now: http://imgur.com/a/6lX5J
It is shown in the wrong place for some reason. Anybody knows how this can be?
The site can be found here: http://websforlocals.com/business/
Hope I gave enough info, and that someone with the same problem can be helped out.
It is issue in your ShortCode registering php code.
When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.
So always return the output in short code function, and then echo the short-code function.
Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).
We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.
function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

Woocommerce override plugin action

The file email-order-items.php has the following line of code:
echo "\n" . sprintf( __( 'Cost: %s', 'woocommerce' ), $order->get_formatted_line_subtotal( $item ) );
The following action hook has been added to a plugin I am using (Woocommerce Composite Products):
add_action( 'woocommerce_order_formatted_line_subtotal', array( $this, 'wc_cp_order_item_subtotal' ), 10, 3 );
I would like to override the function wc_cp_order_item_subtotal to change the way to item subtotal is displayed. I have tried adding the following to my child theme functions.php, but it doesn't do anything.
remove_action( 'woocommerce_order_formatted_line_subtotal', 'wc_cp_order_item_subtotal', 10);
add_action( 'woocommerce_order_formatted_line_subtotal', 'child_wc_cp_order_item_subtotal', 10, 3);
function child_wc_cp_add_order_item_meta( $order_item_id, $cart_item_values, $cart_item_key = '' ) {
return 'xxxxxxx';
}
Any tips to help me get this working would be greatly appreciated.
It isn't mentioned in the Codex, but I usually call the remove_action() function from a hook.
Also, as covered in the Codex example for action added from classes, you need to access the class instance or variable.
I don't see wc_cp_order_item_subtotal in the Composite plugin anywhere, so I presume you aren't using Woo's version. In which case, I don't have access to the code and can't tell you exactly how to access the class variable.
But if you were using Woo's Composite Products it would be as follows:
Edited for Composites 2.4
function so_remove_action(){
global $woocommerce_composite_products;
remove_action( 'woocommerce_order_formatted_line_subtotal', array( $woocommerce_composite_products->order, 'wc_cp_order_item_subtotal' ), 10, 3 ); //not sure why it isn't a filter, but also not sure if there is a huge difference
}
add_action('init', 'so_remove_action');

Hide or remove comments box from wordpress pages

I am developing a plugin everything is going fine but there is a problem.
How can I hide or remove the comments box? I lost many hours but could not develop the right script. My current script is this:
add_filter( 'comments_template', 'remove_comments_template_on_pages', 11 );
function remove_comments_template_on_pages( $file ) {
if ( is_page() )
comments_template('', true);
}
I don't know how to develop this script. Any help could lead me to complete my plugin.
Add the below code to you functions.php file.
add_action('init', 'remove_page_feature');
function remove_page_feature() {
remove_post_type_support( 'page', 'comments' );
}
And now provide a check before the comments template is executed, as below:
if ( post_type_supports( 'page', 'comments' ); )
comments_template('', true);
}
Note: Using the above code will disable comments on pages.

Resources