Hi,
I understand the issue. The customer object in WooCommerce are nowadays being populated by the information provided in Svea Checkout which includes the zip code(s) which is to ensure that the data in WooCommerce and Svea are aligned.
Would it be possible for you to create a step before the checkout (it’s own page) with the shipping/zip selector and then use the option “Zip code read-only when logged in” turned on in Svea Checkout? This will in effect lock the currently chosen zip-code when entering the checkout.
Best regards
This may work if there is a convenient way to sett the zip code in svea checkout prior to entering the checkout page.
“Zip code read-only when logged in” locks the zip code to what is previously saved in svea, not what is saved in woocommerce. So the overwrite still happens as expected.
Is there any way to set the saved zip code in svea checkout prior to entering the checkout page?
Hi,
Yes, that option simply pulls the current value from the WC()->customer object which in normal cases only is filled with information if the user is logged in (or already’ve made a purchase in the current session).
By setting the value via WC()->customer->set_billing_postcode( 12345 ) the value should be populated when entering the checkout.
Best regards
We have now had time to test this further and we think we understand the problem in more detail.
Svea checkout do not populate the postcode used for shipping with the shipping postcode from the shipping address but instead populates it with the billing postcode.
This means that if you set the shipping postcode (with in our case the Woocommerce cart shipping calculator) then Woocommerce will unsurprisingly not sett the billing postcode to the same. Svea checkout will later overwrite the postcode from the shipping address with the one from the billing address possibly causing changes to the available shipping options.
Forcefully setting the billing postcode to the same as the shipping postcode when the shipping postcode is sett avoided the problem. The shipping and billing postcode being both set to the same value prevents Svea checkout from changing the shipping options via changing the postcode on the checkout page.
We have however found a second issue. Even if svea checkout and the checkout page appear to recognize changes to the shipping postcode. The checkout iframe itself only appear to recognize the the postcode from woocommerce the first time (with or without write protection).
If the user backs out of the checkout process returns to the cart and selects a different postcode, the postcode in the iframe will not change even if the postcode is set to a different value with
WC()->customer->set_billing_postcode( ... ).
Is there any way to solve this?
Hi,
Regarding the issue that you mention where changing a zip code in WooCommerce after an initial payment session is created, is unfortunately not something that can be easily updated after the fact.
What you could try is to hook into the woocommerce_sco_needs_new_checkout filter directly and trigger the creation of a new session when the zip code is updated in WooCommerce.
Best regards
Thank you, this solved the problem.
Our solution now looks something like this:
add_action( 'woocommerce_calculated_shipping', array( $this, 'save_shipping_calculator_fields' ), 10 );
add_filter( 'woocommerce_sco_needs_new_checkout', array( $this, 'maybe_reset_svea_checkout' ), 10, 2 );
add_filter( 'woocommerce_sco_create_order' , array( $this, 'trying_to_reset_svea_checkout' ), 10, 1 );
public function save_shipping_calculator_fields() {
//Save shipping postcode to billing to prevent it being overwritten by svea checkout
$postcode = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : '';
if ( $postcode ) {
WC()->customer->set_billing_postcode( $postcode );
WC()->session->set( 'reset_svea_checkout', true );
}
}
function maybe_reset_svea_checkout( $need_new, $checkout_data ) {
if ( WC()->session->get( 'reset_svea_checkout' ) ) {
return true;
}
return $need_new;
}
function trying_to_reset_svea_checkout( $checkout_data ) {
if ( WC()->session->get( 'reset_svea_checkout' ) ) {
WC()->session->set( 'reset_svea_checkout', false );
}
return $checkout_data;
}
Unfortunately ‘woocommerce_sco_create_order’ triggers before the chekcout is created and there do not appear to be a obvious post creation alternative so there is still a small chance for errors.
Still, this is good enough for us to consider this solved.
Thanks for all of your support!
-
This reply was modified 2 weeks, 4 days ago by
dimattias.