How to get a cookie value in Woocommerce email notifications? - php

I'm retrieving a value using php cookies from a plugin to woocommerce thankyou page and customer order detail page, it works fine on thankyou page but didn't print anything on email order detail page, how would I fix this?
I have tried fetching values using php sessions, it prints value only for 10 to 15 minutes, after that it doesn't print anything, can anyone help me to retrieve values using PHP cookie.
From here I want to fetch Post ID
<?php
if('on' == $display_ticket_number){
echo '#' . $post->ID . ' ';
}
echo $post->post_title;
$ticketid = $post->ID;
setcookie ("ticketidno",$ticketid, time() +60, "/");
?>
On thankyou.php it prints value
<?php echo $_COOKIE["ticketidno"];?>
email-order-details.php here it doesn't print
<?php echo $_COOKIE["ticketidno"];?>
Edit
I want to get and display cookie value on:
Email Notification, for emails/email-order-details.php template file on this hook:
do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>
So before Order Table.
Text SMS Plugin: plugins/woocommerce-apg-sms-notifications/includes/admin/proveedores.php
case "solutions_infini":
$respuesta = wp_remote_get( "http://api-global.solutionsinfini.com/v3/?api_key=" . $apg_sms_settings['clave_solutions_infini'] . "&method=sms" . "&to=" . $telefono . "&sender=" . $apg_sms_settings['identificador_solutions_infini'] . "&message=" . "Thanks for Registering in ". $_SESSION['post_title'] . " your Registration ID no is THR". $_COOKIE["ticketidno"] . apg_sms_codifica_el_mensaje( $mensaje ));
break;
Replacing $_COOKIE["ticketidno"]
Any help is appreciated.

Updated
You should need to grab the cookie value as custom order meta data in thankyou "Order received" page:
add_action( 'woocommerce_thankyou', 'thankyou_grab_cookie_as_meta_data', 10, 1 );
function thankyou_grab_cookie_as_meta_data( $order_id ){
if( ! $order_id ){
return;
}
if( isset($_COOKIE["ticketidno"]) && ! get_post_meta( $order_id, '_cookie_ticketidno', true ) ) {
update_post_meta( $order_id, '_cookie_ticketidno', esc_attr($_COOKIE["ticketidno"]) );
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.
The you will be able to get this grabbed cookie value using:
From the Order ID: $cookie = get_post_meta( $order_id, '_cookie_ticketidno', true );
From the Order Object: $order->get_meta( '_cookie_ticketidno' ); // (on Woocommerce 3+)
Display in email notifications:
// Email notifications display
add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 5, 4 );
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
if( $value = $order->get_meta('_cookie_ticketidno') )
echo '<p class="ticket-id">' .__('Ticket Id Number: ') . $value . '</p>';
}
Code goes on function.php file of your active child theme (or active theme).
Display on "Order received" page (thankyou):
// On "Order received" page (on start)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_custom_order_received_text', 10, 2 );
function thankyou_custom_order_received_text( $text, $order ) {
if ( $value = $order->get_meta('_cookie_ticketidno') ) {
$text .= '<br><div class="ticket-id"><p>' . __('Ticket Id Number: ') . $value . '</p></div>' ;
}
return $text;
}
Code goes on function.php file of your active child theme (or active theme).
For SMS - As this requires the order ID, try the following without any guaranty:
case "solutions_infini":
$respuesta = wp_remote_get( "http://api-global.solutionsinfini.com/v3/?api_key=" . $apg_sms_settings['clave_solutions_infini'] . "&method=sms" . "&to=" . $telefono . "&sender=" . $apg_sms_settings['identificador_solutions_infini'] . "&message=" . "Thanks for Registering in ". $_SESSION['post_title'] . " your Registration ID no is THR". get_post_meta( $_SESSION['ID'], '_cookie_ticketidno', true ) . apg_sms_codifica_el_mensaje( $mensaje ));
break;
Code should goes on proveedores.php file in your plugin, Just replacing in the code:
$_COOKIE["ticketidno"]
by:
get_post_meta( $_SESSION['ID'], '_cookie_ticketidno', true )
where $_SESSION['ID'] (I suppose and I hope) should be the order ID.

Related

After WooCommerce update, custom billing fields are not included any more

After an update of WooCommerce & WordPress a lot of custom settings was overwritten so I have tried to get back the same functionality as in the older version. (using childtheme so why it went missing in the first place I dont get)
Fixed everything except this. On the billing info at checkout several fields went missing, the default company_name I got working again , for some reason it was deactivated in the theme functions.php. However two custom fields are left for IVA number and Organization number.
So I used Checkout Manager for WooCommerce to add custom fields to the billing info.
It works on the check out page and the info end up on the order. But it doesn't show up on the thank you page and more importantly it doesn't show up on the email to customer.
Tried adding this to themes functions.php but no luck.
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_wooccm13'] = array(
'label' => __( 'Moms/IVA' ),
'value' => get_post_meta( $order->id, 'billing_wooccm13', true ),
);
return $fields;
}
And also tried this:
add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$output = '';
$billing_field_testing = get_post_meta( $order->id, 'billing_wooccm13', true );
if ( !empty($billing_wooccm13) )
$output .= '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $billing_wooccm13 . '</span></div>';
echo $output;
}
Any ideas how to go about this?
Since WoooCommerce 3 $order->id is replaced by $order->get_id()… Also there are some other ways.
Be sure that the meta key for your custom field is billing_wooccm13 (as it could be instead starting with an underscore like _billing_wooccm13).
Try the following:
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
if ( $value = $order->get_meta( 'billing_wooccm13' ) ) {
$fields['billing_wooccm13'] = array(
'label' => __( 'Moms/IVA' ),
'value' => $value,
);
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Or this too:
add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
if ( $value = $order->get_meta( 'billing_wooccm13' ) ) {
echo '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Thank you so much LoicTheAztec.
Both solutions worked but with the first one the text ended up to high up, before invoice address. The second one ended up below , unfortunately with some blank lines before but no biggie. Assume I have to use an email-template to remove those lines. The code I ended up with:
add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
if ( $value = $order->get_meta( '_billing_wooccm11' ) ) {
echo '<div><strong>' . __( "Ert organisationsnummer", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
if ( $value = $order->get_meta( '_billing_wooccm13' ) ) {
echo '<div><strong>' . __( "Ert Moms/IVA:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
}
}
}

Hide a custom action button on click displaying a text in WooCommerce admin orders list

I am creating woocommerce plugin to send order details via WhatsApp. Here is my plugin code
add_filter( 'manage_edit-shop_order_columns', 'dvs_whatsapp_msg_list_column' );
function dvs_whatsapp_msg_list_column( $columns ) {
$columns['dvs_show_whatsapp'] = 'WhatsApp';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'dvs_whatsapp_msg_list_column_content' );
function dvs_whatsapp_msg_list_column_content( $column ) {
global $post;
if ( 'dvs_show_whatsapp' === $column ) {
$order = wc_get_order( $post->ID );
$firstname = $order->get_billing_first_name();
$lastname = $order->get_billing_last_name();
$phone = $order->get_billing_phone();
$ordernum = $order->get_order_number();
$total = $order->get_total();
$payment = $order->get_payment_method_title();
$country = $order->get_billing_country();
$calling_code = WC()->countries->get_country_calling_code($country);
$whatsappnum = $calling_code.$phone;
$msg = 'Hello ' .$firstname. ' ' .$lastname. ', your order #' .$ordernum. ' has been received. The order amount is ' .$total. '. Your payment method is ' .$payment. '. Please contact us if you have any question regarding your order. Thank you.';
echo 'Send WhatsApp';
}
}
This is output
I want when shop manager or admin click the Send Whatsapp link then it will hide the link and show Message sent so shop manager or admin can know the details of this msg is already sent.
Please help.
Javascript is not the way to achieve this. You will use the following instead to hide the link and display "Message sent" once an external link has been clicked:
add_filter( 'manage_edit-shop_order_columns', 'dvs_whatsapp_msg_list_column' );
function dvs_whatsapp_msg_list_column( $columns ) {
$columns['whatsapp'] = __('WhatsApp', 'woocommerce');
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'dvs_whatsapp_msg_list_column_content' );
function dvs_whatsapp_msg_list_column_content( $column ) {
if ( 'whatsapp' === $column ) {
global $the_order;
if( ! $the_order->get_meta('_wapp_sent') ) {
echo '' . __("Send WhatsApp") . '';
}
else {
echo __("Message sent", "woocommerce");
}
}
}
add_action( 'admin_init', 'dvs_redirect_whatsapp_send' );
function dvs_redirect_whatsapp_send() {
global $pagenow;
# Check current admin page.
if ( $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order'
&& isset($_GET['send']) && $_GET['send'] == 'dvs_whatsapp' && isset($_GET['order_id']) && $_GET['order_id'] > 0 ) {
$order = wc_get_order( $_GET['order_id'] );
$msg = sprintf( __("Hello %s %s, your order #%s has been received. The order amount is %s. Your payment method is %s. %s", "woocommerce"),
$order->get_billing_first_name(),
$order->get_billing_last_name(),
$order->get_order_number(),
$order->get_total(),
$order->get_payment_method_title(),
__("Please contact us if you have any question regarding your order. Thank you.", "woocommerce")
);
$whatsapp_num = WC()->countries->get_country_calling_code( $order->get_billing_country() ) . $order->get_billing_phone();
update_post_meta( $_GET['order_id'], '_wapp_sent', 'true' ); // Mark order as WhatsApp message sent
wp_redirect( 'https://wa.me/' . $whatsappnum . '?text=' . urlencode($msg) ); // Redirect to WhatsApp sending service
exit;
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I believe this should do the trick:
jQuery to insert on your page
jQuery('.dvs-whatsapp-btn').click(function(){
jQuery('<span class="link-clicked">Link clicked!</span>').insertAfter('.dvs-whatsapp-btn');
jQuery('.dvs-whatsapp-btn').hide();
});

Save custom values from Woocommerce checkout as custom order meta data

In Woocommerce, I have a checkout page (review order) with custom data.
I need to find a hook to register some custom data ($totaleiva_1 and $totalefinitocarrello) in the order and then I have to send them in email new order.
I'm not able to make it for instance. Any advice or help please?
Edit - That is my code:
$totaleiva_1 = 0;
$items = $woocommerce->cart->get_cart();
foreach($items as $item ) {
$totaleiva_1 += $totalForSebeneArray[$item ['data']->get_id()];
}
$totaleiva_1 = number_format($totaleiva_1, 2, '.', '');
$totalefinitocarrello = $totaleiva_1 + $total; echo "€";
echo $totalefinitocarrello;
You will need to add and display 2 hidden fields with the 2 desired values (to be able to get and save them as custom order meta data on submission).
Also your code is a bit outdated and can be simplified.
Here is your revisited code:
$totaleiva_1 = 0;
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ) {
$totale_iva_1 += $totalForSebeneArray[$cart_item['data']->get_id()];
}
$totale_finito_carrello = wc_price( $totale_iva_1 + $total );
echo $totale_finito_carrello;
// Display 2 hidden input fields
echo '<input type="hidden" id="totaleiva1" name="totaleiva1" value="'.$totale_iva_1.'">
<input type="hidden" id="tfcarrello" name="tfcarrello" value="'.$totale_finito_carrello.'">';
Then you will save that data as custom order meta data using:
add_action('woocommerce_checkout_create_order', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( $order, $data ) {
if( isset($_POST['totaleiva1']) && ! empty($_POST['totaleiva1']) ) {
$order->update_meta_data( '_totale_iva_1', esc_attr( $_POST['totaleiva1'] ) );
}
if( isset($_POST['tfcarrello']) && ! empty($_POST['tfcarrello']) ) {
$order->update_meta_data( '_totale_fin_carrello', esc_attr( $_POST['tfcarrello'] ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Now to get that data and use it anywhere:
1) from the order ID variable, use:
$totaleiva_1 = get_post_meta( $order_id, '_totale_iva_1', true );
$totalefinitocarrello = get_post_meta( $order_id, '_totale_fin_carrello', true );
2 from the WC_Order Object:
$totaleiva_1 = $order->get_meta('_totale_iva_1');
$totalefinitocarrello = $order->get_meta('_totale_fin_carrello');
To display those in Woocommerce new order notification, ou will use:
add_action( 'woocommerce_email_order_details', 'email_order_details_action_callback', 5, 4 );
function email_order_details_action_callback( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id === 'new_order' ) {
if ( $tiva1 = $order->get_meta('_totale_iva_1') ) {
echo '<p>'. __("Totale IVA") . ': ' . $tiva1 . '</p>';
}
if ( $tfcarr = $order->get_meta('_totale_fin_carrello') ) {
echo '<p>'. __("Totale finito carrello") . ': ' . $tfcarr . '</p>';
}
}
}
Display the fields in Admin order pages:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'admin_order_after_billing_address_callback', 10, 1 );
function admin_order_after_billing_address_callback( $order ){
if ( $tiva1 = $order->get_meta('_totale_iva_1') ) {
echo '<p>'. __("Totale IVA") . ': ' . $tiva1 . '</p>';
}
if ( $tfcarr = $order->get_meta('_totale_fin_carrello') ) {
echo '<p>'. __("Totale finito carrello") . ': ' . $tfcarr . '</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Now I think that all your IVA custom calculations can be handled by Woocommerce using available settings possibilities, as you are making things Much more complicated everywhere.

Add customer email and phone in "Order" column to admin orders list on Woocommerce

I am trying to find a way on how to add the customer phone and email below the name on the WooCommerce order view. See picture for reference where I need to add this information.
Any ideas, tips or pointers on how to make this happen?
The following code will add the billing phone and email under the order number in backend orders list (for Woocommerce 3.3+ only):
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 50, 2 );
function custom_orders_list_column_content( $column, $post_id ) {
if ( $column == 'order_number' )
{
global $the_order;
if( $phone = $the_order->get_billing_phone() ){
$phone_wp_dashicon = '<span class="dashicons dashicons-phone"></span> ';
echo '<br>' . $phone_wp_dashicon . $phone.'</strong>';
}
if( $email = $the_order->get_billing_email() ){
echo '<br><strong>' . $email . '</strong>';
}
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Thank you, worked perfectly.
I had modifed based on my need, adding the code here for others.
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 50, 2 );
function custom_orders_list_column_content( $column, $post_id ) {
if ( $column == 'order_number' )
{
global $the_order;
if( $phone = $the_order->get_billing_phone() ){
$phone_wp_dashicon = '<span class="dashicons dashicons-phone"></span> ';
echo '<br>Mobile: '.'' . $phone.'</strong>';
}
if( $email = $the_order->get_billing_email() ){
echo '<br>Email: '.'' . $email . '';
}
}
}

Customize order item meta only for WooCommerce admin email notifications

I need to add custom taxonomy to admin new order emails but not to customer emails. My current code displays my custom taxonomy for each item in the order but it is showing up in both admin and customer emails, which I don't want.
Looking thru email-order-items.php I don't see a way to utilize $sent_to_admin in the hook that I am using. Am I missing something?
How do I add my custom taxonomy only to admin emails using just hooks and filters?
add_action( 'woocommerce_order_item_meta_end', 'custom_woocommerce_order_item_meta_end', 10, 3 );
function custom_woocommerce_order_item_meta_end( $item_id, $item, $order ) {
$product = $item->get_product();
$locations = get_the_terms( $product->get_id(), 'my_custom_taxonomy' );
echo '<br/>';
echo '<div style="margin-top: 20px;">';
foreach( $locations as $location ) {
echo 'Location: <b>' . $location->name . '</b>';
echo '<br/>';
}
echo '</div>
}
This can be done using $GLOBAL variable. I have revisited a bit your code too. Try this:
// Setting the "sent_to_admin" as a global variable
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['email_data'] = array(
'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
'email_id' => $email->id, // The email ID (to target specific email notification)
);
}
// Conditionally customizing footer email text
add_action( 'woocommerce_order_item_meta_end', 'custom_email_order_item_meta_end', 10, 3 );
function custom_email_order_item_meta_end( $item_id, $item, $order ){
// Getting the custom 'email_data' global variable
$refNameGlobalsVar = $GLOBALS;
$email_data = $refNameGlobalsVar['email_data'];
// Only for admin email notifications
if( ! ( is_array( $email_data ) && $email_data['sent_to_admin'] ) ) return;
## -------------------------- Your Code below -------------------------- ##
$taxonomy = 'my_custom_taxonomy'; // <= Your custom taxonomy
echo '<br/><div style="margin-top: 20px;">';
foreach( get_the_terms( $item->get_product_id(), $taxonomy ) as $term )
echo 'Location: <b>' . $term->name . '</b><br/>';
echo '</div>';
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.

Categories