Out of Stock Notify email address prefill

I am using the Out of Stock Notify plugin so that my customers have the ability to enter their email address on the product page of an out of stock product to be notified when it comes back in stock. If the customer is logged in, how can I prefill the field with their email address?

Hi Jenny,

Unfortunately this feature is not supported out-of-the-box and can only be achieved with some custom JavaScript.

Here’s one way of getting this done:

1. Use Twig to add the logged-in-customer’s email to the HTML of your page. This will only be present for logged-in users, so there’s no need to worry about putting this personal information into the content.

  • Open BlockLab
  • Edit the Footer layout
  • Add a new Custom HTML block
  • Create a HTML tag with a unique class and use Twig to insert the customer’s email:
    e.g.
<p class="customerEmail">{{ customer.email }}</p>
  • Publish your changes

2. Hide this content with CSS everywhere except in BlockLab:

html#shop #blocklab-footer

  • In BlockLab go to Global CSS (from the BlockLab homepage or Options > Advanced > Edit CSS

  • e.g.

html#shop #blocklab-footer .customerEmail {
    display: none;
}

3. Add JavaScript in the Settings > Tracking area to add the email address to the Out Of Stock Notify input box:

  • Add the following code to the “Footer” script box:
<script>
$(document).ready(function() {
  var customerEmail = $(".customerEmail").text();
  if (typeof customerEmail != "undefined" && customerEmail !== null && customerEmail.length > 0) {
    $("input[name='oos-email']").val(customerEmail);
  }  
});
</script>

This code will only run if the Customer Email is present and the Out Of Stock Email input is present.

1 Like