Update product options

Hi, there, I am using a similar code system as the example code below.
And it works, except when the Custom option has an additional price.

If, for example, data-item-custom1-options="Black[+230.00]|Brown|Gold", than the option Black won’t be selected in the cart.

Any ideas?

<label>Quantity</label>
<input id="quantity" type="number"></input
<label>Frame color</label>
<select id="frame_color">
  <option value="Black">Black</option>
  <option value="Brown">Brown</option>
  <option value="Gold">Gold</option>
</select>
<button
  id="starry-night"
  class="snipcart-add-item"
  data-item-id="starry-night"
  data-item-price="79.99"
  data-item-url="/paintings/starry-night"
  data-item-description="High-quality replica of The Starry Night by the Dutch post-impressionist painter Vincent van Gogh."
  data-item-image="/assets/images/starry-night.jpg"
  data-item-name="The Starry Night"
  data-item-custom1-name="Frame color"
  data-item-custom1-options="Black|Brown|Gold"
  data-item-custom2-name="Gift note">
  Add to cart
</button>
<script>
const button = document.querySelector('#starry-night')
const quantity = document.querySelector('#quantity')
quantity.addEventListener('change', () => {
  // Sets the default quantity when adding the item
  button.setAttribute('data-item-quantity', quantity.value)
})
const select = document.querySelector('#frame_color')
select.addEventListener('change', () => {
  // Sets the default frame color when adding the item
  button.setAttribute("data-item-custom1-value", select.value)
})
</script>

@mrfreedom I tested the code above using the custom option of black being an additional $230 and it seems to work for us, here’s a video for it Loom | Free Screen & Video Recording Software

Can you tell me what version of Snipcart are you using and can you provide a recording of the issue that you face on your end?

Hi, @ppanth, thank you for answering.

As I can see in your demo, you tested only on the first load, without changing it to another option. My demo is below.

  • On the first page load, if I don’t change options, the option with price works.
  • But, if I change the option and return the option with the price, it doesn’t.

Check out Loom | Free Screen & Video Recording Software

@mrfreedom I think the problem is that your Material select option value has “Metal (+230,00€)” and snipcart looking for only “Metal” as value.
Check your video at 1:13 line 383.

1 Like

@mrfreedom Like @Kian mentioned can you make sure the value that is for custom attributes set via your eventListener is Metal and not Metal (+230,00€) and see if that helps.

Hi, I will test tomorrow and let you know about the result. Thank you both.

Hi, @Kian and @ppanth, you are right, my mistake.

When I create

data-item-custom1-options="Black (+230,00€)[+230.00]|Brown|Gold"

everything works as planned.

Yeah, my mistake.

Thank you very much both for taking the time to answer and help me. :pray:

1 Like