Shopify Timber; Attempting to include an 'Add To Cart' in product-grid-item2019 Community Moderator ElectionDynamically add a product to Shopify cartAdd to Cart using AJAX in ShopifyShopify add multiple variants to cart with file upload (without ajax?)Issues sending tag and value to shopping cartjQuery Shopify Timber TabsAdd product in cart by using ShopifyHow to include Retina images in Timber?Using Timber and Shopify, how can I update the cart preview?Shopify liquid, add multiple products to cartShopify Timber Development Workflow
What will happen if my luggage gets delayed?
If nine coins are tossed, what is the probability that the number of heads is even?
Movie: boy escapes the real world and goes to a fantasy world with big furry trolls
Trocar background-image com delay via jQuery
What is the oldest European royal house?
Writing text next to a table
Short scifi story where reproductive organs are converted to produce "materials", pregnant protagonist is "found fit" to be a mother
Why do phishing e-mails use faked e-mail addresses instead of the real one?
Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?
What should I do when a paper is published similar to my PhD thesis without citation?
What does the Digital Threat scope actually do?
Giving a career talk in my old university, how prominently should I tell students my salary?
3.5% Interest Student Loan or use all of my savings on Tuition?
Is there stress on two letters on the word стоят
How can a demon take control of a human body during REM sleep?
How to copy the rest of lines of a file to another file
Either of .... (Plural/Singular)
Is there a logarithm base for which the logarithm becomes an identity function?
Translation of 答えを知っている人はいませんでした
Why aren't there more Gauls like Obelix?
What is better: yes / no radio, or simple checkbox?
Origin of the word “pushka”
Finding the minimum value of a function without using Calculus
How exactly does an Ethernet collision happen in the cable, since nodes use different circuits for Tx and Rx?
Shopify Timber; Attempting to include an 'Add To Cart' in product-grid-item
2019 Community Moderator ElectionDynamically add a product to Shopify cartAdd to Cart using AJAX in ShopifyShopify add multiple variants to cart with file upload (without ajax?)Issues sending tag and value to shopping cartjQuery Shopify Timber TabsAdd product in cart by using ShopifyHow to include Retina images in Timber?Using Timber and Shopify, how can I update the cart preview?Shopify liquid, add multiple products to cartShopify Timber Development Workflow
I know that Timber is no longer supported but I am working on an old account that uses it and am not familiar with the theme.
I have been able to create a button that adds an item to a cart but after clicking add to cart it redirects to the cart page, I just want to add the item and allow the user to still browse the collection.
Any suggestions will be helpful, Thank you in advance
<!-- /snippets/product-grid-item.liquid -->
<form action="/cart/add" data-productid="product.id" method="post" enctype="multipart/form-data" id="AddToCartForm--section.id">
<div class = "variants-wrapper">
% if variants_count > 1 %
<select name="id" data-productid="product.id" id="productSelect--section.id" class="product-single__variants">
% for variant in product.variants %
% if variant.available %
<option % if variant == product.selected_or_first_available_variant % selected="selected" % endif % data-sku=" variant.sku " value=" variant.id "> variant.title - variant.price </option>
% else %
<option disabled="disabled">
variant.title - t
</option>
% endif %
% endfor %
</select>
% else %
<input name="id" data-productid="product.id" type="hidden" value=" product.variants[0].id ">
% endif %
% if sold_out %
<input type="hidden" type="text" id="Quantity1" name="quantity" value="0" min="0" class="quantity-selector quantity-input">
<a class="btn" href=" within: collection "> 'products.product.view_item' </a>
% else %
<div class="qtydiv">
<input type="text" id="Quantity1" name="quantity" value="1" min="1" class="quantity-selector quantity-input">
</div>
<button type="submit" name="add" id="AddToCart" class="btn" style="display: inline">
<span id="AddToCartText"> 'products.product.add_to_cart' </span>
</button>
% endif %
</div>
</form>
ajax-cart.js.liquid
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback)
var params =
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
success: function(line_item)
if ((typeof callback) === 'function')
callback(line_item, form);
else
ShopifyAPI.onItemAdded(line_item, form);
,
error: function(XMLHttpRequest, textStatus)
if ((typeof errorCallback) === 'function')
errorCallback(XMLHttpRequest, textStatus);
else
ShopifyAPI.onError(XMLHttpRequest, textStatus);
;
jQuery.ajax(params);
;
shopify timber
add a comment |
I know that Timber is no longer supported but I am working on an old account that uses it and am not familiar with the theme.
I have been able to create a button that adds an item to a cart but after clicking add to cart it redirects to the cart page, I just want to add the item and allow the user to still browse the collection.
Any suggestions will be helpful, Thank you in advance
<!-- /snippets/product-grid-item.liquid -->
<form action="/cart/add" data-productid="product.id" method="post" enctype="multipart/form-data" id="AddToCartForm--section.id">
<div class = "variants-wrapper">
% if variants_count > 1 %
<select name="id" data-productid="product.id" id="productSelect--section.id" class="product-single__variants">
% for variant in product.variants %
% if variant.available %
<option % if variant == product.selected_or_first_available_variant % selected="selected" % endif % data-sku=" variant.sku " value=" variant.id "> variant.title - variant.price </option>
% else %
<option disabled="disabled">
variant.title - t
</option>
% endif %
% endfor %
</select>
% else %
<input name="id" data-productid="product.id" type="hidden" value=" product.variants[0].id ">
% endif %
% if sold_out %
<input type="hidden" type="text" id="Quantity1" name="quantity" value="0" min="0" class="quantity-selector quantity-input">
<a class="btn" href=" within: collection "> 'products.product.view_item' </a>
% else %
<div class="qtydiv">
<input type="text" id="Quantity1" name="quantity" value="1" min="1" class="quantity-selector quantity-input">
</div>
<button type="submit" name="add" id="AddToCart" class="btn" style="display: inline">
<span id="AddToCartText"> 'products.product.add_to_cart' </span>
</button>
% endif %
</div>
</form>
ajax-cart.js.liquid
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback)
var params =
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
success: function(line_item)
if ((typeof callback) === 'function')
callback(line_item, form);
else
ShopifyAPI.onItemAdded(line_item, form);
,
error: function(XMLHttpRequest, textStatus)
if ((typeof errorCallback) === 'function')
errorCallback(XMLHttpRequest, textStatus);
else
ShopifyAPI.onError(XMLHttpRequest, textStatus);
;
jQuery.ajax(params);
;
shopify timber
add a comment |
I know that Timber is no longer supported but I am working on an old account that uses it and am not familiar with the theme.
I have been able to create a button that adds an item to a cart but after clicking add to cart it redirects to the cart page, I just want to add the item and allow the user to still browse the collection.
Any suggestions will be helpful, Thank you in advance
<!-- /snippets/product-grid-item.liquid -->
<form action="/cart/add" data-productid="product.id" method="post" enctype="multipart/form-data" id="AddToCartForm--section.id">
<div class = "variants-wrapper">
% if variants_count > 1 %
<select name="id" data-productid="product.id" id="productSelect--section.id" class="product-single__variants">
% for variant in product.variants %
% if variant.available %
<option % if variant == product.selected_or_first_available_variant % selected="selected" % endif % data-sku=" variant.sku " value=" variant.id "> variant.title - variant.price </option>
% else %
<option disabled="disabled">
variant.title - t
</option>
% endif %
% endfor %
</select>
% else %
<input name="id" data-productid="product.id" type="hidden" value=" product.variants[0].id ">
% endif %
% if sold_out %
<input type="hidden" type="text" id="Quantity1" name="quantity" value="0" min="0" class="quantity-selector quantity-input">
<a class="btn" href=" within: collection "> 'products.product.view_item' </a>
% else %
<div class="qtydiv">
<input type="text" id="Quantity1" name="quantity" value="1" min="1" class="quantity-selector quantity-input">
</div>
<button type="submit" name="add" id="AddToCart" class="btn" style="display: inline">
<span id="AddToCartText"> 'products.product.add_to_cart' </span>
</button>
% endif %
</div>
</form>
ajax-cart.js.liquid
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback)
var params =
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
success: function(line_item)
if ((typeof callback) === 'function')
callback(line_item, form);
else
ShopifyAPI.onItemAdded(line_item, form);
,
error: function(XMLHttpRequest, textStatus)
if ((typeof errorCallback) === 'function')
errorCallback(XMLHttpRequest, textStatus);
else
ShopifyAPI.onError(XMLHttpRequest, textStatus);
;
jQuery.ajax(params);
;
shopify timber
I know that Timber is no longer supported but I am working on an old account that uses it and am not familiar with the theme.
I have been able to create a button that adds an item to a cart but after clicking add to cart it redirects to the cart page, I just want to add the item and allow the user to still browse the collection.
Any suggestions will be helpful, Thank you in advance
<!-- /snippets/product-grid-item.liquid -->
<form action="/cart/add" data-productid="product.id" method="post" enctype="multipart/form-data" id="AddToCartForm--section.id">
<div class = "variants-wrapper">
% if variants_count > 1 %
<select name="id" data-productid="product.id" id="productSelect--section.id" class="product-single__variants">
% for variant in product.variants %
% if variant.available %
<option % if variant == product.selected_or_first_available_variant % selected="selected" % endif % data-sku=" variant.sku " value=" variant.id "> variant.title - variant.price </option>
% else %
<option disabled="disabled">
variant.title - t
</option>
% endif %
% endfor %
</select>
% else %
<input name="id" data-productid="product.id" type="hidden" value=" product.variants[0].id ">
% endif %
% if sold_out %
<input type="hidden" type="text" id="Quantity1" name="quantity" value="0" min="0" class="quantity-selector quantity-input">
<a class="btn" href=" within: collection "> 'products.product.view_item' </a>
% else %
<div class="qtydiv">
<input type="text" id="Quantity1" name="quantity" value="1" min="1" class="quantity-selector quantity-input">
</div>
<button type="submit" name="add" id="AddToCart" class="btn" style="display: inline">
<span id="AddToCartText"> 'products.product.add_to_cart' </span>
</button>
% endif %
</div>
</form>
ajax-cart.js.liquid
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback)
var params =
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
success: function(line_item)
if ((typeof callback) === 'function')
callback(line_item, form);
else
ShopifyAPI.onItemAdded(line_item, form);
,
error: function(XMLHttpRequest, textStatus)
if ((typeof errorCallback) === 'function')
errorCallback(XMLHttpRequest, textStatus);
else
ShopifyAPI.onError(XMLHttpRequest, textStatus);
;
jQuery.ajax(params);
;
shopify timber
shopify timber
edited 2 days ago
Matman
asked Mar 6 at 23:10
MatmanMatman
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This redirects to cart page, because that is how form action value is defined. So if you want to add it without page redirect and reload, use Shopify AJAX API that allows you manage CRUD operations on cart. Shopify AJAX API.
In the above scenario, you need to have a look at Add to Cart functionality.
Post Data
quantity: 2,
id: 794864229
Sample Code
// Send a seralized form
jQuery.post('/cart/add.js', $('form[action="/cart/add"]').serialize());
// or construct post data
jQuery.post('/cart/add.js',
quantity: 1,
id: 794864229,
properties:
'First name': 'Caroline'
);
Another approach that is a bit easy to implement is to use CartJS
So using data API, it would just be
<button data-cart-add="12345678">Add Product</button>
Sample implementation on Timber Shopify theme
in theme.liquid after timber.js load CartJS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js"></script>
Then below that in the jQuery ready function, initialize CartJS
CartJS.init( cart ,
"dataAPI": true
);
The add this code in product-grid-item.liquid
<button data-cart-add=" product.variants.first.id ">Add Product</button>
This is working on my test installation of Timber theme.
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033642%2fshopify-timber-attempting-to-include-an-add-to-cart-in-product-grid-item%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This redirects to cart page, because that is how form action value is defined. So if you want to add it without page redirect and reload, use Shopify AJAX API that allows you manage CRUD operations on cart. Shopify AJAX API.
In the above scenario, you need to have a look at Add to Cart functionality.
Post Data
quantity: 2,
id: 794864229
Sample Code
// Send a seralized form
jQuery.post('/cart/add.js', $('form[action="/cart/add"]').serialize());
// or construct post data
jQuery.post('/cart/add.js',
quantity: 1,
id: 794864229,
properties:
'First name': 'Caroline'
);
Another approach that is a bit easy to implement is to use CartJS
So using data API, it would just be
<button data-cart-add="12345678">Add Product</button>
Sample implementation on Timber Shopify theme
in theme.liquid after timber.js load CartJS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js"></script>
Then below that in the jQuery ready function, initialize CartJS
CartJS.init( cart ,
"dataAPI": true
);
The add this code in product-grid-item.liquid
<button data-cart-add=" product.variants.first.id ">Add Product</button>
This is working on my test installation of Timber theme.
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
add a comment |
This redirects to cart page, because that is how form action value is defined. So if you want to add it without page redirect and reload, use Shopify AJAX API that allows you manage CRUD operations on cart. Shopify AJAX API.
In the above scenario, you need to have a look at Add to Cart functionality.
Post Data
quantity: 2,
id: 794864229
Sample Code
// Send a seralized form
jQuery.post('/cart/add.js', $('form[action="/cart/add"]').serialize());
// or construct post data
jQuery.post('/cart/add.js',
quantity: 1,
id: 794864229,
properties:
'First name': 'Caroline'
);
Another approach that is a bit easy to implement is to use CartJS
So using data API, it would just be
<button data-cart-add="12345678">Add Product</button>
Sample implementation on Timber Shopify theme
in theme.liquid after timber.js load CartJS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js"></script>
Then below that in the jQuery ready function, initialize CartJS
CartJS.init( cart ,
"dataAPI": true
);
The add this code in product-grid-item.liquid
<button data-cart-add=" product.variants.first.id ">Add Product</button>
This is working on my test installation of Timber theme.
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
add a comment |
This redirects to cart page, because that is how form action value is defined. So if you want to add it without page redirect and reload, use Shopify AJAX API that allows you manage CRUD operations on cart. Shopify AJAX API.
In the above scenario, you need to have a look at Add to Cart functionality.
Post Data
quantity: 2,
id: 794864229
Sample Code
// Send a seralized form
jQuery.post('/cart/add.js', $('form[action="/cart/add"]').serialize());
// or construct post data
jQuery.post('/cart/add.js',
quantity: 1,
id: 794864229,
properties:
'First name': 'Caroline'
);
Another approach that is a bit easy to implement is to use CartJS
So using data API, it would just be
<button data-cart-add="12345678">Add Product</button>
Sample implementation on Timber Shopify theme
in theme.liquid after timber.js load CartJS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js"></script>
Then below that in the jQuery ready function, initialize CartJS
CartJS.init( cart ,
"dataAPI": true
);
The add this code in product-grid-item.liquid
<button data-cart-add=" product.variants.first.id ">Add Product</button>
This is working on my test installation of Timber theme.
This redirects to cart page, because that is how form action value is defined. So if you want to add it without page redirect and reload, use Shopify AJAX API that allows you manage CRUD operations on cart. Shopify AJAX API.
In the above scenario, you need to have a look at Add to Cart functionality.
Post Data
quantity: 2,
id: 794864229
Sample Code
// Send a seralized form
jQuery.post('/cart/add.js', $('form[action="/cart/add"]').serialize());
// or construct post data
jQuery.post('/cart/add.js',
quantity: 1,
id: 794864229,
properties:
'First name': 'Caroline'
);
Another approach that is a bit easy to implement is to use CartJS
So using data API, it would just be
<button data-cart-add="12345678">Add Product</button>
Sample implementation on Timber Shopify theme
in theme.liquid after timber.js load CartJS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js"></script>
Then below that in the jQuery ready function, initialize CartJS
CartJS.init( cart ,
"dataAPI": true
);
The add this code in product-grid-item.liquid
<button data-cart-add=" product.variants.first.id ">Add Product</button>
This is working on my test installation of Timber theme.
edited 2 days ago
answered 2 days ago
Bilal AkbarBilal Akbar
842716
842716
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
add a comment |
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
Sorry, maybe I should have also included the ajax-cart.js info. It states Ajax the add to cart experience by revealing it in a side drawer Plugin Documentation -shopify.github.io/Timber/#ajax-cart. I edited the question to include the shopifyAPI. My only thing is that it does not hit the function.
– Matman
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
updated with sample code
– Bilal Akbar
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033642%2fshopify-timber-attempting-to-include-an-add-to-cart-in-product-grid-item%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown