[Skip to main content](#vessel-content)

#   Age Restriction on Checkout \[solved\]

##

- **[ozneilau](/forum/user/3680-ozneilau "View ozneilau's Profile")**
- [![ozneilau's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/3680-ozneilau "View ozneilau's Profile") Topic Author
- [ Offline](/forum/user/3680-ozneilau)

Less More

- Posts: 4
- Thank you received: 2
-
-
- [](https://webilicious.com.au)
-

6 years 6 months ago -  6 years 5 months ago [\#319212](#319212) by [ozneilau](/forum/user/3680-ozneilau "View ozneilau's Profile")  *[Age Restriction on Checkout \[solved\]](/forum/25-install-update/899880-age-restriction-on-checkout-solved#319212 "-- HikaShop version -- : 4.2.3 -- Joomla version -- : 3.9.18 -- PHP version -- : 7.3.17 -- Browser(s) name and version -- : Firefox 76.0.1  To conform to local legislation, the wine sales website I am creating must record the buyers date of birth and ensure that the buyer is at least 18 years old before being able to complete a purchase.  I couldn't find any existing suggestions in the forums that worked sufficiently well for what I need so worked out the following solution for the jQuery UI date picker (the default date picker in the current version). I'm sure there are more elegant solutions but thought I should share this here for anyone else that gets stuck.  1. Create a custom &quot;Date of Birth&quot; field and:  set Field Type = Advanced date picker set Required = Yes set Format = %Y/%m/%d set Allow = Only past dates set Month selector = Yes set Year selector = Yes set Start = 1900  Note: you can set End = -18y so the year is only shown up to 18 years ago but this doesn't take into account the month or date when calculating whether someone is 18 year or older.  2. Add some custom jQuery to the start of the checkout show.php file for your template as follows (change &quot;address&#95;dob&#95;input&quot; to your own custom field name):  References:    www.sitepoint.com/community/t/how-to-val...ry-datepicker/117179    stackoverflow.com/q/29137763  Edited 2nd June 2020:  Alternative Method using PHP  So the jQuery method works but after my client wanted to split the checkout into two steps, shoppers could go back to the first step, amend the date of birth and continue the checkout with a non-validated date.  So I came up with a way to check the DOB in PHP which I implemented instead of the jQuery solution also to the checkout show.php file:  The code is only active on the second step of the checkout process. You will need to change the custom field name and the link as appropriate.")* was created by *[ozneilau](/forum/user/3680-ozneilau "View ozneilau's Profile")*

-- HikaShop version -- : 4.2.3
-- Joomla version -- : 3.9.18
-- PHP version -- : 7.3.17
-- Browser(s) name and version -- : Firefox 76.0.1

To conform to local legislation, the wine sales website I am creating must record the buyers date of birth and ensure that the buyer is at least 18 years old before being able to complete a purchase.

I couldn't find any existing suggestions in the forums that worked sufficiently well for what I need so worked out the following solution for the jQuery UI date picker (the default date picker in the current version). I'm sure there are more elegant solutions but thought I should share this here for anyone else that gets stuck.

**1. Create a custom "Date of Birth" field and:**

set Field Type = Advanced date picker
set Required = Yes
set Format = %Y/%m/%d
set Allow = Only past dates
set Month selector = Yes
set Year selector = Yes
set Start = 1900

Note: you can set End = -18y so the year is only shown up to 18 years ago but this doesn't take into account the month or date when calculating whether someone is 18 year or older.

**2. Add some custom jQuery to the start of the checkout show.php file for your template as follows (change "address\_dob\_input" to your own custom field name):**
Code:

&lt;script&gt; jQuery(document).ready(function($) { $(document).ready(function(){ $("#address_dob_input").datepicker(); $("#address_dob_input").on('change', function(){ var birthday = document.getElementById("address_dob_input").value; var dob = new Date(birthday); var today = new Date(); var age = today.getTime() - dob.getTime(); age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25)); if (age &lt; 18){ alert('You must be over 18 years of age'); $(this).val(''); } }); }); }); &lt;/script&gt;

**References:**

[ www.sitepoint.com/community/t/how-to-val...ry-datepicker/117179](https://www.sitepoint.com/community/t/how-to-validate-date-on-jquery-datepicker/117179)
[ stackoverflow.com/q/29137763](https://stackoverflow.com/q/29137763)

Edited 2nd June 2020:

**Alternative Method using PHP**

So the jQuery method works but after my client wanted to split the checkout into two steps, shoppers could go back to the first step, amend the date of birth and continue the checkout with a non-validated date.

So I came up with a way to check the DOB in PHP which I implemented instead of the jQuery solution also to the checkout show.php file:

Code:

&lt;?php if ($this-&gt;step == 2) { $app = JFactory::getApplication(); $cart = $this-&gt;checkoutHelper-&gt;getCart(); if (is_string($cart-&gt;billing_address-&gt;address_dob)) { $dob = strtotime($cart-&gt;billing_address-&gt;address_dob); } if(time() - $dob &lt; 18 * 31556926) { $message = "&lt;h3&gt;&lt;strong&gt;You must be over 18&lt;/strong&gt;&lt;/h3&gt;&lt;a href='/wines/checkout'&gt;Click here&lt;/a&gt; to go back to the previous page, click on the 'change guest information' button and check the date of birth you have entered&lt;br /&gt;"; $app-&gt;enqueueMessage($message); return; } } ?&gt;

The code is only active on the second step of the checkout process. You will need to change the custom field name and the link as appropriate.

Last edit: 6 years 5 months ago by [ozneilau](/forum/user/3680-ozneilau "View ozneilau's Profile"). Reason: Fix spelling

The following user(s) said Thank You: [nicolas](/forum/user/63-nicolas "View nicolas's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzg5OTg4MC1hZ2UtcmVzdHJpY3Rpb24tb24tY2hlY2tvdXQtc29sdmVkP3RtcGw9bWFya2Rvd24=) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.046 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
