Order Custom Fields

I followed an article from Snipcart’s documentation on adding custom fields to the billing page (Order custom fields – Snipcart Documentation) to include a checkbox for users to accept the terms and conditions before placing an order. However, I encountered an issue where the HTML did not recognize certain Snipcart tags like billing, snipcart-label, and other related Snipcart tags.

Since I have been loading the Snipcart scripts and dynamically creating the Snipcart div programmatically (which has worked fine so far), I decided to also create the custom field for the terms and conditions checkbox programmatically.

private async InitializeSnipcart(): Promise {

//***************** Create Snipcart div if it doesn’t exist *******/

let snipcartDiv = this.document.getElementById(‘snipcart’);

if (!snipcartDiv) {

console.log("snipcartDiv: ", snipcartDiv);

snipcartDiv = this.renderer.createElement('div');

this.renderer.setProperty(snipcartDiv, 'id', 'snipcart');

this.renderer.setAttribute(snipcartDiv, 'hidden', 'true');

this.renderer.setAttribute(snipcartDiv, 'data-api-key', 'my API Key');

this.renderer.appendChild(this.document.body, snipcartDiv);

console.log("snipcartDiv: ", snipcartDiv);

//*************** Programmatically Insert Custom Fields ************/

if (snipcartDiv) {

  console.log("now adding the custom fields");

  this.addTermsCheckboxToCheckout(snipcartDiv!);

}

}

//*****************vinitializeSnipcartEvents ***************** */

this.initializeSnipcartEvents();

console.log(“snipcart loaded successfully”);

}

private addTermsCheckboxToCheckout(snipcartDiv: HTMLElement): void {

// Create the billing section container

const billingSection = this.renderer.createElement(‘billing’);

this.renderer.setAttribute(billingSection, ‘section’, ‘top’);

// Create a fieldset element to group the checkbox

const fieldset = this.renderer.createElement(‘fieldset’);

this.renderer.setAttribute(fieldset, ‘class’, ‘snipcart-form__set’);

// Create the terms and conditions checkbox container

const termsCheckboxDiv = this.renderer.createElement(‘div’);

this.renderer.setAttribute(termsCheckboxDiv, ‘class’, ‘snipcart-form__field’);

// Create the checkbox element for accepting terms

const termsCheckbox = this.renderer.createElement(‘snipcart-checkbox’);

this.renderer.setAttribute(termsCheckbox, ‘name’, ‘acceptTerms’);

// Create the label for the checkbox

const termsLabel = this.renderer.createElement(‘snipcart-label’);

this.renderer.setAttribute(termsLabel, ‘class’, ‘snipcart__font–tiny’);

this.renderer.setAttribute(termsLabel, ‘for’, ‘acceptTerms’);

this.renderer.setProperty(termsLabel, ‘innerHTML’, ‘I accept the Terms and Conditions.’);

// Append the checkbox and label to the terms checkbox div

this.renderer.appendChild(termsCheckboxDiv, termsCheckbox);

// this.renderer.appendChild(termsCheckboxDiv, termsLabel);

// Append the terms checkbox div to the fieldset

this.renderer.appendChild(fieldset, termsCheckboxDiv);

// Append the fieldset to the billing section

this.renderer.appendChild(billingSection, fieldset);

// Append the billing section to the snipcart div

this.renderer.appendChild(snipcartDiv, billingSection);

}

here is the created html element as per the console log:

Yet when I go to the checkout page I dont see the custom field

Any idea how to overcome this

I found the solution.

The mistake I did before is that I tried to add the billing div within the Sinpcart div in my component.
Instead I shoould create an HTML template fiile and refer to that template in the Snipcart Settings

const settings: CustomSnipcartSettings = {

publicApiKey: "my API key",

templatesUrl: "/assets/SnipcartTemplates/snipcart-templates.html",

addProductBehavior: "yes",

modalStyle: "side", // other option is fullscreen

//loadStrategy: "on-user-interaction", // Loads the Snipcart script when the user interacts with the page (e.g., clicks a button).

loadStrategy: "immediate",  //  Loads the Snipcart script immediately when the page loads.

version: "3.7.3", // updated to the latest vers on Aug 31 2024

timeoutDuration: 5000,

domain: "cdn.snipcart.com",

protocol: "https",

loadCSS: true,

};

window[‘SnipcartSettings’] = settings;

below is how the template looks like

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Snipcart Templates</title>
<div id="snipcart-templates">

    <billing section="bottom">

        <fieldset class="snipcart-form__set">

            <div class="snipcart-form__field">

                <div class="snipcart-form__field-checkbox">

                    <snipcart-checkbox name="subscribeToNewsletter"></snipcart-checkbox>

                    <snipcart-label for="subscribeToNewsletter" class="snipcart__font--tiny snipcart-form__label--checkbox">

                        Subscribe to newsletter

                    </snipcart-label>

                </div>

            </div>

            <fieldset class="snipcart-form__set">

                <div class="snipcart-form__field">

                  <snipcart-checkbox name="acceptTerms" required></snipcart-checkbox>

                  <snipcart-label for="acceptTerms">I accept the <a href="/terms-and-conditions">Terms and Conditions</a></snipcart-label>

                </div>

              </fieldset>

        </fieldset>

    </billing>

</div>  

no need to programtically create this div, although I made some sucess in that direction but the styles were not as my expected so I reverted to the originally published solution which now worked for me