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
Print a physical multiplication table
PTIJ: Which Dr. Seuss books should one obtain?
Hackerrank All Women's Codesprint 2019: Name the Product
What is the difference between something being completely legal and being completely decriminalized?
Turning a hard to access nut?
label a part of commutative diagram
Gauss brackets with double vertical lines
PTIJ: Why do we make a Lulav holder?
What is the tangent at a sharp point on a curve?
Hot air balloons as primitive bombers
What kind of footwear is suitable for walking in micro gravity environment?
Pre-Employment Background Check With Consent For Future Checks
Was World War I a war of liberals against authoritarians?
The English Debate
What is the reasoning behind standardization (dividing by standard deviation)?
Unfrosted light bulb
Could any one tell what PN is this Chip? Thanks~
Should a narrator ever describe things based on a characters view instead of fact?
CLI: Get information Ubuntu releases
Recursively updating the MLE as new observations stream in
Symbolism of 18 Journeyers
What (if any) is the reason to buy in small local stores?
How do researchers send unsolicited emails asking for feedback on their works?
Isn't the word "experience" wrongly used in this context?
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 - money_with_currency </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 "> t </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"> t </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 - money_with_currency </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 "> t </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"> t </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 - money_with_currency </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 "> t </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"> t </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 - money_with_currency </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 "> t </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"> t </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 Mar 7 at 17:53
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( json ,
"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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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( json ,
"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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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( json ,
"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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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( json ,
"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( json ,
"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 Mar 7 at 23:13
answered Mar 7 at 11:47
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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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
Mar 7 at 17:47
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
Mar 7 at 17:47
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
updated with sample code
– Bilal Akbar
Mar 7 at 23:13
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