Hi I’m new to Figma and I am learning about variables to create an Add to Cart function. So far from watching a tutorial I have created a quantity variable that when clicked up and down adds and subtracts the quantity. I now want to create a function once the ‘Added to Cart’ button is clicked it will add whatever quantity is chosen to the cart in the top right nav. The tutorial didn’t cover this so I am not sure how to go about it.
Hi @Eva4
You can achieve this by:
-
creating a variable with type boolean (e.g:
cartHasItem
) initialized atfalse
(since you don’t have any item in your card you don’t want it to be displayed,
-
then make a new component with your pill. It need to have a property with value : false and true (e.g:
display: false, true
)
the variant display: false, should has an opacity of 0
-
on your prototype frame replace your pill with this new component
-
On the pill instance (the one you just add to your prototype),
4.1 - select its text, on the right panel section Text : click on the hexagon icon at bottom right to have your quantity variable (e.gtotal
)
4.2 - then select the instance again and go to its property, on the right panel, hover thedisplay
property, an icon should appears on its far right (as shown below). Click on it and add you variablecartHasItem
. It should now disappear (sincecartHasItem
is initialized at false)
-
Then go to your add function and append a condition, after your set variable, to check it
total
is above 0. If so setcartHasItem
to true
(it should looks something like that)
-
You can go to your subtract function and append a condition, after your set variable, to check if
total
is equal 0, then set cadHastItem to false.
( make sure thattotal
variable cannot be lowered below 0)
Hope it helped