Ad

Issues Sending Tag And Value To Shopping Cart

- 1 answer

I currently am working on a site within shopify. I have setup the order form i wish to use and currently everything works fine however i also need to add in a customer text field that relays the teams name to the shopping cart.

Shopify.itemsToAdd = [];
Shopify.addItemstoTheCart = function() {
  if (Shopify.itemsToAdd.length) {
    var item = Shopify.itemsToAdd.pop();
    $.ajax({
      url: '/cart/add',
      dataType: 'json',
      type: 'post',
      data: item,
      success: Shopify.addItemstoTheCart,
      error: Shopify.addItemstoTheCart
    });
  }
  else {
    window.location.href = '/cart';
  }
};
jQuery(function($) {
  $('table .quantity:first').focus();
  $('.add-to-cart-order-form').click(function() {
    $('.add-to-cart-order-form').addClass('disabled').attr('disabled','disabled');
    // Resetting.
    Shopify.itemsToAdd = [];
    $('.quantity').each(function() {
      var quantity = parseInt($(this).val(), 10);
      if (this.checked) {
        Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity} );
      }
    });     
    if (Shopify.itemsToAdd.length) {
      Shopify.addItemstoTheCart();
    }
    else {
      alert('All quantities are set to zero.'); 
      $('.add-to-cart-order-form').removeAttr('disabled').removeClass('disabled');
    }
  });
});

as such i need to send the name from the following inputs name as the variable and the input from the customer as the variable data.

<input type="text" class="tname" name="properties[tname]">

it needs to accompany at the end of this line in the code above.

Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity} );

the data that is sent to the shopping cart is id, quantity, and properties[tname]

i have tried to input this to no success.

Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties[tname]: "walker"} );

listing "walker" as test data for the variable. it continues to error i believe do to the [] but they have to be there for the shopify system to acknowledge the listing and populate the to the cart.

if you can provide any help i would be grateful.

EDIT

ok i can get the teamname to post in the cart but i can only get the first text field to be sent. i have aligned the text fields id tag to match the check boxes data-id tag. and my function works but only for the first pair. after that everything else is sent to the cart without the team names.

jQuery(function($) {
  $('table .quantity:first').focus();
  $('.add-to-cart-order-form').click(function() {
    $('.add-to-cart-order-form').addClass('disabled').attr('disabled','disabled');
    // Resetting.
    Shopify.itemsToAdd = [];
    $('.quantity').each(function() {
      var quantity = parseInt($(this).val(), 10);
      var did = $(this).attr('data-id')
      var idname;
      if (did == $('.tname').attr('id')) {
        var idname = $('.tname').val();
      };
      if (this.checked) {
        Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties: { tname: idname}} );
      }
    });     
    if (Shopify.itemsToAdd.length) {
      Shopify.addItemstoTheCart();
    }
    else {
      alert('All quantities are set to zero.');
      $('.add-to-cart-order-form').removeAttr('disabled').removeClass('disabled');
    }
  });
});
Ad

Answer

Should be:

Shopify.itemsToAdd.push( { id: $(this).attr('data-id'), quantity: quantity, properties: { tname: "walker"}} );
Ad
source: stackoverflow.com
Ad