How to increase the product price in Snipcart with checkbox - snipcart

I have been implementing the Snipcart in my website and I want to prompt the user with checkbox, and depending on whether the user checked it or not to increase a price of a product.
Here is my code:
<button
class="snipcart-add-item btn btn-primary"
[attr.data-item-id]="selectedService._id"
[attr.data-item-price]="mainPrice"
data-item-url="http://localhost:4200/"
[attr.data-item-description]="selectedService.jsonData.description.en"
[attr.data-item-name]="selectedService.name"
data-item-custom1-name="Number of additional employees"
data-item-custom1-options="0|1[+25.00]|2[+50.00]|3[+75.00]|4[+100.00]|5[+125.00]|6[+150.00]|7[+175.00]|8[+200.00]|9[+225.00]|10[+250.00]"
data-item-custom2-name="Procure employment pass for USD 1,650.00 to allow a foreigner to serve as the local director."
data-item-custom2-options="false|true[+1650.00]"
>
In your Docs, it says that if the values of "data-item-options" are "true|false", then it will be displayed as a checkbox, but it is displayed as a dropdown list with values "true" and "'false". Am I doing something wrong and is there some other way to implement this?
Also I have tried to use "data-item-custom-type"="checkbox", but then I don't have the option to increase the value of product price (or maybe I just didn't find how to do that).

Checkboxes don't support price modifiers yet, this is something our team will implement in the future though.
For now, we suggest you use a dropdown for this:
data-item-custom2-options="No|Yes[+1650.00]"

Related

validating checkbox checked in Orbeon FormBuilder

How do I validate a checkbox in FormBuilder? I'm looking for something like checkbox.isAny. I'm working on very complex branching on a survey form. If any are boxes are checked, I will show another question in a survey, and so on.
Right now, my formula is looking for checkbox1 or checkbox2 or checkbox3. That works when any 1 checkbox is checked. But when checkbox1 AND checkbox2 is checked the next question does not appear. There must be a way to check for any boxes.
I'm new, so if you can point me to a wiki or online learning source for xforms validation and xpath, I'd greatly appreciate it.
Thank you
If you want to show a question (say named question) if any of the checkboxes for a field (say named checkboxes) is checked, maybe because if any of the checkboxes is set users need to provide more information, then set the Visibility for question to:
$checkboxes != ''

How do I create a dropdown list specifically for Indian states and cities?

My app is planned to go for only India and I need to add the location information to user profiles. So is there any way to create the dropdowns myself for the states and cities or gems specifically for India.
I mean if I could feed the states and cities myself which I want showing up to the user and then allow him to choose his, how do I do that?
I can't work with the text fields because I want to make the whole thing searchable by locations and if users enter different locations the quality of the search will drastically decrease.
Links to tutorials are also welcome :)
you an use the form select helper. if you have all your possible states in activerecord, you can do something like this in your form:
<%= f.select :state_id, State.all.collect {|s| [s.name, s.id]} %>
if you don't want them in activerecord, i would define an array of that in the model, and feed that to the form.
also, take a look at the enumerize, gem, it's useful if you have a finite set of options for a field.
and another thing, there are the countries and cities gems to help you householding the city/country data.
I am now rendering a .json along with the Twitter Typeahead library instead of adding a dropdown. I've uploaded the file to Github for future reference.
Here is a link to the JSON
https://github.com/nshntarora/Indian-Cities-JSON
Here is a link to a SO answer on Typeahead
Rails implementation of search with autocomplete

Preventing form submission conflicts between multiple users (Rails 2.x)

Let's say I have a simple form in the administration of my Rails 2.x powered website.
<form action="/products/123">
Price:
<input type="text" name="product[price]" value="12.99"></input>
Description:
<textarea name="product[description]">
A long and descriptive block of text goes here.
</textarea>
<input type="submit"></input>
</form>
Admin Sally wants to change the description.
Admin Sally opens the page with this form and begins writing some new sales copy in the textarea.
A few seconds later, admin Joe decides this product needs to go on sale
Admin Joe opens the page with the form on it
Admin Joe lowers the price to $9.99
Admin Joe submits the form, setting the price in the database to $9.99
Admin Sally is done writing the copy and submits the form, but the price field in her browser still says $12.99.
Joe becomes angry and yells at Sally for changing the price back, even though she had no idea she just did that.
So the database is updated to contain what Sally had in the form, removing the price that Joe set while she was working with that page open.
Surely I am not the first one to stumble on a problem like this, but I've never had to deal with it. So first of all, does this sort of problem have a name? And second, what are some solutions to making it suck less?
A few solutions that come to mind, but some have serious drawbacks.
Don't save the record if the updated_at timestamp was changed from when you opened the form. But what happens to the content you wanted to save? You'd have to copy the content you wanted to change out of the form, reload the edit form, and then paste it back in, hoping noone else edits anything in the meantime.
Forms load with most fields disabled, requiring the admin to "unlock" the fields they want to edit, and only the unlocked fields get sent to the server on submit. This greatly reduces the likelihood of conflicts in large forms, but adds a bunch of clicks to the content editing process, even it would be fine 99% of the time.
JS based solution that would do dirty field checking, disabling any form input that has the same value as when the form was loaded, so only changed fields are ever submitted, I like this option best so far, I guess. But it does sound a bit complex, and involves dumping my models to JSON on the page for the form submit handler to compare against.
But again, I'm sure I'm not the first person to have this problem. So is there a standard solution for this?
Have you checked this: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html ?
UPDATE
Well, it doesn't really explain what to do if lock_version is wrong, but at least it's better than updated_at I guess. It all depends on who wins and why. If Sally didn't touch price it seems reasonable to update price from Joe's data when saving. Just add original values as hidden fields and compare them with Sally's input when saving in case lock_version is wrong. If they both modified same field, well, no luck. Somebody should win, or you can go back to the edit page and highlight changes for acceptance.

How to get params from checkbox in rails3 controllers

Here is my codes:
<%=form_tag('/user_group_follows/follow',:id=>'follow_select_form',:remote=>true,:method=>:get) do %>
<p>You want to add this user to?</p>
<%=hidden_field_tag 'user_id',#user.id%>
<%#user.user_groups.each do |ug|%>
<%=check_box_tag 'user_group_id',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%><br/>
<%end%>
<%end%>
//using jquery-ui, so there is no submit button....
I wanna the user to make a multiple choice to decide which groups that he/she would like to add into the following list.
So I made several checkboxes with the same name as 'user_group_id' and different ids.
I could successfully get the params throught params[:user_group_id], if the user only checked one box. But if he truly checked several of them, how to get this value set in controller? In this circumstance, params[:user_group_id] could only get one of them. And I quite believe codes like: params[:user_group_id_+XXX.id] is not going to work....
If you name them with id's like user_group_id['+ug.id+'], I think you should get params like params[:user_group_id] which should contain an array of all the id's of groups that were checked.
Something like this, not sure exactly, but basically you want to name your fields such that they are grouped into an array naturally, by virtue of how they are named:
<%=check_box_tag 'user_group_id['+ug.id']',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%>
So, params[:user_group_id].first would contain the id of the first checkbox that was selected.
if you go in this way <%=check_box_tag 'user_group_id[]'%> it's returning array of selected ids,
<%=check_box_tag 'user_group_id',ug.id, params[:user_group_id].try(:include?,ug.id.to_s),{:id=>'user_group_id_'+ug.id.to_s}%>

Rails gem for managing content "segments" on a site, such as promotions?

Im building an eCommerce Rails 3.1 site and need the ability to add promotions throughout the site in various places. For example, the entire content side of the homepage might display a holiday promotion during the holidays, which is essentially an HTML block. Then the product pages would have a banner at the top.
I want to be able to do something like:
= render_content(:homepage_main)
In the views and then use a web-based admin to create and manage the content that is displayed. My requirements are:
Ability to define default content per block
Ability to schedule content for display
Ability to rotate content
Ability to track all views and eventual conversions
Internationalization
Does anyone know of a gem that does all or parts of the above?
What you're describing is essentially a partial that is rendered based on some condition (i.e. is today's date between 'x' and 'y').
I'm not aware of a gem for this, but it shouldn't be too hard to build. Just create a class called Promotion with the following fields:
datetime start_date
datetime end_date
text html_code
string block_id
boolean active
string landing_page
The block_id is essentially an HTML id that is used to insert the content of this Promotion to a <div> with a matching id. Then, in your store's layout, you put place marker <div> tags in places that would hold promotional call outs. The active field could be used to turn a promotion on/off (ignoring the start_date and end_date values).
I think that's pretty much all you would need.
To answer you list of needs:
Set the default value on the html_code field to whatever you want
Scheduling is done via the start_date and end_date fields
Do you mean rotate as in how a jCarousel rotates content visually? You can easily add jCarousel support yourself.
In your view code you can automatically add Google Analytics code to do this, and/or your own code to track how many times a promotion is shown in the rotation, and how many clicks it receives. If you did it a custom way, you would just add a views field for the number of times a given promotion was displayed, and a clicked field for when it was clicked. You can count clicks through a Promotions controller that handles redirects to the appropriate landing page, based on the landing_page field.
See the i18n gem

Resources