There are hooks after shipping methods e.g. https://github.com/woothemes/woocommerce/blob/master/templates/cart/cart-shipping.php#L36 which could be used.
That value may be lost after re-calculation/changing address fields when totals update though so you could either try posting and storing in session, or allow it to clear seeing as its last on checkout anyway.
Thank you for your answer.
I added the field using the hook “woocommerce_review_order_after_shipping”, in that way I was able to add a new row in the table.
My jQuery callback function retrieves the address on “onmessage” and set the value of my custom checkout field. Until here, no problems!
To store the value (for not loosing it on totals update or if the customer lets the checkout page and comes back in a second time), I saved that value in a $.cookie (setting the parameters “expires: 1” and “secure : true” because I have https on the checkout page), and I retrieve it via PHP, setting the field value in function woocommerce_form_field() with an if statement.
I’m not sure if this is the best/safest way to manage the situation: the cookie expires in 1 day and is not a session cookie. I thought to pass the value to WC() session but I was not able to find a way. I would like not introducing a piece of unsafe code…
I’m hoping in some help.. π
Cookies would work; its not sensitive. But WC Session could also be used. What was the problem you ran into with that?
Basically, the problem was my poor knowledge of coding π
BTW, I was able to use WC Session to store the field value only when customer clicks the “Place Order” button. If customer entered wrong information, WC scrolls the page to the top to show the error notices and the field value is there. The problem comes when customer doesn’t click the “Place Order” button: basically I don’t know if I have to use some hook (and if yes, which one) to set the field value in WC Session before/during the totals update. Or may should I go with ajax in my jQuery callback function?
I’m sorry for my long explanation: I’m trying to improve my (poor) coding skills and, customizing WooCommerce, I learned a lot but, of course, I find some limits (my limits, not in the plugin) and sometime I should need some direction (specially after hours/days spent on coding). Anyway, thank you for your time spent on helping me.
Even before place order, when the checkout updates this ajax callback is used https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-ajax.php#L278
There are actions inside there you can use.
Yes, I found that hook yesterday but I was not able to set WC session using it. My function:
add_action( 'woocommerce_checkout_update_order_review', 'set_field_in_wc_session' );
function set_field_in_wc_session( $posted ) {
if ( isset($_POST['my_field']) ) {
WC()->session->set( 'chosen_my_field', wc_clean($_POST['my_field']) );
}
return $posted;
}
Obviously there’s something wrong…
Oh, I’ve got some results…
add_action( 'woocommerce_checkout_update_order_review', 'set_field_in_wc_session' );
function set_field_in_wc_session( $post_data ) {
if ( !is_admin() ){
parse_str($post_data);
if ( isset($my_field) && !empty($my_field) ) {
WC()->session->set( 'chosen_my_field', wc_clean($my_field) );
}
}
}
I’m not sure if it’s ok at 100%, but the behavior seems to be what I wanted…
Yup that’s right. No need for admin check.
Thank you for the confirm! Last point..
To set:
WC()->session->set( 'my_field', wc_clean($my_field) );
To retrieve:
WC()->session->get( 'my_field' );
To unset?
If customer delete manually a field value and the field (now) it’s empty, I think I should unset in some way, right?
Set to empty string to unset.
Got it!
Thank you Mike for your help. Now everything about WC Session is clear!
Hi Morris,
I am struggling with the same issue.
Are you able to share your code with cookie which saves the field values so that it doesn’t get deleted upon the refresh/reload??
Also, i’m not a hardcore coder but i am also having issues with the code i wrote which re-routes the new order email based on the checkout add-on value(stores) selection on the checkout. The problem is that whenever the user is redirected to a payment gateway hosted payment page and after the completion of the payment it should use the code i wrote to reroute the email but due to my lack of knowledge about sessions and cookies i am not able to do it and my code fails. The code works fine with cheque payments or anything done on the same page.
Below is my code for your reference:
// Change new order email recipient for different stores
function wc_change_admin_new_order_email_recipient( $recipient, $order ) {
global $woocommerce;
$dropdownValue = $_POST[‘wc_checkout_add_ons_2’];
switch($dropdownValue)
{
case “newmarket”:
$recipient = “abc@xyz.com”;
break;
case “takapuna”:
$recipient = “def@xyz.com”;
break;
}
return $recipient;
}
add_filter(‘woocommerce_email_recipient_new_order’, ‘wc_change_admin_new_order_email_recipient’, 1, 2);
//End
Your help is much appreciated.
Kind Regards,
Vishal