I have been experimenting all day and researching the whole web, and I cant seem to get this action to work. Basically I'm trying to trigger a Woo Email when a custom order action is selected. In this case, its a gift receipt.
Please Note: When I turn on debugging, I get a headers already sent notice, none when its off.
Here is the code which I had tried:
function gift_receipt_add_order_meta_box_action($actions)
{
global $theorder;
$actions['send_gift_receipt'] = __('Send Gift Receipt', 'enyc');
return $actions;
}
add_action('woocommerce_order_actions', 'gift_receipt_add_order_meta_box_action');
function gift_receipt_wc_process_order_meta_box_action()
{
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if (!empty($mails))
{
foreach ($mails as $mail)
{
if ($mail->id == 'wc_gift_order_email')
{
$mail->trigger($order->id);
}
}
}
}
add_action('woocommerce_order_action_send_gift_receipt', 'gift_receipt_wc_process_order_meta_box_action');
Thanks.
function gift_receipt_wc_process_order_meta_box_action()
is missing $order
function gift_receipt_wc_process_order_meta_box_action($order)
might this be the issue?
So I figured it out after some more coffee. The problem was 2 fold:
1) I did not pass the order ($order) info to the function gift_receipt_wc_process_order_meta_box_action()
2) the id (name) of the email was actually 'wc_gift_order' instead of 'wc_gift_order_email'
Thanks for the help!
Related
I am trying to write a wordpress filter that would change the post status to trash if it contains explicit words, but I can't manage to get it to work. Could you please help me?
This is what I got so far:
add_filter('wp_insert_post_data', 'delete_invalid_posts', '99');
function delete_invalid_posts($data) {
$false_titles = array("*****", "******");
if (in_array($data['post_title'], $false_titles) {
// If post data is invalid then
$data['post_status'] = 'trash';
}
return $data;
}
If you want to search the title for Explicit Words, you may use this code:
add_filter('wp_insert_post_data', 'delete_invalid_posts', 99);
function delete_invalid_posts($data) {
$false_titles = array("*****", "******");
$title_arr = explode(' ', $data['post_title']);
$found = array_intersect($false_titles, $title_arr);
if (!empty($found)) {
$data['post_status'] = 'trash';
}
return $data;
}
I've not tested the code, So try it and if you have any question don't hesitate to ask.
I might be wrong, but I think you are missing a closing parentheses here...?
Are you getting an error message?
if (in_array($data['post_title'], $false_titles) // <--- HERE should be a ")"
Like I said, I could be mistaken or there may be other issues...
I have borrowed this code from stackoverflow in attempt to find a solution. I need to create a new post from submitted data on a form. I was using the filter:
add_filter( 'wpcf7_posted_data', 'save_new_booked_event_data' );
but found that it fired after every submit and not AFTER validation. So I moved on to this code:
add_action('wpcf7_before_send_mail','contactform7_before_send_mail',1);function
contactform7_before_send_mail( $contact_form ) {
if ( !isset($contact_form->posted_data) && class_exists('WPCF7_Submission') ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$formData = $submission->get_posted_data();
$formField = $formData['_wpcf7'];
update_post_meta(199, 'first_name', $formField);
}
} else {
// We can't retrieve the form data
return $contact_form;
}
return $contact_form;
}
Five hours later: the get_posted_data method is always NULL. I ve tried approaching it from different angles including turning off ajax and posting the form. I've checked and dumped every object to make sure that it works but it always turns up NULL. What am I missing here? Please anyone?
Thank you!
Educated guess:
The get_posted_data method relies on setup_posted_data firing, first, and that occurs when the constructor for the WPCF7_Submission class is invoked.
Perhaps there are multiple contact forms and your contactform7_before_send_mail function is hooked to one of them that hasn't been submitted, and therefore, the data are null.
That being the case, you could override and force population of posted data for your form if you pass an instance of WPCF7_ContactForm to the WPCF7_Submission::get_instance(); call.
Something like:
$form = new WPCF7_ContactForm($id_of_your_form);
$submission = WPCF7_Submission::get_instance($form);
By the way! Your snippet appears to be from this wordpress plugin. (Form to Post) In case you want a more configurable solution, or to browse the context it lives in.
Im trying to create an action when a user meta value is updated, another meta value of the same user will be updated. Like this:
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
update_user_meta($user_id, 'meta_to_store', $_POST['my_meta_stored_field']);
}
I can not understand the procedure, any ideas? Thanks in advance;
NOTE: Now works! Thanks Amin.T
Its not the right hook and you are missing some functions parameters, so you need something like this:
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
$new_value = "new_value";
update_user_meta($user_id, 'other_meta', $new_value);
}
i am trying to limit the post for authors , currently i am allowing 2 post no more then 2 post , currently i have few users with more then 2 post so how do i fix my code so it dont allow them to make any more post.
if ($post_count < 2) {
return TRUE;
} else {
echo 'You have already posted your maximum number of posts.';
return FALSE;
}
}
as i said few authors have more then 2 post so using this code will allow them to make post because the if stating wil be bypassed because its trying to detect value 2 since they gone past that.
You should use wp_insert_post_data filter. And in this filter define your conditions.
So it will be something like that:
add_filter('wp_insert_post_data', 'aa_func_20150916080916', 20, 2);
function aa_func_20150916080916($data , $postarr)
{
$warn = "You have already posted your maximum number of posts";
$current_user = get_current_user_id();
$user_post_count = count_user_posts($current_user);
if($user_post_count >2 ) {
return wp_die($warn);
}
return $data;
}
Please note: it's quick answer, you should change code for your purposes.
Docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
I have created a plugin which has a separate page for order listing.. in that it looks like the same as WooCommerce's Order Listing page but. i am unable to get the comments of the order so i added my custom post type to wc_order_types after that there is no order listed.. its showing a empty table. ?
add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3);
public function add_wc_order_types($order_types,$type){
$order_types[] = WC_QD_PT;
return $order_types;
}
apply_filters( 'wc_order_types', $order_types, $for ); is default wc_filters which takes 2 parameters you have asked for 3 here add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3); and again supplied 2.
visit http://docs.woothemes.com/wc-apidocs/source-function-wc_get_order_types.html#149 It may help you do this.
I Solved The issue just by adding a if condition in my hook's function
function add_wc_order_types($order_types,$type){
$order_type = $order_types;
if('' == $type){
$order_type[] = WC_QD_PT;
}
return $order_type;
}