I have a shopify store and I have created a custom form in footer of my store and want to use details added from that form to create a new customer. I have tried to send a post request to endpoint “/admin/customers.json” using code given below
$(document).ready(function(){
$('.klaviyo_submit_button').on('click', function(){
$.ajax({
url: '/admin/customers.json',
dataType: 'json',
type: 'POST',
data:{
"customer": {
"first_name": "Steve",
"last_name": "Lastnameson",
"email": "[email protected]",
"phone": "+15142546011",
"verified_email": true
}
},
success: function() {
alert('Customer is added');
}
});
})
})
But it is not sending post request and shows status code “303 see other”. Can someone please let me know what mistake I made here and how can I add customer from this form. I am using this code in shopify’s theme.js.liquid file.
Thanks
2
Answers
You do not add customers to your Shop from the front end using Javascript calls to /admin. That would mean you’d be exposing App API keys to the public. Instead, Shopify provides you with forms that customers fill in, and they get submitted to the shop and processed securely.
If you insist on adding customers with your own forms, you can setup an App, with permissions to add customers, then use the App Proxy pattern to send the form to the App to create the customers. That is secure, and works.
You cannot create new customer record submitting post data on admin/customers.json directly with public access. Even though your complete requirement is not clear from detail shared by you, I suggest here two solutions.
Solution 1: Use account creation form to create new record
<form method="post" action="/account" id="create_customer" accept-charset="UTF-8">
.Solution 2: Create a custom App in your store and submit your data to a script(you can use PHP) hosted on server which will create a record using Customer API.
Step 1: Go to Apps tab in admin section of your store. Scroll down and click on Manage private apps, create an App here to generate API keys and password.
Step 2: Create and host(on a secured server) a script that will use API keys generated in previous step to create new customer record using https://help.shopify.com/en/api/reference/customers/customer#create
Step 3: Create a form on website and submit your form using Ajax to script in step 2. A new customer record will be created in your store.
Note: You can test this API by using credential created in Step 1 on Postmen tool you can refer https://help.shopify.com/en/api/reference/customers/customer#create. You will have to hit Example URL generated in Private app from Step 1.