1

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 ===========

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

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

<div class="col-sm-4 pad-sm red">
<img src="https://childsown.com/assets/images/softie-alien.jpg" class="img-responsive">
<div class="block-container">
<h3 class="text-gloria">Deposit</h3>
<div class="block">
<h1 class="text-white text-gloria"><span class="initprice" data-usd="">$</span></h1>
</div>
</div>
<form method="POST">
<input type="hidden" name="action" value="commerce/cart/updateCart">
<input type="hidden" name="redirect" value="commerce/cart">
<input type="hidden" name="qty" value="1">
<input type="hidden" name="purchasableId" value="">
<button class="form-control btn btn-red text-white" type="submit" data-click=".softie-form .red .btn">BUY NOW <i class="fa fa-play"></i></button>
</form>
</div>
4
  • 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? Dec 6, 2016 at 7:04
  • Yes, that is pulled in from what is already built into the site. I have updated the original post. Dec 6, 2016 at 7:32
  • 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? Dec 6, 2016 at 13:12
  • 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) %} Dec 7, 2016 at 1:28

1 Answer 1

1

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 %}
    <form method="POST">
        <input type="hidden" name="action" value="commerce/cart/updateCart">
        <input type="hidden" name="redirect" value="commerce/cart">
        <input type="hidden" name="qty" value="1">
        <input type="hidden" name="purchasableId" value="{{ product.defaultVariant.purchasableId }}">

        <button type="submit">BUY NOW {{ product.defaultPrice|commerceCurrency }}</button>
    </form>
{% endif %}
1
  • Hi Luke and Brad, Thank you. That definitely helped. Dec 8, 2016 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.