<p>I am a little confused with your twig code assigning so many variables.</p>

<p>Put simply, only a <code>purchasableId</code> can be used to add something to the cart.</p>

<p>If you know the product ID then it is as simple as:</p>

<pre><code>{% set product = craft.commerce.products.id(6901}.first() %}

{% if product %}
    &lt;form method="POST"&gt;
        &lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
        &lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
        &lt;input type="hidden" name="qty" value="1"&gt;
        &lt;input type="hidden" name="purchasableId" value="{{ product.defaultVariant.purchasableId }}"&gt;

        &lt;button type="submit"&gt;BUY NOW {{ product.defaultPrice|commerceCurrency }}&lt;/button&gt;
    &lt;/form&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-12-08T07:45:09.857Z
Hi Luke and Brad, Thank you. That definitely helped.
Arren Vidal
CC BY-SA 3.0
2016-12-08T02:34:28.780Z
2016-12-08T04:11:34.997Z
2016-12-08T04:11:34.997Z
Luke Holder
<p>I am trying to get the price and add to cart link for a product</p>

<p>This is what I am testing at the moment, is it completely wrong?</p>

<p>========== Setting Variables ===========</p>

<pre><code>{% set giftDepositID = 6901 %}
{% set giftDepositPrices = 0 %}

{% if craft.commerce.products.id(giftDepositID)|length %}
    {% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}
    {% set giftDepositvariant = giftDepositproduct.defaultVariant %}
    {% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}
    {% set giftDepositPrices = craft.childsown.getVariantPrice(giftDepositpurchasableId) %}
{%endif%}
</code></pre>

<p>========== Example HTML ===========</p>

<pre><code>            &lt;div class="col-xs-12 mobile-price-item"&gt;
                    &lt;div class="row"&gt;
                    &lt;div class="col-xs-12"&gt;
                        &lt;img src="{{baseUrl}}/assets/images/softie-robot.jpg" class="img-responsive"&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="clearfix bg-gradient-yellow"&gt;
                    &lt;div class="col-xs-8"&gt;
                        &lt;h3 class="text-gloria"&gt;{{"Deposit"|t}}&lt;/h3&gt;
                    &lt;/div&gt;
                    &lt;div class="col-xs-4"&gt;
                        &lt;h1 class="text-white text-gloria"&gt;&lt;span class="initprice" data-usd="{{giftDepositPrices}}"&gt;${{giftDepositPrices}}&lt;/span&gt;&lt;/h1&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="row"&gt;
                    &lt;div class="col-xs-12"&gt;
                        &lt;form method="POST"&gt;
                            &lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
                            &lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
                            &lt;input type="hidden" name="qty" value="1"&gt;
                            &lt;input type="hidden" name="purchasableId" value="{{giftDepositpurchasableId}}"&gt;
                            &lt;button class="form-control btn btn-yellow text-white" type="submit" data-click=".softie-form .red .btn"&gt;{{"BUY NOW"|t}} &lt;i class="fa fa-play"&gt;&lt;/i&gt;&lt;/button&gt;
                        &lt;/form&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
</code></pre>

<p>Any help/guidance is appreciated.</p>

<p>======= EDIT ========</p>

<p>New Variable set, I have updated the following:</p>

<pre><code>{% set giftDepositID = 6901 %}
{% set giftDepositPrices = 0 %}
{% set giftStandardID = 6899 %}
{% set giftStandardPrices = 0 %}
{% set giftLargeID = 6897 %}
{% set giftLargePrices = 0 %}

{% if craft.commerce.products.id(giftDepositID)|length %}
    {% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}
    {% set giftDepositvariant = giftDepositproduct.defaultVariant %}
    {% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}
    {% set giftDepositPrices = giftDepositvariant.getVariantPrice(giftDepositpurchasableId) %}
{%endif%}
</code></pre>

<p>This is the output of the HTML</p>

<pre><code>&lt;div class="col-sm-4 pad-sm red"&gt;
&lt;img src="https://childsown.com/assets/images/softie-alien.jpg" class="img-responsive"&gt;
&lt;div class="block-container"&gt;
&lt;h3 class="text-gloria"&gt;Deposit&lt;/h3&gt;
&lt;div class="block"&gt;
&lt;h1 class="text-white text-gloria"&gt;&lt;span class="initprice" data-usd=""&gt;$&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;form method="POST"&gt;
&lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
&lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
&lt;input type="hidden" name="qty" value="1"&gt;
&lt;input type="hidden" name="purchasableId" value=""&gt;
&lt;button class="form-control btn btn-red text-white" type="submit" data-click=".softie-form .red .btn"&gt;BUY NOW &lt;i class="fa fa-play"&gt;&lt;/i&gt;&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2016-12-06T07:04:30.153Z
What is `craft.childsown.getVariantPrice`?

Where are those hard coded IDs coming from? For example the `{% set giftDepositID = 6901 %}` Is that a product ID?

Can you also post the HTML output after twig renders?
Luke Holder
CC BY-SA 3.0
2016-12-06T07:32:02.440Z
Yes, that is pulled in from what is already built into the site.
I have updated the original post.
Arren Vidal
CC BY-SA 3.0
2016-12-06T13:12:13.290Z
I can see the purchasableId input in the html output has no value? my guess is you are getting a product/variant incorrectly, maybe the number is wrong?
Luke Holder
CC BY-SA 3.0
2016-12-07T01:28:38.617Z
Okay, thanks @LukeHolder.
So, is this a way to get a product?

`{% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}`
Is this how to get a product variant for a product that does not have multiple variants?
`{% set giftDepositvariant = giftDepositproduct.defaultVariant %}`

Would this be how you get a purchasableID?
`{% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}`

Then is this how you get the pricing of that variant?
`{% set giftDepositPrices = giftDepositvariant.getVariantPrice(giftDepositpurchasableId) %}`
Arren Vidal
<plugin-craftcommerce>
Getting Price and Add to Cart Button. Any thoughts on what I am doing wrong
# Getting Price and Add to Cart Button. Any thoughts on what I am doing wrong

(Asked by: username_0 on 2016-12-06)

I am trying to get the price and add to cart link for a product
This is what I am testing at the moment, is it completely wrong?
========== Setting Variables ===========
{% set giftDepositID = 6901 %}
{% set giftDepositPrices = 0 %}

{% if craft.commerce.products.id(giftDepositID)|length %}
    {% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}
    {% set giftDepositvariant = giftDepositproduct.defaultVariant %}
    {% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}
    {% set giftDepositPrices = craft.childsown.getVariantPrice(giftDepositpurchasableId) %}
{%endif%}

========== Example HTML ===========
            
                    
                    
                        
                    
                
                
                    
                        {{"Deposit"|t}}
                    
                    
                        ${{giftDepositPrices}}
                    
                
                
                    
                        
                            
                            
                            
                            
                            {{"BUY NOW"|t}} 
                        
                    
                
            

Any help/guidance is appreciated.
======= EDIT ========
New Variable set, I have updated the following:
{% set giftDepositID = 6901 %}
{% set giftDepositPrices = 0 %}
{% set giftStandardID = 6899 %}
{% set giftStandardPrices = 0 %}
{% set giftLargeID = 6897 %}
{% set giftLargePrices = 0 %}

{% if craft.commerce.products.id(giftDepositID)|length %}
    {% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}
    {% set giftDepositvariant = giftDepositproduct.defaultVariant %}
    {% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}
    {% set giftDepositPrices = giftDepositvariant.getVariantPrice(giftDepositpurchasableId) %}
{%endif%}

This is the output of the HTML



Deposit

$







BUY NOW

**Tags:** plugin-craftcommerce

> **Comments:**
> **username_1**: What is `craft.childsown.getVariantPrice`?  Where are those hard coded IDs coming from? For example the `{% set giftDepositID = 6901 %}` Is that a product ID?  Can you also post the HTML output after twig renders?  
> **username_0**: Yes, that is pulled in from what is already built into the site. I have updated the original post.  
> **username_1**: I can see the purchasableId input in the html output has no value? my guess is you are getting a product/variant incorrectly, maybe the number is wrong?  
> **username_0**: Okay, thanks @LukeHolder. So, is this a way to get a product?  `{% set giftDepositproduct = craft.commerce.products.id(giftDepositID).find() %}` Is this how to get a product variant for a product that does not have multiple variants? `{% set giftDepositvariant = giftDepositproduct.defaultVariant %}`  Would this be how you get a purchasableID? `{% set giftDepositpurchasableId = giftDepositvariant.purchasableId %}`  Then is this how you get the pricing of that variant? `{% set giftDepositPrices = giftDepositvariant.getVariantPrice(giftDepositpurchasableId) %}`  

---

## 1 answers

### Answer by username_1 on 2016-12-08. Score: 1 | Accepted answer

I am a little confused with your twig code assigning so many variables.
Put simply, only a purchasableId can be used to add something to the cart.
If you know the product ID then it is as simple as:
{% set product = craft.commerce.products.id(6901}.first() %}

{% if product %}
    
        
        
        
        

        BUY NOW {{ product.defaultPrice|commerceCurrency }}
    
{% endif %}

> **Comments:**
> **username_0**: Hi Luke and Brad, Thank you. That definitely helped.  

---

