Adding Buy Button code to HTML

After you create a Buy Button in your Shopify admin, you're ready to add it to your own website or blog.

The process of adding embed code to your website's source HTML is a bit different depending on how and where you want your Buy Buttons and cart to , on your publishing platform, and sometimes on the theme you're using on that platform.

Adding embed code to WordPress

You can add embed code to posts, pages, or templates on your WordPress site.

Add embed code to a WordPress post or page

  1. In Shopify, create a Buy Button and copy its embed code from the Embed Code dialog (or click Copy embed code to clipboard).
  2. From your WordPress dashboard, go to your posts or pages.
  3. Either create a new post or page, or edit an existing one.
  4. Inside the block editor, click the + button, search for Custom HTML, and then click to insert a Custom HTML block.
  5. Paste the embed code into the text field provided by the Custom HTML block.
  6. Optional: Move the Custom HTML block or preview how your Buy Button will display:
    • To move the Custom HTML block, click the arrow buttons in the block's toolbar, or drag the block to a new location.
    • To preview the Buy Button, click Preview on the block toolbar.
  7. When you're done, click the Save Draft, Preview, or Publish button for your post or page.

If you use WordPress's Classic Editor Plugin, then instead of using a Custom HTML block, change the text editor from Visual to Text mode and paste the embed code in the editor.

Add embed code to a WordPress template

If your WordPress theme supports the site editor, then you can add an Embedded Buy Button to any of your site’s templates.

Steps:

  1. In Shopify, create a Buy Button and copy its embed code from the Embed Code dialog (or click Copy embed code to clipboard).
  2. In your WordPress dashboard, go to the Editor.
  3. Navigate to the template that you’d like to edit.
  4. Inside the block editor, click the + button, search for Custom HTML, and then click to insert a Custom HTML block.
  5. Paste the embed code into the text field provided by the Custom HTML block.
  6. Optional: Move the Custom HTML block or preview how your Buy Button will display:
    • To move the Custom HTML block, click the arrow buttons in the block's toolbar, or drag the block to a new location.
    • To preview the Buy Button, click Preview on the block toolbar.
  7. When you're done, click the Preview, or Save button for the template.

Adding embed code to your Shopify blog

Note: #### Steps:
Desktop
  1. From your Shopify admin, go to Online Store > Blog Posts.
  2. From the Blog Posts page, click the title of a blog post you want to edit, or click Add blog post to create a new one.
  3. In the Write your blog post section, click Show HTML in the rich text editor.
  4. Paste your embed code into the main text field.
  5. Click Save.
iPhone
  1. From the Shopify app, tap the button.
  2. In the Sales channels section, tap Online Store.
  3. Tap Blog posts.
Android
  1. From the Shopify app, tap the button.
  2. In the Sales channels section, tap Online Store.
  3. Tap Blog posts.
## Adding embed code to Squarespace

You can add embed code to individual posts in Squarespace and to the menus on your home page. In some cases, you might want to do both. For example, you can embed a cart on your home page to receive purchases from Buy Buttons that you embed on individual posts.

Steps:

  1. From your Squarespace dashboard, open the webpage where you want to add the embed code.
  2. Find the element on the page where you want the Buy Button or embedded cart to display, move your cursor over the Page Content area, and then click EDIT.
  3. Click the insert point where you want to add the embed code.
  4. In the More section of the Content Blocks dialog, click Code.
  5. From your Shopify admin, copy the embed code from the Embed Code dialog.
  6. From your Squarespace dashboard, paste the embed code into the CODE dialog. Make sure that the text field is set to receive HTML.
  7. Click APPLY on the EDIT CODE dialog.
  8. Click SAVE again on the page editor. Because Squarespace disables JavaScript in its dashboard, you need to preview your page separately to confirm the Buy Button or embedded cart is working.

If you complete the steps and your Buy Button isn't loading, then you might need to turn off Ajax loading. For more details, refer to Enable or disable Ajax.

Adding embed code to Wix

After you've created a Buy Button or collection in your Shopify admin, you can add it to your Wix website using the Wix Website Editor.

If you want to display more than one product on your Wix website without editing the embed code, then you can embed a collection. You can create a new collection in your Shopify admin for the products that you want to display on Wix.

Steps:

  1. From your Shopify admin, create a Buy Button for a product or collection, and then copy its embed code.
  1. From your Wix account, find the site you want to edit in the My Sites section, and then click Edit Site.
  2. In the Wix Website Editor, click the + button, and then click More.
  3. Click HTML Code to add an HTML code widget to the page.
  4. Click Enter Code.
  5. On the HTML Settings dialog, paste the embed code for your Buy Button or collection in the Add your code here field and then click Update.
  6. Resize the HTML code widget to fit its contents. If you are creating an embed with a cart, then make sure the cart tab displays in the right place.
  7. When you're done, click Save.

Embed multiple products or collections

Steps:

  1. From your Shopify admin, create a Buy Button for a product or collection, and then copy its embed code.
  2. Paste the code into a code editor or the Wix website builder. Repeat the first two steps until you have created the Buy Buttons you need.
  3. Edit the embed codes to make sure they display in the proper alignment.
  4. From your Wix account, find the site you want to edit in the My Sites section, and then click Edit Site.
  5. In the Wix Website Editor, click the + button, and then click More.
  6. Click HTML Code to add an HTML code widget to the page.
  7. Click Enter Code.
  8. On the HTML Settings dialog, paste the edited code for your Buy Buttons in the Add your code here field and then click Update.
  9. Resize the HTML code widget to fit its contents. If you are creating an embed with a cart, then make sure the cart tab displays in the right place.
  10. When you're done, click Save.

Add script tags separately

Some platforms (like Unbounce) require that you paste the embed code's <script> tags in the page header, and the rest of the embed code on the page where you want the Buy Button to display.

Steps:

  1. From the embed code that's generated when you create a Buy Button, copy both the <div> element and the <script> element. For example:
<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: 'haris-mahmood.myshopify.com',
      apiKey: 'a4e6808b1c4afa490c5b7459f7758347',
      appId: '15'
    });

    ShopifyBuy.UI.onReady(client).then(function (ui) {
      ui.createComponent('product', {
        id: [9017131142],
        node: document.getElementById('product-component-2dd3c8704e6'),
        moneyFormat: '%24%24%24%7B%7Bamount%7D%7D',
        options: {
  "product": {
    "variantId": "all",
    "contents": {
      "variantTitle": false,
      "description": false,
      "buttonWithQuantity": false,
      "quantity": false
    },
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "calc(25% - 20px)",
          "margin-left": "20px",
          "margin-bottom": "50px"
        }
      }
    }
  },
  "cart": {
    "contents": {
      "button": true
    },
    "styles": {
      "footer": {
        "background-color": "#ffffff"
      }
    }
  },
  "modalProduct": {
    "contents": {
      "variantTitle": false,
      "buttonWithQuantity": false,
      "quantity": false
    },
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "100%",
          "margin-left": "0px",
          "margin-bottom": "0px"
        }
      }
    }
  },
  "productSet": {
    "styles": {
      "products": {
        "@media (min-width: 601px)": {
          "margin-left": "-20px"
        }
      }
    }
  }
}
      });
    });
  }
})();
/*]]>*/
</script>
  1. Open the page header for the website where you want to embed a Buy Button.
  2. Paste the entire <script> element in the page header.
  3. Save your changes.
  4. From the original embed code in your Shopify admin, copy only the <div> element. For example:
<div id='product-component-2dd3c8704e6'></div>
  1. Open the page on your website where you want to embed a Buy Button.
  2. Paste the <div> code snippet into the page.
  3. Save your changes.
Can’t find the answers you’re looking for? We’re here to help.