Event Handler Not Executing During Shopify Checkout

yourbudweiser
New Member
7 0 0

I am trying to execute some code when the Shopify checkout form is submitted but it is not firing.

 

$(document).ready(function () {

  $(document).on('submit', '.ajax-cart__cart-form', function(e) {
    e.preventDefault(); // Prevent the default form submission

    alert('testing');

    if ($('#add-product-checkbox').is(':checked')) {
      // Use Shopify AJAX API to add the product to the cart
      $.ajax({
        type: 'POST',
        url: '/cart/add.js',
        data: {
          quantity: 1, // Adjust the quantity as needed
          id: 12345 // Variant ID of the product to add
        },
        dataType: 'json',
        success: function(response) {
          // Redirect the user to the checkout page
          window.location.href = '/checkout';
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log('Error adding the product to the cart');
          console.log(textStatus);
        }
      });
    } else {
      // If the checkbox is not checked, proceed to the regular checkout
      window.location.href = '/checkout';
    }
  });    
});   

My classes are correct and other jQuery code fires within the document.ready block but this doesn't get touched.

 

Thanks!

Replies 10 (10)

yourbudweiser
New Member
7 0 0

I'm guessing this is not possible due to the lack of a response?

WhiteWater_Web
Shopify Expert
457 17 42

Can please you provide a bit more context what you are trying to achieve? We assume you are on Shopify Plus?

 

Cheers.

 

WhiteWater Web

A certified Shopify Partner and Expert, WhiteWater Web (WWW) is a premium digital solutions company specializing in advanced Shopify Dev, Usability/Design, and Online Strategy. WWW has been working with the Shopify platform since 2006.
info@whitewatersolutions.com
yourbudweiser
New Member
7 0 0

On my cart page, I added a checkbox so the user can add a product to the cart. I want this product to be added if the checkbox is checked but only when the user submits the Checkout form. I have added the code above but nothing happens when the Checkout form is submitted.

 

Yes, I am on ShopifyPlus.

WhiteWater_Web
Shopify Expert
457 17 42

Mind if we take a look at the site, to see an example of what you have done so far?

 

Cheers.

 

WhiteWater Web

A certified Shopify Partner and Expert, WhiteWater Web (WWW) is a premium digital solutions company specializing in advanced Shopify Dev, Usability/Design, and Online Strategy. WWW has been working with the Shopify platform since 2006.
info@whitewatersolutions.com
yourbudweiser
New Member
7 0 0

Removed.

WhiteWater_Web
Shopify Expert
457 17 42

Is this, "Would you like to have your stone crabs claws professionally cracked ($1.50 per pound)" a product in the back end?

A certified Shopify Partner and Expert, WhiteWater Web (WWW) is a premium digital solutions company specializing in advanced Shopify Dev, Usability/Design, and Online Strategy. WWW has been working with the Shopify platform since 2006.
info@whitewatersolutions.com
WhiteWater_Web
Shopify Expert
457 17 42

PM us if you want too.

 

Cheers.

A certified Shopify Partner and Expert, WhiteWater Web (WWW) is a premium digital solutions company specializing in advanced Shopify Dev, Usability/Design, and Online Strategy. WWW has been working with the Shopify platform since 2006.
info@whitewatersolutions.com
yourbudweiser
New Member
7 0 0

PM Sent, thanks.

yourbudweiser
New Member
7 0 0

I am still in search of help with this issue and would appreciate assistance.

yourbudweiser
New Member
7 0 0

*bump* anyone with any ideas or pointers for this?