is_kco_confirmation() ) { return; } echo ''; } /** * Populates WooCommerce checkout form in KCO confirmation page. */ public function maybe_populate_wc_checkout( $checkout ) { if ( ! $this->is_kco_confirmation() ) { return; } echo '
'; $klarna_order_id = WC()->session->get( 'kco_wc_order_id' ); $response = KCO_WC()->api->request_post_get_order( $klarna_order_id ); $klarna_order = apply_filters( 'kco_wc_klarna_order_pre_submit', json_decode( $response['body'] ) ); $this->save_customer_data( $klarna_order ); } /** * Submits WooCommerce checkout form in KCO confirmation page. */ public function maybe_submit_wc_checkout() { if ( ! $this->is_kco_confirmation() ) { return; } ?> customer. * * @param $klarna_order */ private function save_customer_data( $klarna_order ) { // First name. WC()->customer->set_billing_first_name( sanitize_text_field( $klarna_order->billing_address->given_name ) ); WC()->customer->set_shipping_first_name( sanitize_text_field( $klarna_order->shipping_address->given_name ) ); // Last name. WC()->customer->set_billing_last_name( sanitize_text_field( $klarna_order->billing_address->family_name ) ); WC()->customer->set_shipping_last_name( sanitize_text_field( $klarna_order->shipping_address->family_name ) ); // Country. WC()->customer->set_billing_country( strtoupper( sanitize_text_field( $klarna_order->billing_address->country ) ) ); WC()->customer->set_shipping_country( strtoupper( sanitize_text_field( $klarna_order->shipping_address->country ) ) ); // Street address 1. WC()->customer->set_billing_address_1( sanitize_text_field( $klarna_order->billing_address->street_address ) ); WC()->customer->set_shipping_address_1( sanitize_text_field( $klarna_order->shipping_address->street_address ) ); // Street address 2. if ( isset( $klarna_order->billing_address->street_address2 ) ) { WC()->customer->set_billing_address_2( sanitize_text_field( $klarna_order->billing_address->street_address2 ) ); WC()->customer->set_shipping_address_2( sanitize_text_field( $klarna_order->shipping_address->street_address2 ) ); } // City. WC()->customer->set_billing_city( sanitize_text_field( $klarna_order->billing_address->city ) ); WC()->customer->set_shipping_city( sanitize_text_field( $klarna_order->shipping_address->city ) ); // County/State. WC()->customer->set_billing_state( sanitize_text_field( $klarna_order->billing_address->region ) ); WC()->customer->set_shipping_state( sanitize_text_field( $klarna_order->shipping_address->region ) ); // Postcode. WC()->customer->set_billing_postcode( sanitize_text_field( $klarna_order->billing_address->postal_code ) ); WC()->customer->set_shipping_postcode( sanitize_text_field( $klarna_order->shipping_address->postal_code ) ); // Phone. WC()->customer->set_billing_phone( sanitize_text_field( $klarna_order->billing_address->phone ) ); // Email. WC()->customer->set_billing_email( sanitize_text_field( $klarna_order->billing_address->email ) ); WC()->customer->save(); } /** * When checking out using KCO, we need to make sure none of the WooCommerce are required, in case Klarna * does not return info for some of them. * * @param array $fields WooCommerce checkout fields. * * @return mixed */ public function unrequire_fields( $fields ) { if ( 'kco' === WC()->session->get( 'chosen_payment_method' ) ) { foreach ( $fields as $fieldset_key => $fieldset ) { foreach ( $fieldset as $key => $field ) { $fields[ $fieldset_key ][ $key ]['required'] = ''; $fields[ $fieldset_key ][ $key ]['wooccm_required'] = ''; } } } return $fields; } /** * Makes sure there's no empty data sent for validation. * * @param array $data Posted data. * * @return mixed */ public function unrequire_posted_data( $data ) { if ( 'kco' === WC()->session->get( 'chosen_payment_method' ) ) { foreach ( $data as $key => $value ) { if ( '' === $value ) { unset( $data[ $key ] ); } } } return $data; } /** * Adds hidden field to WooCommerce checkout form, holding Klarna Checkout order ID. */ public function add_kco_order_id_field() { if ( 'kco' === WC()->session->get( 'chosen_payment_method' ) && isset( $_GET['confirm'] ) && 'yes' === $_GET['confirm'] ) { if ( isset( $_GET['kco_wc_order_id'] ) ) { // Input var okay. $klarna_order_id = esc_attr( sanitize_text_field( $_GET['kco_wc_order_id'] ) ); echo ''; } } } /** * Saves KCO order ID to WooCommerce order as meta field. * * @param WC_Order $order WooCommerce order. * @param array $data Posted data. */ public function save_kco_order_id_field( $order, $data ) { if ( isset( $_POST['kco_order_id'] ) ) { $kco_order_id = sanitize_text_field( $_POST['kco_order_id'] ); update_post_meta( $order->get_id(), '_wc_klarna_order_id', sanitize_key( $kco_order_id ) ); update_post_meta( $order->get_id(), '_transaction_id', sanitize_key( $kco_order_id ) ); } } } Klarna_Checkout_For_WooCommerce_Confirmation::get_instance();