Product Options: Altering Items Prices with Custom Fields

In this quick tutorial, we'll show you how to use our custom fields to alter and modify the price of an item depending on the item options selected. We'll use a simple scenario for our example: you want to offer the options of natural or smoked flavor for your bacon, and you’d like to charge an extra dollar for the smoked option.

When defining your item properties, you can define custom dropdown fields and optionally associate an option within the dropdown list to a specific price variation, as follow.

<!-- lang: html -->

<a href="#"
  class="snipcart-add-item" 
  data-item-id="2" 
  data-item-name="Oh bacon!" 
  data-item-price="10.00" 
  data-item-weight="20" 
  data-item-url="http://myapp.com/products/bacon" 
  data-item-custom1-name="Flavor"
  data-item-custom1-options="Natural|Smoked[+1.00]">
    Buy now
</a>

Notice that the “Natural” option has no price varation associated. That means that that option has no effect on the item price. On the other hand, notice the the “+1.00” markup next to the “smoked” option. The variation can be either positive or negative. You can use the “+” sign or the “-“ (minus) sign to configure the variation. In our case we’ll be adding an extra 1.00$ to the item price.

We could have also configured the regular price to be the one of the “smoked” option and configured the “Natural” option to remove a dollar, like that.

<!-- lang: html -->

...
data-item-custom1-name="Flavor"
data-item-custom1-options="Natural[-1.00]|Smoked" 
...

Now let’s do something more complicated. In addition to the flavors, we’d also like to offer a cut size option for the bacon, where you’d charge an extra 3.00$ for bigger size bacon. All we’ll need to do is add an additional dropdown custom field and configure the price variations associated to the size options. The final price will be the sum of all the price variations of the selected options.

<!-- lang: html -->

<a href="#"
  class="snipcart-add-item" 
  data-item-id="2" 
  data-item-name="Oh bacon!" 
  data-item-price="10.00" 
  data-item-weight="20" 
  data-item-url="http://myapp.com/products/bacon" 
  data-item-custom1-name="Flavor"
  data-item-custom1-options="Natural|Smoked[+1.00]" 
  data-item-custom2-name="Cut size"
  data-item-custom2-options="Regular|Large[+3.00]">
    Buy now
</a>

When using the option price variation markup you can express advanced pricing configuration for almost any type of product you’d like to sell online.

About the author

Georges Saad
Co-Founder

Georges is the co-founder of Snipcart and Spektrum, a web development company in Québec City. For over a decade, he has been an entrepreneur, a manager, and a mentor. He's also launched SPK ecosystem, an NGO under which Apollo13 (startup incubator), BNKR (coworking space) and Academie (educational branch) operate. Formerly a software engineer and certified Scrum Master by Lean XP founder Ron Jeffries, he popularized the Agile method in Quebec. He's been a featured speaker in dozens of business and web conferences all over the world.

Follow him on LinkedIn.

Simple and Quick Discount Settings

Read next from Georges
View more

36 000+ geeks are getting our monthly newsletter: join them!