I am sending extra Mail after new woocommerce order.
I am using woo commerce_new_order hook.
Problem is that when the email arrives it hasn't had product info. I think that woocommerce_new_order hook fires before everything are stored in the database. Because if I run this with the existing order every info is included.
The question is how could I add a delay before data is fetched and email is sent?
add_action( 'woocommerce_new_order', 'extra_mail_after_new_order', 20, 1 );
function extra_mail_after_new_order( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$product_variation_id = $item->get_variation_id();
$product_data = $product->get_meta('extra_email')
}
add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
$to = 'mail.mail#gmail.com';
$subject = $product_name . ' uusi tilaus!';
$message = 'Order id: '. $order_id . '<br />product name: '. $product_name . '<br />product id: '. $product_id. '<br />product meta: '. $product_data. '<br />status: '. $status ;
wp_mail( $to, $subject, $message ); }
Problem was solved with this hook
woocommerce_booking_in-cart_to_pending-confirmation_notification
Related
I want a function that triggers when a new WordPress post publish.
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $post_id, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$edit = get_edit_post_link( $post_id, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
I tried this one. I tried also different statuses such as save_post but I didn't get any email when the user add a new post and publish it. so it doesn't work. how can I solve this problem?
You use the publish_post hook. If get_post_meta() returns a value, it will trigger your actions to occur.
function run_on_post_publish( $post_id ) {
$post_data = get_post_meta( $post_id, 'run_once', true );
if ( ! empty( $post_data ) ) {
return;
}
// Run the action you wish to occur when a post is published
update_post_meta( $post_id, 'run_once', true );
}
add_action( 'publish_post', 'run_on_post_publish' );
I have advanced custom field plugin and i have added a custom field to my product the label name is (stock_number) my question is how do it display/show this field data in the new order email.
The below code should do the trick.
Option1:
add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
if( $stock_number = get_field( 'stock_number', $item->get_product_id() ) ;
echo $stock_number;
}
Option2:
add_action( 'woocommerce_email_order_details', 'display_stock_email_order_details', 10, 4 );
function display_stock_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
foreach( $order->get_items() as $item ) {
if( $stock_number = get_field( "stock_number", $item->get_product_id() ) ){
echo '<p><strong>'.__('Stock Number').': </strong>'.$stock_number.'</p>';
}
}
}
Option3:
The below code will replace the Product Title with the ACF Custom Value.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
Option4:
The below code will replace the Product Name with your custom field.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php
I added this function in my themes functions.php but it just not working.. any idea what i do wrong?
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
// $ttotal = $order->get_order_total();
//echo trim( str_replace( '#', '', $order->get_order_number() ) );
//$order_id = absint( $wp->query_vars['order-received'] );
//$order = new WC_Order($GLOBALS['post']->ID);
echo $order;
}
add_shortcode('Woocommerce-Thankyou', 'woocommerce_thankyou_fun');
Use this code to see all datamember of order object.
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
echo "<pre>";
print_r( $order);
echo "</pre>";
die();
}
add_shortcode('woocommerce-thankyou', 'woocommerce_thankyou_fun',20,1);
I have the following code as a function to add the form data to the database usermeta table then send an email which all works. The issue is the form stays stuck on the loading image and I can't find anyway to get it to redirect or display a confirmation message, any help would be much appreciated. I've tried using another function with wpcf7_mail_sent but nothing happens, tried additional settings for the form and am stuck.
add_action('wpcf7_before_send_mail', 'cf7import',1);
function cf7import() {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ( $submission )
{
$posted_data = $submission->get_posted_data();
$formtitle = $cfdata->title; }
if ( $formtitle == 'Apply Form') {
}
global $wpdb;
$user_id = get_current_user_id();
update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );
global $current_user;
get_currentuserinfo();
$email_address = 'contact#website.com';
// write the email content
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . $email_address;
$subject = 'New Application Form';
$message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
$message .= "Your application has been submitted successfully";
wp_mail($current_user->user_email, $subject, $message, $header);
}
The problem with your code is that $contact_form isn't defined.
You can use something like this:
add_action('wpcf7_before_send_mail', 'cf7import', 1);
function cf7import($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ){
$posted_data = $submission->get_posted_data();
$formtitle = $contact_form->title();
}
if ( $formtitle == 'Apply Form') {
global $wpdb, $current_user;
$user_id = get_current_user_id();
update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );
get_currentuserinfo();
$email_address = 'contact#website.com';
// write the email content
$header = "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . $email_address;
$subject = 'New Application Form';
$message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
$message .= "Your application has been submitted successfully";
wp_mail($current_user->user_email, $subject, $message, $header);
}
}
Also made the code more readable and fixed some other issues (empty if conditional and undefined $header).
The code actually works as it should it was a conflict with the "WP Job Manager - Contact Listing" plugin that was stopping it from working