Skip to content

Commit

Permalink
Merge pull request #104 from krokedil/develop
Browse files Browse the repository at this point in the history
Version 1.7.0
  • Loading branch information
NiklasHogefjord authored Oct 9, 2018
2 parents 3ba5dcf + a4a4d51 commit 1e2d824
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 104 deletions.
3 changes: 1 addition & 2 deletions assets/css/klarna-checkout-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,4 @@ a.kb-button {
outline:none;
text-decoration:none;
color:black;
}

}
16 changes: 15 additions & 1 deletion assets/js/klarna-checkout-for-woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,19 @@ jQuery(function($) {
});
},

setFieldValues: function( data ) {
// Billing fields
$('#billing_email').val(data.email);
$('#billing_state').val(data.states.billing_state);

// Shipping fields
$('#shipping_state').val(data.states.shipping_state);

// Trigger changes
$('#billing_email').change();
$('#billing_email').blur();
},

init: function () {
$(document).ready(kco_wc.documentReady);

Expand Down Expand Up @@ -365,6 +378,7 @@ jQuery(function($) {
success: function (response) {
kco_wc.log(response);
$('.woocommerce-checkout-review-order-table').replaceWith(response.data.html);
kco_wc.setFieldValues( response.data );
},
error: function (response) {
kco_wc.log(response);
Expand Down Expand Up @@ -400,5 +414,5 @@ jQuery(function($) {
if (event.keyCode == 13) {
event.preventDefault();
}
});
});
});
2 changes: 1 addition & 1 deletion assets/js/klarna-checkout-for-woocommerce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion includes/class-klarna-checkout-for-woocommerce-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public function check_settings() {
if ( ! empty( $_POST ) ) {
add_action( 'woocommerce_settings_saved', array( $this, 'check_terms' ) );
add_action( 'woocommerce_settings_saved', array( $this, 'check_account' ) );
add_action( 'admin_notices', array( $this, 'check_autoptimize' ) );
} else {
add_action( 'admin_notices', array( $this, 'check_terms' ) );
add_action( 'admin_notices', array( $this, 'check_https' ) );
add_action( 'admin_notices', array( $this, 'check_terms' ) );
add_action( 'admin_notices', array( $this, 'check_account' ) );
add_action( 'admin_notices', array( $this, 'check_autoptimize' ) );
}
}

Expand Down Expand Up @@ -117,6 +119,21 @@ public function check_account() {
echo '</div>';
}
}

/**
* Check Autoptimize plugin checkout settings if they exist.
*/
public function check_autoptimize() {
if ( 'yes' !== $this->enabled ) {
return;
}

if ( 'on' === get_option( 'autoptimize_optimize_checkout' ) ) {
echo '<div class="notice notice-error">';
echo '<p>' . sprintf( __( 'It looks like you are using the Autoptimize plugin and have enabled their <i>Optimize shop cart/checkout</i> setting. This might cause conflicts with the Klarna Checkout plugin. You can deactivate this feature in the <a href="%s">Autoptimize settings page</a> (<i>→ Show advanced settings → Misc section</i>).', 'klarna-checkout-for-woocommerce' ), admin_url( 'options-general.php?page=autoptimize' ) ) . '</p>';
echo '</div>';
}
}
}

Klarna_Checkout_For_WooCommerce_Admin_Notices::get_instance();
24 changes: 14 additions & 10 deletions includes/class-klarna-checkout-for-woocommerce-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function kco_wc_change_payment_method() {
* Updates Klarna order.
*/
public static function kco_wc_update_klarna_order() {

wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );

if ( 'kco' === WC()->session->get( 'chosen_payment_method' ) ) {
Expand All @@ -176,8 +176,8 @@ public static function kco_wc_update_klarna_order() {
WC()->cart->calculate_fees();
WC()->cart->calculate_totals();

if( ! WC()->cart->needs_payment() ) {
$return = array();
if ( ! WC()->cart->needs_payment() ) {
$return = array();
$return['redirect_url'] = wc_get_checkout_url();
wp_send_json_error( $return );
wp_die();
Expand Down Expand Up @@ -210,37 +210,31 @@ public static function kco_wc_iframe_shipping_address_change() {
$customer_data = array();

if ( isset( $address['email'] ) ) {
$customer_data['email'] = $address['email'];
$customer_data['billing_email'] = $address['email'];
}

if ( isset( $address['postal_code'] ) ) {
$customer_data['postcode'] = $address['postal_code'];
$customer_data['billing_postcode'] = $address['postal_code'];
$customer_data['shipping_postcode'] = $address['postal_code'];
}

if ( isset( $address['given_name'] ) ) {
$customer_data['first_name'] = $address['given_name'];
$customer_data['billing_first_name'] = $address['given_name'];
$customer_data['shipping_first_name'] = $address['given_name'];
}

if ( isset( $address['family_name'] ) ) {
$customer_data['last_name'] = $address['family_name'];
$customer_data['billing_last_name'] = $address['family_name'];
$customer_data['shipping_last_name'] = $address['family_name'];
}

if ( isset( $address['region'] ) ) {
$customer_data['state'] = $address['region'];
$customer_data['billing_state'] = $address['region'];
$customer_data['shipping_state'] = $address['region'];
}

if ( isset( $address['country'] ) && kco_wc_country_code_converter( $address['country'] ) ) {
$country = kco_wc_country_code_converter( $address['country'] );
$customer_data['country'] = $country;
$customer_data['billing_country'] = $country;
$customer_data['shipping_country'] = $country;
}
Expand All @@ -250,13 +244,23 @@ public static function kco_wc_iframe_shipping_address_change() {
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();

// Send customer data to frontend
$email = Klarna_Checkout_For_Woocommerce_Checkout_Form_Fields::maybe_set_customer_email();
$states = Klarna_Checkout_For_Woocommerce_Checkout_Form_Fields::maybe_set_customer_state();

KCO_WC()->api->request_pre_update_order();

ob_start();
woocommerce_order_review();
$html = ob_get_clean();

wp_send_json_success( array( 'html' => $html ) );
wp_send_json_success(
array(
'html' => $html,
'email' => $email,
'states' => $states,
)
);
wp_die();
}

Expand Down
Loading

0 comments on commit 1e2d824

Please sign in to comment.