【问题标题】:Direct link using Shopify's Buy Button使用 Shopify 购买按钮的直接链接
【发布时间】:2021-01-12 22:18:48
【问题描述】:

我有一个用 HTML5 设计的网站,我想链接到产品结帐页面,但我真的不想使用 BuyButton 的所有格式和所有附加功能。

这是 Shopify 告诉我使用的代码:

<div id='product-component-xxx'></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: 'xxx.myshopify.com',
      storefrontAccessToken: 'xxx',
    });
    ShopifyBuy.UI.onReady(client).then(function (ui) {
      ui.createComponent('product', {
        id: 'xxx',
        node: document.getElementById('product-component-xxx'),
        moneyFormat: '%24%7B%7Bamount%7D%7D',
        options: {
  "product": {
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "calc(25% - 20px)",
          "margin-left": "20px",
          "margin-bottom": "50px"
        }
      },
      "button": {
        "font-weight": "bold",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        ":hover": {
          "background-color": "#ff0042"
        },
        "background-color": "#ca0027",
        ":focus": {
          "background-color": "#ff0042"
        },
        "border-radius": "7px",
        "padding-left": "100px",
        "padding-right": "100px"
      },
      "quantityInput": {
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px"
      }
    },
    "buttonDestination": "checkout",
    "contents": {
      "img": false,
      "title": false,
      "price": false
    },
    "text": {
      "button": "Buy now"
    }
  },
  "productSet": {
    "styles": {
      "products": {
        "@media (min-width: 601px)": {
          "margin-left": "-20px"
        }
      }
    }
  },
  "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",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        ":hover": {
          "background-color": "#ff0042"
        },
        "background-color": "#ca0027",
        ":focus": {
          "background-color": "#ff0042"
        },
        "border-radius": "7px",
        "padding-left": "100px",
        "padding-right": "100px"
      },
      "quantityInput": {
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px"
      }
    },
    "text": {
      "button": "Add to cart"
    }
  },
  "cart": {
    "styles": {
      "button": {
        "font-weight": "bold",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        ":hover": {
          "background-color": "#ff0042"
        },
        "background-color": "#ca0027",
        ":focus": {
          "background-color": "#ff0042"
        },
        "border-radius": "7px"
      }
    },
    "text": {
      "total": "Subtotal",
      "button": "Checkout"
    },
    "popup": false
  },
  "toggle": {
    "styles": {
      "toggle": {
        "font-weight": "bold",
        "background-color": "#ca0027",
        ":hover": {
          "background-color": "#ff0042"
        },
        ":focus": {
          "background-color": "#ff0042"
        }
      },
      "count": {
        "font-size": "18px"
      }
    }
  }
},
      });
    });
  }
})();
/*]]>*/
</script>

但我真的只想要一个简单的 HTML 链接来转到产品结帐页面

立即购买

我该怎么做?有没有办法保留我想要和使用的样式,只需让购买按钮打开链接?

谢谢

【问题讨论】:

  • 您使用现有代码对BUY NOW 进行设计更改,但您无法修改默认步骤或流程,如果您想添加直接链接,则使用直接添加到购物车链接添加产品页面,但同样不直接到结帐页面。

标签: javascript html shopify buybutton.js


【解决方案1】:

这里有两个非常简单的选择:

  1. 只需使用 checkout API,它是一个 rest API,您可以将它与 fetch、axios、ajax 一起使用 here more details

  2. 如果您需要更简单的内容,您甚至可以使用 ajax cart api 发布经典表单,您需要产品的变体 ID 和数量。

要查找变体的 ID,请在 shopify 管理员上转到产品页面并在末尾添加 .xml。 改变

your-store.myshopify.com/admin/products/2373831663781

your-store.myshopify.com/admin/products/2373831663781.xml

之后你可以这样做:

await fetch('your-store.myshopify.com/cart/add.js', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    id: 'XXX',
    quantity: 1,
  })
});

它以 json 响应,或者您可以在表单提交上使用 /cart/add,它将重定向到结帐。

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多