我目前正在使用 Shopify Buy Button .
在大多数情况下,我只是复制并粘贴了嵌入代码,并没有做太多更改。
我有一些产品需要购买 5 件或更多的所述商品。
典型的添加到购物袋按钮只添加一个,在购物车内,按下时,添加/减去一个。
注意:我不想对所有商品都使用此方法,只希望嵌入购买代码的特定商品。
我搜索了 default compenents和 developer section并发现以下内容:
var events = {
addVariantToCart: function (product) {},
updateQuantity: function (product) {},
...
}
我想这可能是我要找的东西,但我找不到关于如何更新所述产品的任何具体细节。
如果有人能在正确的方向上帮助或指导我,我将不胜感激。
我的嵌入代码
<div id='product-component-ITEM-ID'></div>
<script type="text/javascript">
/*<![CDATA[*/
(function () {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
if (window.ShopifyBuy){if(window.ShopifyBuy.UI){ShopifyBuyInit();}else{loadScript();}}else{loadScript();}
function loadScript() {var script = document.createElement('script');script.async = true;script.src = scriptURL;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);script.onload = ShopifyBuyInit;}
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({domain: 'DOMAIN.myshopify.com',apiKey: 'API-KEY',appId: '6'});
ShopifyBuy.UI.onReady(client).then(function(ui){ui.createComponent('product',{id:["ITEM-ID"],node:document.getElementById('product-component-{{page.item_id}}'),moneyFormat:'%24%7B%7Bamount%7D%7D',
options:{
"product":{
"variantId":"all",
"width":"240px",
"contents":{
"img":false,
"imgWithCarousel":false,
"title":false,
"variantTitle":false,
"price":false,
"description":false,
"buttonWithQuantity":false,
"quantity":false
},
"text":{
"button":"ADD TO BAG"
},
"styles":{
"product":{
"text-align":"left",
"@media(min-width:601px)":{
"max-width":"100%",
"margin-left":"0",
"margin-bottom":"50px"
}
},
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
"padding-left":"35px",
"padding-right":"35px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"title":{
"font-size":"26px"
},
"price":{
"font-size":"18px"
},
"quantityInput":{
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px"
},
"compareAt":{
"font-size":"15px"
}
},
"googleFonts":[
"Lato"
]
},
"cart":{
"contents":{
"button":true
},
"styles":{
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"footer":{
"background-color":"#ffffff"
}
},
"googleFonts":[
"Lato"
]
},
"modalProduct":{
"contents":{
"img":false,
"imgWithCarousel":true,
"variantTitle":false,
"buttonWithQuantity":true,
"button":false,
"quantity":false
},
"styles":{
"product":{
"@media(min-width:601px)":{
"max-width":"100%",
"margin-left":"0px",
"margin-bottom":"0px"
}
},
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
"padding-left":"35px",
"padding-right":"35px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"quantityInput":{
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px"
}
},
"googleFonts":[
"Lato"
]
},
"toggle":{
"iframe":false,
"sticky":false,
"templates":{
"icon":'<i class="fa fa-shopping-bag fa-lg darkgrey" aria-hidden="true"></i>'
},
"contents":{
"icon":true,
"title":false
},
"events":{
afterInit:function (component) {
document.getElementById('cart-toggle').appendChild(component.node);
},
},
"order":[
'count',
'icon'
],
"styles":{
"toggle":{
"font-family":"Lato,sans-serif",
"background-color":"#393a39",
":hover":{
"background-color":"#333433"
},
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"count":{
"font-size":"13px"
}
},
"googleFonts":[
"Lato"
]
},
"productSet":{
"styles":{
"products":{
"@media(min-width:601px)":{
"margin-left":"-20px"
}
}
}
}
}
}
);});}})();
/*]]>*/
</script>
最佳答案
经过几个小时的实验,这是我想出的解决方案。到目前为止,我的测试表明它稳健可靠,但如果您发现任何问题,请告诉我,我会尽力而为。
以下是代码,使用来自 Shopify Buy Button homepage 的示例代码.不幸的是,这不会在代码片段中运行,但如果您将其放入 HTML 文件并运行它,您就可以对其进行测试。
HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id='product-component-05cfb487fb6' data-minqty='5'></div>
Javascript:
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({
domain: 'embeds.myshopify.com',
apiKey: '952162710f94aa7b7644b14b2a94f4a3',
appId: '6',
});
ShopifyBuy.UI.onReady(client).then(function(ui) {
ui.createComponent('product', {
id: [3030475907],
node: document.getElementById('product-component-05cfb487fb6'),
moneyFormat: '%24%7B%7Bamount%7D%7D',
options: {
"product": {
"styles": {
"button": {
"background-color": "#292929",
":hover": {
"background-color": "#464646"
},
":focus": {
"background-color": "#464646"
}
},
},
"events": {
"addVariantToCart": function(product) {
product.model.selectedQuantity = 1;
var minQty = $(product.node).data('minqty');
if (product.cart.model.lineItemCount < minQty) {
product.updateQuantity(function() {
return minQty - (product.cart.model.lineItemCount);
});
}
}
}
},
"cart": {
"events": {
"updateItemQuantity": function(cart) {
for (let i = 0; i < ui.components.product.length; i++) {
var product = ui.components.product[i];
var node = product.node;
if ($(node).data('minqty')) {
var minQty = $(node).data('minqty');
setTimeout(function() {
setProductQuantity(node, product, minQty, ui);
}, 1);
}
}
}
}
}
}
});
});
}
function setProductQuantity(node, product, minQty, ui) {
if (product.cart.model.lineItemCount < minQty && product.cart.model.lineItemCount > 0) {
$('.shopify-buy-cart-wrapper').find('iframe').contents().find("div[id='" + product.cart.model.lineItems[0].id + "']").find('.shopify-buy__quantity-decrement').click();
}
}
(function() {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
window.ShopifyBuy && window.ShopifyBuy.UI ? ShopifyBuyInit() : loadScript();
function loadScript() {
var script = document.createElement('script');
script.async = true;
script.src = scriptURL;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
script.onload = ShopifyBuyInit;
}
})();
这通过监听来自 Shopify Buy Button 的两个事件来实现:addVariantToCart 和 updateItemQuantity。您要做的第一件事是向产品 div 添加一个 data-minqty 属性。稍后我们将使用该数据属性。
<div id='product-component-05cfb487fb6' data-minqty='5'></div>
第一个事件在点击添加到购物车时触发。我们将听取它,以便我们可以将 minQty 项添加到购物车(在我们的示例中,这是 5)。
"addVariantToCart": function(product) {
product.model.selectedQuantity = 1;
var minQty = $(product.node).data('minqty');
if (product.cart.model.lineItemCount < minQty) {
product.updateQuantity(function() {
return minQty - product.cart.model.lineItemCount;
});
}
}
首先将商品的选定数量设置为 1。这是因为选定数量会自动设置为上次添加的数量,如果我们是第一次将商品添加到购物车,它将 == minQty(即 5)。然后我们从我们之前添加的数据属性中找到最小数量(product.node 是产品的 div 元素)。我们检查数量是否小于 minQty,如果是,我们对产品运行 updateQuantity。出于某种原因,它是相对的,所以我们使用 minQty 和 product.cart.model.lineItemCount 之间的差来运行它,这是当前数量。
第二个事件在更改购物车本身的数量时触发。我们听取它,以确保客户不会将数量减少到 minQty 以下。
"updateItemQuantity": function(cart) {
for (let i = 0; i < ui.components.product.length; i++) {
var product = ui.components.product[i];
var node = product.node;
if ($(node).data('minqty')) {
var minQty = $(node).data('minqty');
setTimeout(function() {
setProductQuantity(node, product, minQty, ui);
}, 1);
}
}
}
由于我们不确切知道购物车中的哪件商品的数量发生了变化,我们会检查所有商品并检查它们是否有 minQty,如果有,是否有他们在下面。然后我们使用 setTimeout,因为这个事件在 数量更新之前触发,所以如果我们在这里更改它,它只会被重置。我们称这个函数为:
function setProductQuantity(node, product, minQty, ui) {
if (product.cart.model.lineItemCount < minQty && product.cart.model.lineItemCount > 0) {
$('.shopify-buy-cart-wrapper').find('iframe').contents().find("div[id='" + product.cart.model.lineItems[0].id + "']").find('.shopify-buy__quantity-decrement').click();
}
}
此函数检查新数量是否小于 minQty。如果不是,我们什么都不做(让数量正常更新)。如果是,我们想从购物车中移除该商品。没有“删除”按钮(它通常依赖于将数量降低到 0 才能从购物车中删除),并且由于我们有最小数量,所以这不可能发生,因此我们需要检查并手动删除它。我们只是模拟点击“数量减少”按钮。我们只需要这样做一次,因为每次我们这样做都会触发 updateItemQuantity,因此它会自循环直到项目被删除。
我无法直接测试您的代码,但我已将我的更改添加到您的代码中,希望您能够弄明白。它确实需要 jQuery,因为我对 native Javascript 的了解不足以完成我需要做的事情。
<div id='product-component-ITEM-ID' data-minqty='5'></div>
<script type="text/javascript">
/*<![CDATA[*/
(function () {
var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
if (window.ShopifyBuy){if(window.ShopifyBuy.UI){ShopifyBuyInit();}else{loadScript();}}else{loadScript();}
function loadScript() {var script = document.createElement('script');script.async = true;script.src = scriptURL;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);script.onload = ShopifyBuyInit;}
function ShopifyBuyInit() {
var client = ShopifyBuy.buildClient({domain: 'DOMAIN.myshopify.com',apiKey: 'API-KEY',appId: '6'});
ShopifyBuy.UI.onReady(client).then(function(ui){ui.createComponent('product',{id:["ITEM-ID"],node:document.getElementById('product-component-{{page.item_id}}'),moneyFormat:'%24%7B%7Bamount%7D%7D',
options:{
"product":{
"variantId":"all",
"width":"240px",
"contents":{
"img":false,
"imgWithCarousel":false,
"title":false,
"variantTitle":false,
"price":false,
"description":false,
"buttonWithQuantity":false,
"quantity":false
},
"events": {
"addVariantToCart": function(product) {
product.model.selectedQuantity = 1;
var minQty = $(product.node).data('minqty');
if (product.cart.model.lineItemCount < minQty) {
product.updateQuantity(function() {
return minQty - (product.cart.model.lineItemCount);
});
}
}
}
"text":{
"button":"ADD TO BAG"
},
"styles":{
"product":{
"text-align":"left",
"@media(min-width:601px)":{
"max-width":"100%",
"margin-left":"0",
"margin-bottom":"50px"
}
},
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
"padding-left":"35px",
"padding-right":"35px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"title":{
"font-size":"26px"
},
"price":{
"font-size":"18px"
},
"quantityInput":{
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px"
},
"compareAt":{
"font-size":"15px"
}
},
"googleFonts":[
"Lato"
]
},
"cart":{
"contents":{
"button":true
},
"events": {
"updateItemQuantity": function(cart) {
for (let i = 0; i < ui.components.product.length; i++) {
var product = ui.components.product[i];
var node = product.node;
if ($(node).data('minqty')) {
var minQty = $(node).data('minqty');
setTimeout(function() {
setProductQuantity(node, product, minQty, ui);
}, 1);
}
}
}
}
"styles":{
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"footer":{
"background-color":"#ffffff"
}
},
"googleFonts":[
"Lato"
]
},
"modalProduct":{
"contents":{
"img":false,
"imgWithCarousel":true,
"variantTitle":false,
"buttonWithQuantity":true,
"button":false,
"quantity":false
},
"styles":{
"product":{
"@media(min-width:601px)":{
"max-width":"100%",
"margin-left":"0px",
"margin-bottom":"0px"
}
},
"button":{
"background-color":"#393a39",
"font-family":"Lato,sans-serif",
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px",
"padding-left":"35px",
"padding-right":"35px",
":hover":{
"background-color":"#333433"
},
"border-radius":"0px",
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"quantityInput":{
"font-size":"13px",
"padding-top":"14.5px",
"padding-bottom":"14.5px"
}
},
"googleFonts":[
"Lato"
]
},
"toggle":{
"iframe":false,
"sticky":false,
"templates":{
"icon":'<i class="fa fa-shopping-bag fa-lg darkgrey" aria-hidden="true"></i>'
},
"contents":{
"icon":true,
"title":false
},
"events":{
afterInit:function (component) {
document.getElementById('cart-toggle').appendChild(component.node);
},
},
"order":[
'count',
'icon'
],
"styles":{
"toggle":{
"font-family":"Lato,sans-serif",
"background-color":"#393a39",
":hover":{
"background-color":"#333433"
},
":focus":{
"background-color":"#333433"
},
"font-weight":"normal"
},
"count":{
"font-size":"13px"
}
},
"googleFonts":[
"Lato"
]
},
"productSet":{
"styles":{
"products":{
"@media(min-width:601px)":{
"margin-left":"-20px"
}
}
}
}
}
}
);});}})();
function setProductQuantity(node, product, minQty, ui) {
if (product.cart.model.lineItemCount < minQty && product.cart.model.lineItemCount > 0) {
$('.shopify-buy-cart-wrapper').find('iframe').contents().find("div[id='" + product.cart.model.lineItems[0].id + "']").find('.shopify-buy__quantity-decrement').click();
}
}
/*]]>*/
</script>
关于javascript - Shopify Buy Button 最小数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683058/
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我在关注RyanbatesRailsCast的devise和omniauth(第235集-devise-and-omniauth-revised)。当我尝试使用Twitter登录时,标题中不断出现错误。defself.new_with_session(params,session)ifsession["devise.user_attributes"]new(session["devise.user_attributes"],without_protection:true)do|user|user.attributes=paramsuser.valid?end完整跟踪:C:/Ruby20
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
Ruby中如何“一般地”计算以下格式(有根、无根)的JSON对象的数量?一般来说,我的意思是元素可能不同(例如“标题”被称为其他东西)。没有根:{[{"title":"Post1","body":"Hello!"},{"title":"Post2","body":"Goodbye!"}]}根包裹:{"posts":[{"title":"Post1","body":"Hello!"},{"title":"Post2","body":"Goodbye!"}]} 最佳答案 首先,withoutroot代码不是有效的json格式。它将没有包
n00b问题警报!这是问题所在:我正在创建一个至少包含3个参数的shell脚本:一个字符串、一个行号和至少一个文件。我已经编写了一个脚本,可以接受EXACTLY3个参数,但我不知道如何处理多个文件名参数。这是我的代码的相关部分(跳过写回文件等):#!/usr/bin/envrubythe_string=ARGV[0]line_number=ARGV[1]the_file=ARGV[2]definsert_script(str,line_n,file)f=files=strln=line_n.to_iif(File.file?f)read_in(f,ln,s)elseputs"false
给定一个包含各种语言字符的UTF-8文件,我如何计算它包含的唯一字符的数量,同时排除选定数量的符号(例如:“!”、“@”、"#",".")从这个算起? 最佳答案 这是一个bash解决方案。:)bash$perl-CSD-ne'BEGIN{$s{$_}++forsplit//,q(!@#.)}$s{$_}++||$c++forsplit//;END{print"$c\n"}'*.utf8 关于python-如何计算文件中唯一字符的数量?,我们在StackOverflow上找到一个类似的问题
如何以最佳方式在字符串中找到唯一元素?示例字符串格式为myString="34345667543"对/对['3','4','3','5'.....] 最佳答案 这是一个有趣的问题,因为它返回了很多几乎相似的结果,所以我做了一个简单的基准测试来决定哪个实际上是最好的解决方案:require'rubygems'require'benchmark'require'set'puts"Dothetest"Benchmark.bm(40)do|x|STRING_TEST="26263636362626218118181111232112233"
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文