• Resolved morris4ever69

    (@morris4ever69)


    I’m building a custom shipping method plugin for WooCommerce to let customers choosing a shipping address from an external API when a particular shipping method is selected. The field is populated by jQuery when customer clicks the button “Choose this address” in a popup.

    The custom field is positioned in the Order Review table, under the row where to select the shipping method. Here comes the problem: editing some other checkout fields reloads the content of this table, loosing the content of the choosen custom shipping address field.

    I was able to pass the variable to WC session in a validate function and maintaining the field content but this is not the best solution: in this way I can save in session the variable only when customer clicks the “Place Order” button. So I was able to set a cookie with jquery and maintain the field content without problems but… I was asking to myself if there is a solution to pass the variable to session when the order review is reloading.

    I found this hook: woocommerce_checkout_update_order_review but I’m not sure :

    1. if it’s the right hook to use for my scope;
    2. how to use it

    Is someone so kind to give me an example to solve the situation? Thanks for any help.

    https://www.ads-software.com/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    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.

    Thread Starter morris4ever69

    (@morris4ever69)

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

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Cookies would work; its not sensitive. But WC Session could also be used. What was the problem you ran into with that?

    Thread Starter morris4ever69

    (@morris4ever69)

    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.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    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.

    Thread Starter morris4ever69

    (@morris4ever69)

    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…

    Thread Starter morris4ever69

    (@morris4ever69)

    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…

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Yup that’s right. No need for admin check.

    Thread Starter morris4ever69

    (@morris4ever69)

    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?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Set to empty string to unset.

    Thread Starter morris4ever69

    (@morris4ever69)

    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 = “[email protected]”;
    break;
    case “takapuna”:
    $recipient = “[email protected]”;
    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

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom checkout field in order review table’ is closed to new replies.