Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v:2.0:snippets:dynamically_require_phone [2015/03/30 16:05] – Changing script to be more thorough sparkwebv:2.0:snippets:dynamically_require_phone [2018/07/30 06:02] (current) adam
Line 5: Line 5:
 ==== Add Javascript ==== ==== Add Javascript ====
  
-Put this javascript in the header of your checkout templateYou may want to put it in the "add code to the header and footer" section of the admin configuration settings.+Copy and paste the following code into your store's custom "footer" optionThis can be found on the "configuration" page of your store's FoxyCart administration, look for the option labelled "Add custom header and footer code to your templates". Enable it if it isn't already, and paste the code into the "footer" textarea of that option.
  
-<code javascript><script type="text/javascript"> +<code javascript>{% if context == "checkout" %} 
-function requirePhone() { +<script type="text/javascript"> 
-  var country = FC.json.shipping_address.country; +function requirePhone(params) { 
-  var phone_required = FC.json.required_fields.indexOf("shipping_phone"); +    var country = params.address.country; 
- if (country != "US" && phone_required == -1) { +    var phone_required = FC.json.required_fields.indexOf("billing_phone"); 
- FC.json.required_fields.push('shipping_phone'); +    if (country != "US" && phone_required == -1) { 
- FC.json.config.template_config.custom_checkout_field_requirements['shipping_phone'] = "required"; +        FC.json.required_fields.push('billing_phone'); 
- FC.checkout.renderCustomerShipping(); +        FC.json.config.template_config.custom_checkout_field_requirements['billing_phone'] = "required"; 
- jQuery("#shipping_phone").attr("placeholder", "Phone"); +        FC.Template("checkout").clearOutput(); 
- } else if (country == "US" && phone_required > -1) { +        FC.checkout.renderCustomerShipping(); 
- FC.json.required_fields.splice(phone_required, 1); +    } else if (country == "US" && phone_required > -1) { 
- FC.json.config.template_config.custom_checkout_field_requirements['shipping_phone'] = "optional"; +        FC.json.required_fields.splice(phone_required, 1); 
- FC.checkout.renderCustomerShipping(); +        FC.json.config.template_config.custom_checkout_field_requirements['billing_phone'] = "optional"; 
- jQuery("#shipping_phone").attr("placeholder", "Phone (Optional)"); +        FC.util.removeError(params.address.prefix + "_phone"); 
- }+        FC.util.notifyErrors()
 +        FC.Template("checkout").clearOutput()
 +        FC.checkout.renderCustomerShipping(); 
 +    }
 } }
-if (FC.json.context == "checkout") { +  
- FC.client.on("customer-address-change", requirePhone); +FC.client.on("customer-address-change", requirePhone); 
- FC.client.on("ready", requirePhone); +FC.client.on("ready.done", function() { 
-+    var addresses = []; 
-</script></code>+    if (FC.json.shipping_address.has_shippable_products) { 
 +        addresses = FC.json.has_multiship ? FC.json.multiship_data : [FC.json.shipping_address]; 
 +    } 
 +    if (FC.json.use_alternate_shipping_address || !FC.json.shipping_address.has_shippable_products) { 
 +        addresses.push(FC.json.billing_address); 
 +    } 
 +    for (var i = 0; i < addresses.length; i++) { 
 +        requirePhone({"address": addresses[i]}); 
 +    } 
 +}); 
 +</script> 
 +{% endif %}</code>

Site Tools