Which hooks does WooCommerce used to initiate saving post details and meta details? - wordpress

I am struggling to know how WooCommerce initiate the saving of post and post meta from the dashboard? Please help me with this.
I was able to find the insert code here:
includes/class-wc-order.php
public function save() {
//statement
}
Thanks

May be this one can be helpful to others..
WooCommerce use post meta to add extra information on default WordPress post. Hence every time we update or create order from WordPress admin, WordPress initiate from this action...
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
Location: woocommerce/includes/class-wc-admin-meta-boxes.php
Everything happens from this hooks.
Note: As this is not related to aboven question but the best hooks to process any action on custom meta box is:
woocommerce_order_object_updated_props
Thanks

Related

Send WooCommerce Email on Address Save using woocommerce_email_actions

I've been struggling with this for a couple days now and have been through every post/comment/discussion/etc... I could find trying to find a working solution.
I want to send an email via a custom class that extends WC_Email whenever a woocommerce user updates their address. I've found various resources explaining how to create custom wc emails (skyverge was most useful) and I have successfully done that. I have a plugin that adds a custom email in WP-Admin->WooCommerce->Settings->Emails.
If I use an action that is already part of the woocommerce_email_actions such as add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) ); and manually change the order status in the backend everything works just fine.
Problem is I want to use add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) ); and unfortunately it never fires.
Based on some other threads I've tried adding the following to my main plugin file
function new_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_customer_save_address';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'new_woocommerce_email_actions' );
Supposedly this should allow me to use the action in my custom class, but no luck. I've also tried adding other actions without any success. For instance using the filter to add woocommerce_order_status_cancelled won't fire when manually changing the order to cancelled. I'm struggling to figure out why this isn't working and most of the the threads I've found are 2+ years old and dead, so here I am. Any help or pointers would be greatly appreciated.
Posting as an answer in case others are trying to get this functionality working.
I found this article from Tyche Softwares after extensive digging and was able to create a plugin that will send a custom WC_Email whenever a customer updates their address information.
I'm still not 100% as to why the add_filter('woocommerce_email_actions'... wasn't working, and if anyone can tell me why I'd still be interested.
This plugin works by calling a custom action defined in my extended WC_Email class via do_action whenever the existing woocommerce_customer_save_address action happens.

Manipulate woocommerce product edit page

I am new to wordpress and woocommerce development and I am just wondering, how to manipulate an admin-screen in a clean, updateable way.
For example, I want to add a custom field to a product edit page (see screen):
I know, that I have to write a custom extension, but is it possible, to manipulate admin-screens of other extensions? I couldn't find any suitable tutorial? Maybe someone has a hint, where to start?
The feature of creating custom fields for products is baked right into WooCommerce, whether to implement it directly in functions.php or do the same via a plugin is left to one's sole discretion.
Remi Corson has written an excellent article detailing the same.
Here's the gist :
1.Create the fields using the woocommerce_product_options_general_product_data hook
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
// Define your fields here.
// You can create text, textarea, select, checkbox and custom fields
}
2.When product is saved save the value entered in your custom field using woocommerce_process_product_meta hook
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $_post_id ) {
//save field values
}
WooCommerce is a WordPress plugin that will help you to turn your website into an eCommerce store.
Yes, you can write an Extension ( or ADD-On) for this plugin, in fact there are already hundreds of Extension ( free and Paid ) have been made for it.
In order to create an Extension ( or ADD-ON ) for this plugin, you need to understand 2 things:
WooCommerce API
http://docs.woothemes.com/document/create-a-plugin/
WordPress API https://codex.wordpress.org/Writing_a_Plugin

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

Wordpress - remove some posts from query

I'm trying to make a plugin.
Its job is to generate and send a link to the author after a post is published by admin.
After clicking on the link will be the post actually published.
I did that after the click on the link there will be a post meta added to the post.
NOW I cant find a solution how to show only posts with the meta or ADMINS (or with some user level) posts.
I decided I need a filter bud I cant figure out how to do the ADMIN posts exceptions.
How do I filter only non-admin posts.
I think I need to remove the "bad" posts from $query but how ?
add_filter( 'pre_get_posts' , 'postsClean' );
function postsClean( $query ){
// check all posts and if the post should be not published remove it from query
}
Or is there any better way ?
If you are using wp_query you can use - for negation, e.g.
$wp_query_obj->set( 'author', '-1' );
WP_Query shows a full list of query arguments.

wordpress - catching save event with a plugin

i'm writing my first plugin for wordpress and i have some doubts regarding the hooks.
So i want an action to be executed when admin saves a post, and i'm using (inside a class):
add_action( 'save_post', array($this, 'save_post'));
function save_post(){
global $wp_query;
var_dump($wp_query);
}
the problem is that it prints the global variable when admin opens the "create a new post" and it doesn't when the user saves a post.
I want it to happen the other way around, but i can't find anything in the docs, and i'm completely alone here.
Any help? thanks
I think you're looking for the 'publish_post' action. There's also a 'draft_post' if you need it.

Resources