{"id":18750687,"date":"2025-12-10T14:57:40","date_gmt":"2025-12-10T14:57:40","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/migrate-guest-data-to-user-data\/"},"modified":"2025-12-10T14:57:40","modified_gmt":"2025-12-10T14:57:40","slug":"migrate-guest-data-to-user-data","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/migrate-guest-data-to-user-data\/","title":{"rendered":"Migrate guest data to user data"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;m trying to restore the previous session shipping information and shipping method of a guest user who logs in as a returning customer on the checkout page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve managed to restore the shipping information using <em>&#8216;woocommerce_restored_session_data&#8217;<\/em> and * &#8216;woocommerce_guest_session_to_user_id&#8217;* But in trying to restore the shipping method chosen before they logged back in seems to be overriden somewhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m using the below snippet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n                |----------------------------------------------------------------------\n                | Restore chosen shipping methods\n                |----------------------------------------------------------------------\n                *\/\n                if ( ! empty( $cached&#091;'chosen_shipping_methods'] ) ) {\n                    $session_data&#091;'chosen_shipping_methods'] = $cached&#091;'chosen_shipping_methods'];\n                    $shipping_methods = maybe_unserialize( $cached&#091;'chosen_shipping_methods'] );\n\n                    if ( ! empty( $shipping_methods ) ) {\n                        \/\/ Force WooCommerce to acknowledge the restored method\n                        WC()-&gt;session-&gt;set( 'chosen_shipping_methods', $shipping_methods );\n\n                        \/\/ Needed to prevent WC from overriding your rate during refresh\n                        $_POST&#091;'shipping_method'] = $shipping_methods;\n                    }\n                }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">at woocommerce_checkout_init and wp_login and woocommerce_init but seems to get overridden after those hooks. The session data has the information and the values exists.<\/p>\n","protected":false},"template":"","class_list":["post-18750687","topic","type-topic","status-publish","hentry","topic-tag-customer","topic-tag-guest","topic-tag-login","topic-tag-session","topic-tag-shipping"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18750687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18750687\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=18750687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}