How to get a cookie value in Woocommerce email notifications?

谁说胖子不能爱 提交于 2019-12-24 13:33:46

问题


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:

  1. 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.

  2. 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.


回答1:


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:

  1. From the Order ID: $cookie = get_post_meta( $order_id, '_cookie_ticketidno', true );
  2. 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.



来源:https://stackoverflow.com/questions/55335639/how-to-get-a-cookie-value-in-woocommerce-email-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!