Shopify 购买按钮

时间:2021-04-21 13:40:12

标签: shopify

我正在使用 Shopify Buy Button,客户问我是否可以从 URL 直接链接到特定产品模式。

我在想,如果我输入 /shop.php?product=12345678,我可以打开该产品模式已经打开的页面,产品查询是 Shopify 的产品 ID。

这可能吗,我想也许是获取 ID,然后在匹配的产品上触发点击事件。

这是我目前使用的代码:

<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: 'harrisons-respiratory-protection.myshopify.com',
        storefrontAccessToken: '5548f7e9e0a419188135d4caa368d998',
      });
      ShopifyBuy.UI.onReady(client).then(function (ui) {
        ui.createComponent('collection', {
          id: '231641579674',
          node: document.getElementById('collection-component-1611309165925'),
          moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
          options: {
             "product": {
               "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;
                      dss = function (event, target) {
                          event.stopImmediatePropagation();
                          this.options.buttonDestination = "modal";
                          actualOnButtonClick.call(this, event, target);
                        }
                      product.onButtonClick = function (event, target) {
                          event.stopImmediatePropagation();
                          this.options.buttonDestination = "modal";
                          actualOnButtonClick.call(this, event, target);
                          alert(ui.components.product.id);
                      };
                  },
                  afterInit: function (product) {

                  }
              },
               "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",
                     "cursor": "pointer"
                   },
                   "imgWrapper": {
                     "padding-top": "calc(75% + 15px)",
                     "position": "relative",
                     "height": "0"
                   }
                 },
                 "title": {
                   "font-size": "20px",
                   "color": "#555555",
                   "min-height": "50px"
                 },
                 "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"
               },
               "popup": false
             },
             "toggle": {
               "styles": {
                 "toggle": {
                   "font-weight": "bold",
                   "background-color": "#50c9f6",
                   ":hover": {
                     "background-color": "#48b5dd"
                   },
                   ":focus": {
                     "background-color": "#48b5dd"
                   }
                 }
               }
             }
           },
        });
      });
    }
  })();
  /*]]>*/
这是页面的链接 - https://www.temporary-url.com/2B4B89

0 个答案:

没有答案