Snipcart custom field troubles

Hello,
First, I would like to create this as a post rather than message, and feel like a dummy for not being able to find how to do that :sweat_smile: I thought i was past the user trust level where I can post.

I have been trying to set up Snipcart on a site, and am looking to let users checkmark which size item they can select:

options
https://cooperlovano.com/base/shop/shorts-white

In my individual product PHP:

<?php if ($variationavail > 1): ?>
        <?php foreach ($page->sizeoptions()->split() as $sizeoption): ?>
        <tr>
          <td class="checkmark"><span class="table-cell"><input type="radio" id="productsize" name="productsize" value="<?= $sizeoption ?>"></span></td>
          <td class=""><?= $sizeoption ?></td>
        </tr>
        <?php endforeach ?>
        <?php endif ?>

The checkmarks are displaying appropriately, and I set it up to save the selected size as a PHP cookie. This successfully worked and displayed the selected size on the next page:

(Here is the Snipcart button that created to display the selection):

<button class="cta snipcart-add-item"
        data-item-id="<?= $page->slug() ?>"
        data-item-price="<?= $page->price()->html() ?>"
        data-item-url="<?= $page->url() ?>"
        data-item-image="<?= $page->image()->resize(150)->url() ?>"
        data-item-name="<?= $page->title()->html() ?>"
        data-item-custom1-name="Size"
        data-item-custom1-options="<?php echo $_COOKIE['sizechoice'] ?>">
        Add to cart
      </button>

I am running into problems when trying to reset the cookie value, so the user can go back and select a different size. (The originally selected one stays, it only adds more of that size)

This is quiiite new to me, and I have been running myself into walls.
Is there an alternative approach you would suggest to be able to allow the user to select the size of their item before selecting ‘Add to Cart’?

Hi there,

Snipcart staff member here. :wave:

Typically what we recommend for these scenarios is having some custom frontend (JavaScript) logic that will update the data-item-custom1-value attribute’s value to the options selected by the customer on your site. That way when they finally click the buy button the option will be correctly set in the markup when Snipcart parses the attributes.

I hope this helps.

Don’t hesitate if you have further questions.

1 Like

Hi @thatfrankdev Thanks a lot for helping out here! Highly appreciated.

2 Likes

Thank you @thatfrankdev ! :slight_smile: