From 8243974004d135c0787665c8f61a2211295ef5b9 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 26 Nov 2012 20:56:48 -0500 Subject: [PATCH] Add address to customer session. Closes #1836 --- assets/js/frontend/checkout.js | 10 ++++ assets/js/frontend/checkout.min.js | 2 +- classes/class-wc-customer.php | 86 ++++++++++++++++++++++++++++++ woocommerce-ajax.php | 12 +++++ 4 files changed, 109 insertions(+), 1 deletion(-) diff --git a/assets/js/frontend/checkout.js b/assets/js/frontend/checkout.js index 16285ed6be6..ccb210a0fe5 100644 --- a/assets/js/frontend/checkout.js +++ b/assets/js/frontend/checkout.js @@ -18,17 +18,23 @@ jQuery(document).ready(function($) { var state = $('#billing_state').val(); var postcode = $('input#billing_postcode').val(); var city = $('input#billing_city').val(); + var address = $('input#billing_address_1').val(); + var address_2 = $('input#billing_address_2').val(); if ( $('#shiptobilling input').is(':checked') || $('#shiptobilling input').size() == 0 ) { var s_country = country; var s_state = state; var s_postcode = postcode; var s_city = city; + var s_address = address; + var s_address_2 = address_2; } else { var s_country = $('#shipping_country').val(); var s_state = $('#shipping_state').val(); var s_postcode = $('input#shipping_postcode').val(); var s_city = $('input#shipping_city').val(); + var s_address = $('input#shipping_address_1').val(); + var s_address_2 = $('input#shipping_address_2').val(); } $('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.ajax_loader_url + ') no-repeat center', opacity: 0.6}}); @@ -42,10 +48,14 @@ jQuery(document).ready(function($) { state: state, postcode: postcode, city: city, + address: address, + address_2: address_2, s_country: s_country, s_state: s_state, s_postcode: s_postcode, s_city: s_city, + s_address: s_address, + s_address_2: s_address_2, post_data: $('form.checkout').serialize() }; diff --git a/assets/js/frontend/checkout.min.js b/assets/js/frontend/checkout.min.js index 25e3b4cb712..61daa1dbc18 100644 --- a/assets/js/frontend/checkout.min.js +++ b/assets/js/frontend/checkout.min.js @@ -1 +1 @@ -jQuery(document).ready(function(e){function i(){r&&r.abort();if(e("select#shipping_method").size()>0)var t=e("select#shipping_method").val();else var t=e("input[name=shipping_method]:checked").val();var n=e("#order_review input[name=payment_method]:checked").val(),i=e("#billing_country").val(),s=e("#billing_state").val(),o=e("input#billing_postcode").val(),u=e("input#billing_city").val();if(e("#shiptobilling input").is(":checked")||e("#shiptobilling input").size()==0)var a=i,f=s,l=o,c=u;else var a=e("#shipping_country").val(),f=e("#shipping_state").val(),l=e("input#shipping_postcode").val(),c=e("input#shipping_city").val();e("#order_methods, #order_review").block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});var h={action:"woocommerce_update_order_review",security:woocommerce_params.update_order_review_nonce,shipping_method:t,payment_method:n,country:i,state:s,postcode:o,city:u,s_country:a,s_state:f,s_postcode:l,s_city:c,post_data:e("form.checkout").serialize()};r=e.ajax({type:"POST",url:woocommerce_params.ajax_url,data:h,success:function(t){if(t){e("#order_review").after(t).remove();e("#order_review input[name=payment_method]:checked").click();e("body").trigger("updated_checkout")}}})}function s(){n=!1;e("body").trigger("update_checkout")}var t,n=!1,r;e("body").bind("update_checkout",function(){clearTimeout(t);i()});e("p.password, form.login, .checkout_coupon, div.shipping_address").hide();e("input.show_password").change(function(){e("p.password").slideToggle()});e("a.showlogin").click(function(){e("form.login").slideToggle();return!1});e("a.showcoupon").click(function(){e(".checkout_coupon").slideToggle();return!1});e("#shiptobilling input").change(function(){e("div.shipping_address").hide();e(this).is(":checked")||e("div.shipping_address").slideDown()}).change();if(woocommerce_params.option_guest_checkout=="yes"){e("div.create-account").hide();e("input#createaccount").change(function(){e("div.create-account").hide();e(this).is(":checked")&&e("div.create-account").slideDown()}).change()}e(".payment_methods input.input-radio").live("click",function(){if(e(".payment_methods input.input-radio").length>1){e("div.payment_box").filter(":visible").slideUp(250);e(this).is(":checked")&&e("div.payment_box."+e(this).attr("ID")).slideDown(250)}else e("div.payment_box").show()});e("#order_review input[name=payment_method]:checked").click();e("select#shipping_method, input[name=shipping_method], #shiptobilling input, .update_totals_on_change select").live("change",function(){clearTimeout(t);e("body").trigger("update_checkout")});e(".update_totals_on_change input").live("change",function(){if(n){clearTimeout(t);e("body").trigger("update_checkout")}});e(".update_totals_on_change input.input-text").live("keydown",function(e){var r=e.keyCode||e.which;if(r=="9")return;n=!0;clearTimeout(t);t=setTimeout(s,"1000")});e("form.checkout_coupon").submit(function(){var t=e(this);if(t.is(".processing"))return!1;t.addClass("processing").block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});var n={action:"woocommerce_apply_coupon",security:woocommerce_params.apply_coupon_nonce,coupon_code:t.find("input[name=coupon_code]").val()};e.ajax({type:"POST",url:woocommerce_params.ajax_url,data:n,success:function(n){e(".woocommerce_error, .woocommerce_message").remove();t.removeClass("processing").unblock();if(n){t.before(n);t.slideUp();e("body").trigger("update_checkout")}},dataType:"html"});return!1});e("form.checkout").submit(function(){clearTimeout(t);var n=e(this);if(n.is(".processing"))return!1;if(n.triggerHandler("checkout_place_order")!==!1&&n.triggerHandler("checkout_place_order_"+e("#order_review input[name=payment_method]:checked").val())!==!1){n.addClass("processing");var r=n.data();r["blockUI.isBlocked"]!=1&&n.block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});e.ajax({type:"POST",url:woocommerce_params.checkout_url,data:n.serialize(),success:function(t){try{result=e.parseJSON(t);if(result.result=="success")window.location=decodeURI(result.redirect);else{if(result.result!="failure")throw"Invalid response";e(".woocommerce_error, .woocommerce_message").remove();n.prepend(result.messages);n.removeClass("processing").unblock();result.refresh=="true"&&e("body").trigger("update_checkout");e("html, body").animate({scrollTop:e("form.checkout").offset().top-100},1e3)}}catch(r){e(".woocommerce_error, .woocommerce_message").remove();n.prepend(t);n.removeClass("processing").unblock();e("html, body").animate({scrollTop:e("form.checkout").offset().top-100},1e3)}},dataType:"html"})}return!1});var o=woocommerce_params.locale.replace(/"/g,'"'),u=e.parseJSON(o),a=' *';e("body").bind("country_to_state_changing",function(t,n,r){var i=r;if(u[n])var s=u[n];else var s=u["default"];var o={address_1:"#billing_address_1_field, #shipping_address_1_field",address_2:"#billing_address_2_field, #shipping_address_2_field",state:"#billing_state_field, #shipping_state_field",postcode:"#billing_postcode_field, #shipping_postcode_field",city:"#billing_city_field, #shipping_city_field"};e.each(o,function(e,t){var n=i.find(t);if(s[e]){s[e].label&&n.find("label").html(s[e].label);s[e].placeholder&&n.find("input").attr("placeholder",s[e].placeholder);n.find("label abbr").remove();(typeof s[e]["required"]=="undefined"||s[e]["required"]==1)&&n.find("label").append(a);e!=="state"&&(s[e]["hidden"]==1?n.fadeOut(200).find("input").val(""):n.fadeIn(500))}else if(u["default"][e]){u["default"][e]["required"]==1&&n.find("label abbr").size()==0&&n.find("label").append(a);e!=="state"&&(typeof u["default"][e]["hidden"]=="undefined"||u["default"][e]["hidden"]==0)&&n.fadeIn(500)}});var f=i.find("#billing_postcode_field, #shipping_postcode_field"),l=i.find("#billing_city_field, #shipping_city_field");if(s.postcode_before_city){if(l.is(".form-row-first")){l.fadeOut(200,function(){l.removeClass("form-row-first").addClass("form-row-last").insertAfter(f).fadeIn(500)});f.fadeOut(200,function(){f.removeClass("form-row-last").addClass("form-row-first").fadeIn(500)})}}else if(l.is(".form-row-last")){l.fadeOut(200,function(){l.removeClass("form-row-last").addClass("form-row-first").insertBefore(f).fadeIn(500)});f.fadeOut(200,function(){f.removeClass("form-row-first").addClass("form-row-last").fadeIn(500)})}});e("body").bind("init_checkout",function(){e("#billing_country, #shipping_country, .country_to_state").change();e("body").trigger("update_checkout")});woocommerce_params.is_checkout==1&&e("body").trigger("init_checkout")}); \ No newline at end of file +jQuery(document).ready(function(e){function i(){if(r)r.abort();if(e("select#shipping_method").size()>0)var t=e("select#shipping_method").val();else var t=e("input[name=shipping_method]:checked").val();var n=e("#order_review input[name=payment_method]:checked").val();var i=e("#billing_country").val();var s=e("#billing_state").val();var o=e("input#billing_postcode").val();var u=e("input#billing_city").val();var a=e("input#billing_address_1").val();var f=e("input#billing_address_2").val();if(e("#shiptobilling input").is(":checked")||e("#shiptobilling input").size()==0){var l=i;var c=s;var h=o;var p=u;var d=a;var v=f}else{var l=e("#shipping_country").val();var c=e("#shipping_state").val();var h=e("input#shipping_postcode").val();var p=e("input#shipping_city").val();var d=e("input#shipping_address_1").val();var v=e("input#shipping_address_2").val()}e("#order_methods, #order_review").block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});var m={action:"woocommerce_update_order_review",security:woocommerce_params.update_order_review_nonce,shipping_method:t,payment_method:n,country:i,state:s,postcode:o,city:u,address:a,address_2:f,s_country:l,s_state:c,s_postcode:h,s_city:p,s_address:d,s_address_2:v,post_data:e("form.checkout").serialize()};r=e.ajax({type:"POST",url:woocommerce_params.ajax_url,data:m,success:function(t){if(t){e("#order_review").after(t).remove();e("#order_review input[name=payment_method]:checked").click();e("body").trigger("updated_checkout")}}})}function s(){n=false;e("body").trigger("update_checkout")}var t;var n=false;var r;e("body").bind("update_checkout",function(){clearTimeout(t);i()});e("p.password, form.login, .checkout_coupon, div.shipping_address").hide();e("input.show_password").change(function(){e("p.password").slideToggle()});e("a.showlogin").click(function(){e("form.login").slideToggle();return false});e("a.showcoupon").click(function(){e(".checkout_coupon").slideToggle();return false});e("#shiptobilling input").change(function(){e("div.shipping_address").hide();if(!e(this).is(":checked")){e("div.shipping_address").slideDown()}}).change();if(woocommerce_params.option_guest_checkout=="yes"){e("div.create-account").hide();e("input#createaccount").change(function(){e("div.create-account").hide();if(e(this).is(":checked")){e("div.create-account").slideDown()}}).change()}e(".payment_methods input.input-radio").live("click",function(){if(e(".payment_methods input.input-radio").length>1){e("div.payment_box").filter(":visible").slideUp(250);if(e(this).is(":checked")){e("div.payment_box."+e(this).attr("ID")).slideDown(250)}}else{e("div.payment_box").show()}});e("#order_review input[name=payment_method]:checked").click();e("select#shipping_method, input[name=shipping_method], #shiptobilling input, .update_totals_on_change select").live("change",function(){clearTimeout(t);e("body").trigger("update_checkout")});e(".update_totals_on_change input").live("change",function(){if(n){clearTimeout(t);e("body").trigger("update_checkout")}});e(".update_totals_on_change input.input-text").live("keydown",function(e){var r=e.keyCode||e.which;if(r=="9")return;n=true;clearTimeout(t);t=setTimeout(s,"1000")});e("form.checkout_coupon").submit(function(){var t=e(this);if(t.is(".processing"))return false;t.addClass("processing").block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});var n={action:"woocommerce_apply_coupon",security:woocommerce_params.apply_coupon_nonce,coupon_code:t.find("input[name=coupon_code]").val()};e.ajax({type:"POST",url:woocommerce_params.ajax_url,data:n,success:function(n){e(".woocommerce_error, .woocommerce_message").remove();t.removeClass("processing").unblock();if(n){t.before(n);t.slideUp();e("body").trigger("update_checkout")}},dataType:"html"});return false});e("form.checkout").submit(function(){clearTimeout(t);var n=e(this);if(n.is(".processing"))return false;if(n.triggerHandler("checkout_place_order")!==false&&n.triggerHandler("checkout_place_order_"+e("#order_review input[name=payment_method]:checked").val())!==false){n.addClass("processing");var r=n.data();if(r["blockUI.isBlocked"]!=1)n.block({message:null,overlayCSS:{background:"#fff url("+woocommerce_params.ajax_loader_url+") no-repeat center",opacity:.6}});e.ajax({type:"POST",url:woocommerce_params.checkout_url,data:n.serialize(),success:function(t){try{result=e.parseJSON(t);if(result.result=="success"){window.location=decodeURI(result.redirect)}else if(result.result=="failure"){e(".woocommerce_error, .woocommerce_message").remove();n.prepend(result.messages);n.removeClass("processing").unblock();if(result.refresh=="true")e("body").trigger("update_checkout");e("html, body").animate({scrollTop:e("form.checkout").offset().top-100},1e3)}else{throw"Invalid response"}}catch(r){e(".woocommerce_error, .woocommerce_message").remove();n.prepend(t);n.removeClass("processing").unblock();e("html, body").animate({scrollTop:e("form.checkout").offset().top-100},1e3)}},dataType:"html"})}return false});var o=woocommerce_params.locale.replace(/"/g,'"');var u=e.parseJSON(o);var a=' *';e("body").bind("country_to_state_changing",function(t,n,r){var i=r;if(u[n]){var s=u[n]}else{var s=u["default"]}var o={address_1:"#billing_address_1_field, #shipping_address_1_field",address_2:"#billing_address_2_field, #shipping_address_2_field",state:"#billing_state_field, #shipping_state_field",postcode:"#billing_postcode_field, #shipping_postcode_field",city:"#billing_city_field, #shipping_city_field"};e.each(o,function(e,t){var n=i.find(t);if(s[e]){if(s[e]["label"]){n.find("label").html(s[e]["label"])}if(s[e]["placeholder"]){n.find("input").attr("placeholder",s[e]["placeholder"])}n.find("label abbr").remove();if(typeof s[e]["required"]=="undefined"||s[e]["required"]==true){n.find("label").append(a)}if(e!=="state"){if(s[e]["hidden"]==true){n.fadeOut(200).find("input").val("")}else{n.fadeIn(500)}}}else if(u["default"][e]){if(u["default"][e]["required"]==true){if(n.find("label abbr").size()==0)n.find("label").append(a)}if(e!=="state"&&(typeof u["default"][e]["hidden"]=="undefined"||u["default"][e]["hidden"]==false)){n.fadeIn(500)}}});var f=i.find("#billing_postcode_field, #shipping_postcode_field");var l=i.find("#billing_city_field, #shipping_city_field");if(s["postcode_before_city"]){if(l.is(".form-row-first")){l.fadeOut(200,function(){l.removeClass("form-row-first").addClass("form-row-last").insertAfter(f).fadeIn(500)});f.fadeOut(200,function(){f.removeClass("form-row-last").addClass("form-row-first").fadeIn(500)})}}else{if(l.is(".form-row-last")){l.fadeOut(200,function(){l.removeClass("form-row-last").addClass("form-row-first").insertBefore(f).fadeIn(500)});f.fadeOut(200,function(){f.removeClass("form-row-first").addClass("form-row-last").fadeIn(500)})}}});e("body").bind("init_checkout",function(){e("#billing_country, #shipping_country, .country_to_state").change();e("body").trigger("update_checkout")});if(woocommerce_params.is_checkout==1)e("body").trigger("init_checkout")}) \ No newline at end of file diff --git a/classes/class-wc-customer.php b/classes/class-wc-customer.php index 1780edbd9fe..fc08afa68e4 100644 --- a/classes/class-wc-customer.php +++ b/classes/class-wc-customer.php @@ -39,10 +39,14 @@ class WC_Customer { 'state' => '', 'postcode' => '', 'city' => '', + 'address' => '', + 'address_2' => '', 'shipping_country' => esc_html( $country ), 'shipping_state' => '', 'shipping_postcode' => '', 'shipping_city' => '', + 'shipping_address' => '', + 'shipping_address_2' => '', 'is_vat_exempt' => false, 'calculated_shipping' => false ); @@ -228,6 +232,25 @@ class WC_Customer { if ( isset( $this->_data['city'] ) ) return $this->_data['city']; } + /** + * Gets the address from the current session. + * + * @access public + * @return void + */ + function get_address() { + if ( isset( $this->_data['address'] ) ) return $this->_data['address']; + } + + /** + * Gets the address_2 from the current session. + * + * @access public + * @return void + */ + function get_address_2() { + if ( isset( $this->_data['address_2'] ) ) return $this->_data['address_2']; + } /** * Gets the state from the current session. @@ -274,6 +297,25 @@ class WC_Customer { if ( isset( $this->_data['shipping_city'] ) ) return $this->_data['shipping_city']; } + /** + * Gets the address from the current session. + * + * @access public + * @return void + */ + function get_shipping_address() { + if ( isset( $this->_data['shipping_address'] ) ) return $this->_data['shipping_address']; + } + + /** + * Gets the address_2 from the current session. + * + * @access public + * @return void + */ + function get_shipping_address_2() { + if ( isset( $this->_data['shipping_address_2'] ) ) return $this->_data['shipping_address_2']; + } /** * get_taxable_address function. @@ -362,6 +404,28 @@ class WC_Customer { $this->_data['city'] = $city; } + /** + * Sets session data for the address. + * + * @access public + * @param mixed $address + * @return void + */ + function set_address( $address ) { + $this->_data['address'] = $address; + } + + /** + * Sets session data for the address_2. + * + * @access public + * @param mixed $address_2 + * @return void + */ + function set_address_2( $address_2 ) { + $this->_data['address_2'] = $address_2; + } + /** * Sets session data for the location. * @@ -427,6 +491,28 @@ class WC_Customer { $this->_data['shipping_city'] = $city; } + /** + * Sets session data for the address. + * + * @access public + * @param mixed $address + * @return void + */ + function set_shipping_address( $address ) { + $this->_data['shipping_address'] = $address; + } + + /** + * Sets session data for the address_2. + * + * @access public + * @param mixed $address_2 + * @return void + */ + function set_shipping_address_2( $address_2 ) { + $this->_data['shipping_address_2'] = $address_2; + } + /** * Sets session data for the tax exemption. diff --git a/woocommerce-ajax.php b/woocommerce-ajax.php index d7ab952fe0d..d4306891159 100644 --- a/woocommerce-ajax.php +++ b/woocommerce-ajax.php @@ -171,6 +171,12 @@ function woocommerce_ajax_update_order_review() { if ( isset( $_POST['city'] ) ) $woocommerce->customer->set_city( $_POST['city'] ); + + if ( isset( $_POST['address'] ) ) + $woocommerce->customer->set_address( $_POST['address'] ); + + if ( isset( $_POST['address_2'] ) ) + $woocommerce->customer->set_address_2( $_POST['address_2'] ); if ( isset( $_POST['s_country'] ) ) $woocommerce->customer->set_shipping_country( $_POST['s_country'] ); @@ -183,6 +189,12 @@ function woocommerce_ajax_update_order_review() { if ( isset( $_POST['s_city'] ) ) $woocommerce->customer->set_shipping_city( $_POST['s_city'] ); + + if ( isset( $_POST['s_address'] ) ) + $woocommerce->customer->set_shipping_address( $_POST['s_address'] ); + + if ( isset( $_POST['s_address_2'] ) ) + $woocommerce->customer->set_shipping_address_2( $_POST['s_address_2'] ); $woocommerce->cart->calculate_totals();