Shopify 购买按钮 - 使图像可点击

时间:2021-01-28 14:44:18

标签: jquery shopify buybutton.js

我在 Shopify 上使用 Buy Button,我已经嵌入了一个系列,单击 Buy 按钮会打开模态,这很好。我正在尝试使图像也可以点击并且也应该打开模态。

我已经完成了一半,实际上我可以使用 ui.openCart() 打开购物车,但我不知道如何访问模态,我尝试了以下方法但无济于事。

>
function ShopifyBuyInit() {
      var client = ShopifyBuy.buildClient({
        domain: 'harrisons-respiratory-protection.myshopify.com',
        storefrontAccessToken: '5548f7e9e0a419188135d4caa368d998',
      });
      ShopifyBuy.UI.onReady(client).then(function (ui) {
        ui.createComponent('collection', {
          id: '<?php echo $catID; ?>',
          node: document.getElementById('collection-component-<?php echo $divID; ?>'),
          moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
          options: {
             "product": {
                "events": {
                    afterInit: function (component) {
                        var elem = $('img', component.node);
                        $(elem).click(function() {
                          e.stopPropagation();
                          ui.openModal();
                        });
                        
                    }
               },
               "styles": {
                 "product": {
                   "@media (min-width: 601px)": {
                     "max-width": "calc(25% - 20px)",
                     "margin-left": "20px",
                     "margin-bottom": "50px",
                     "width": "calc(25% - 20px)"
                   },
                   "img": {
                     "height": "calc(100% - 15px)",
                     "position": "absolute",
                     "left": "0",
                     "right": "0",
                     "top": "0"
                   },
                   "imgWrapper": {
                     "padding-top": "calc(75% + 15px)",
                     "position": "relative",
                     "height": "0"
                   }
                 },
                 "title": {
                   "font-size": "20px",
                   "color": "#555555",
                   "min-height": "<?php echo $three != '' ? $three : '50px'; $three = ''; ?>"
                 },
                 "button": {
                   "font-weight": "bold",
                   ":hover": {
                     "background-color": "#48b5dd"
                   },
                   "background-color": "#50c9f6",
                   ":focus": {
                     "background-color": "#48b5dd"
                   },
                   "border-radius": "4px",
                   "padding-left": "47px",
                   "padding-right": "47px"
                 },
                 "price": {
                   "font-size": "16px",
                   "color": "#555555"
                 },
                 "compareAt": {
                   "font-size": "13.6px",
                   "color": "#555555"
                 },
                 "unitPrice": {
                   "font-size": "13.6px",
                   "color": "#555555"
                 }
               },
               "buttonDestination": "modal",
               "contents": {
                 "options": false
               },
               "text": {
                 "button": "VIEW PRODUCT"
               }
             },
             "productSet": {
               "styles": {
                 "products": {
                   "@media (min-width: 601px)": {
                     "margin-left": "-20px",
                     "text-align" : "left"
                   }
                 }
               }
             },
             "modalProduct": {
               "contents": {
                 "img": false,
                 "imgWithCarousel": true,
                 "button": false,
                 "buttonWithQuantity": true
               },
               "styles": {
                 "product": {
                   "@media (min-width: 601px)": {
                     "max-width": "100%",
                     "margin-left": "0px",
                     "margin-bottom": "0px"
                   }
                 },
                 "button": {
                   "font-weight": "bold",
                   ":hover": {
                     "background-color": "#48b5dd"
                   },
                   "background-color": "#50c9f6",
                   ":focus": {
                     "background-color": "#48b5dd"
                   },
                   "border-radius": "4px",
                   "padding-left": "47px",
                   "padding-right": "47px"
                 },
                 "title": {
                   "color": "#555555"
                 },
                 "price": {
                   "color": "#555555"
                 },
                 "compareAt": {
                   "color": "#555555"
                 },
                "quantityInput": {
                  "-moz-appearance": "textfield"
                },
                "close": {
                  "outline": "0"
                },
                 "unitPrice": {
                   "color": "#555555"
                 },
                 "description": {
                   "color": "#555555"
                 }
               },
               "text": {
                 "button": "BUY NOW"
               }
             },
             "cart": {
               "styles": {
                 "button": {
                   "font-weight": "bold",
                   ":hover": {
                     "background-color": "#48b5dd"
                   },
                   "background-color": "#50c9f6",
                   ":focus": {
                     "background-color": "#48b5dd"
                   },
                   "border-radius": "4px"
                 }
               },
               "text": {
                 "total": "Subtotal",
                 "button": "Checkout"
               }
             },
             "toggle": {
               "styles": {
                 "toggle": {
                   "font-weight": "bold",
                   "background-color": "#50c9f6",
                   ":hover": {
                     "background-color": "#48b5dd"
                   },
                   ":focus": {
                     "background-color": "#48b5dd"
                   }
                 }
               }
             }
           },
        });
      });

1 个答案:

答案 0 :(得分:0)

好的,所以在检查脚本代码并进行一些研究之后,这里是根据您的需要修改行为的代码,您需要修改之前的beforeInit

"events": {
    beforeInit: function (product) {
        Object.defineProperty(product, "isButton", {
            get: function () {
                return true;
            }
        });
        Object.defineProperty(product, "options", {
            get: function () {
                return this.config[this.typeKey];
            }
        });
        var actualOnButtonClick = product.onButtonClick;
        product.onButtonClick = function (event, target) {
            event.stopImmediatePropagation();
            this.options.buttonDestination = "modal";
            actualOnButtonClick.call(this, event, target);
        };
    }
},