Shopify添加到购物车按钮重定向到Upsell Page

时间:2017-05-09 03:27:33

标签: javascript jquery redirect shopify

我有购买按钮代码,工作得很好。我需要做的是点击按钮后页面重定向到另一个页面,一个追加销售页面。卖家页面在shopify之外,所以我使用购买按钮代码shopify offer。问题是我需要在单击按钮后将按钮嵌入的页面重定向到另一个页面,因此在重定向之前将产品添加到购物车。

这是购买按钮代码

<div id='product-component-NUMBER'></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: 'THESHOP.myshopify.com',
      apiKey: 'APIKEY',
      appId: '6',
    });

    ShopifyBuy.UI.onReady(client).then(function (ui) {
      ui.createComponent('product', {
        id: [8888888888],
        node: document.getElementById('product-component-NUMBER'),
        moneyFormat: '%24%7B%7Bamount%7D%7D',
        options: {
  "product": {
    "variantId": "all",
    "width": "380px",
    "contents": {
      "imgWithCarousel": false,
      "variantTitle": false,
      "description": false,
      "buttonWithQuantity": true,
      "button": false,
      "quantity": false
    },
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "100%",
          "margin-left": "0",
          "margin-bottom": "50px"
        }
      },
      "button": {
        "background-color": "#e43030",
        "font-family": "Lato, sans-serif",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        "padding-left": "50px",
        "padding-right": "50px",
        ":hover": {
          "background-color": "#cd2b2b"
        },
        ":focus": {
          "background-color": "#cd2b2b"
        },
        "font-weight": "bold"
      },
      "quantityInput": {
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px"
      },
      "compareAt": {
        "font-size": "12px"
      }
    },
    "googleFonts": [
      "Lato"
    ]
  },
  "cart": {
    "contents": {
      "button": true
    },
    "styles": {
      "button": {
        "background-color": "#e43030",
        "font-family": "Lato, sans-serif",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        ":hover": {
          "background-color": "#cd2b2b"
        },
        ":focus": {
          "background-color": "#cd2b2b"
        },
        "font-weight": "bold"
      },
      "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": "#e43030",
        "font-family": "Lato, sans-serif",
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px",
        "padding-left": "50px",
        "padding-right": "50px",
        ":hover": {
          "background-color": "#cd2b2b"
        },
        ":focus": {
          "background-color": "#cd2b2b"
        },
        "font-weight": "bold"
      },
      "quantityInput": {
        "font-size": "18px",
        "padding-top": "17px",
        "padding-bottom": "17px"
      }
    },
    "googleFonts": [
      "Lato"
    ]
  },
  "toggle": {
    "styles": {
      "toggle": {
        "font-family": "Lato, sans-serif",
        "background-color": "#e43030",
        ":hover": {
          "background-color": "#cd2b2b"
        },
        ":focus": {
          "background-color": "#cd2b2b"
        },
        "font-weight": "bold"
      },
      "count": {
        "font-size": "18px"
      }
    },
    "googleFonts": [
      "Lato"
    ]
  },
  "productSet": {
    "styles": {
      "products": {
        "@media (min-width: 601px)": {
          "margin-left": "-20px"
        }
      }
    }
  }
}
      });
    });
  }
})();
/*]]>*/
</script>

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery,因为Shopify将其加载到所有主题中。然后只需使用preventDefault(),然后将购物者发送到您喜欢的任何位置。 click事件如下所示:

$("#idOfMyButton").click(function(event){
  event.preventDefault();
  window.location.href = "http://sendtowherever.com";            
});