php - Session data lost on Woocommerce ajax call -
i'm working on woocommerce plugin add vars in session on "add cart" action, , use vars later when order completed , order confirmation email.
basically workflow following:
on
woocommerce_add_to_cart
action, set session vars.add_filter('woocommerce_add_to_cart', array(&$this->wc, 'add_to_cart'), 10, 1);
public function add_to_cart($cart_item_key) { if(!isset($_session['tickets'])) { $_session['tickets'] = array(); } $_session['tickets'][$cart_item_key] = array(); foreach($_post $key => $value) { if(preg_match('#^ticket_#', $key)) { $_session['tickets'][$cart_item_key][$key] = $value; } } }
on
woocommerce_email_after_order_table
, use vars add informations confirmation email.add_action('woocommerce_email_after_order_table', array(&$this->wc, 'email_after_order_table'), 10, 1);
public function email_after_order_table($order) { if(isset($_session['tickets']) && !empty($_session['tickets'])) { $output = ''; foreach($_session['tickets'] $cart_item) { if(is_array($cart_item) && !empty($cart_item)) { foreach($cart_item $ticket_id) { $ticket = get_post($ticket_id); $room = get_the_term_list($ticket_id, 'product_tag'); $output .= $ticket->post_title . ' (' . $room . ')<br />'; } } } if(!empty($output)) { echo '<h4>' . __('tickets', 'my-context') . '</h4><p>' . $output . '</p>'; } } }
please note action executed woocommerce on
?wc-ajax=checkout
ajax call.on
woocommerce_order_status_completed
orwoocommerce_order_status_on-hold
, update cpt using vars, delete session vars.
the problem have when hooking on woocommerce_email_after_order_table
$_session
empty. if on $_cookie['phpsessid']
, set , same value in context session vars set. , if try query cpt, aren't updated yet, woocommerce_order_status_completed
hook (which working , have no issue accessing session vars) isn't yet executed.
i tried using wc_session
instead of $_session
, didn't changed (step 1 , 3 working not step 2).
does know why woocommerce_email_after_order_table
action isn't in same context woocommerce_order_status_completed
? there way pass custom data in hook?
allright found out going on tracking hooks execution time, , figured woocommerce_order_status_complete
executed before email hooks. seems woocommerce doesn't provide hook in fact executed after order actions have been processed.
woocommerce_order_status_complete 0.15321100 1453995747 woocommerce_email_after_order_table 0.40655700 1453995747
so considering deleting session vars on woocommerce_order_status_complete
, of course not accessible email templates. changing email_after_order_table
use updated cpt instead of session vars solved problem:
public function email_after_order_table($order) { $tickets = get_posts(array( 'post_type' => 'tickets', 'numberposts' => -1, 'meta_query' => array(array( 'key' => 'tickets_reservation', 'value' => $order->id )) )); if($tickets) { $output = ''; foreach($tickets $ticket) { $room = strip_tags(get_the_term_list($ticket->id, 'product_tag')); $output .= $ticket->post_title . ' (' . $room . ')<br />'; } if(!empty($output)) { echo '<h4>' . __('tickets', 'my-context') . '</h4><p>' . $output . '</p>'; } } }
the reason why failed query cpt before pretty trivial: used $order->id
instead of $order->id
...
i have if woocommerce provided more info hooks execution order.
Comments
Post a Comment