window.Shopify

505 阅读1分钟

window.Shopify

在shopify项目 控制台输入 window.Shopify 可以看到输出以下

image.png

可能经常用到的

Image

image.png

源码


Shopify.Image = {
    // 预加载图片
    preload: function(t, e) {
        for (var o = 0; o < t.length; o++) {
            var i = t[o];
            this.loadImage(this.getSizedImageUrl(i, e))
        }
    },
    // 懒加载图片
    loadImage: function(t) {
        (new Image).src = t
    },
    switchImage: function(t, e, o) {
        if (t && e) {
            var i = this.imageSize(e.src)
              , r = this.getSizedImageUrl(t.src, i);
            o ? o(r, t, e) : e.src = r
        }
    },
    imageSize: function(t) {
        var e = t.match(/.+_((?:pico|icon|thumb|small|compact|medium|large|grande)|\d{1,4}x\d{0,4}|x\d{1,4})[_\.@]/);
        return null !== e ? e[1] : null
    },
    getSizedImageUrl: function(t, e) {
        if (null == e)
            return t;
        if ("master" == e)
            return this.removeProtocol(t);
        var o = t.match(/\.(jpg|jpeg|gif|png|bmp|bitmap|tiff|tif)(\?v=\d+)?$/i);
        if (null == o)
            return null;
        var i = t.split(o[0])
          , r = o[0];
        return this.removeProtocol(i[0] + "_" + e + r)
    },
    removeProtocol: function(t) {
        return t.replace(/http(s)?:/, "")
    }
}


money_format 和 formatMoney

  • window.Shopify.money_format 设置默认价格货币转换格式, {{amount}} 价格处理格式类型, {{}}内的可选值有 amount、amount_no_decimals、amount_with_comma_separator、amount_with_space_separator、amount_with_period_and_space_separator、amount_no_decimals_with_comma_separator、amount_no_decimals_with_space_separator、amount_with_apostrophe_separator
  • window.Shopify.formatMoney(价格,返回的格式) 获取价格转换后的值

使用

image.png

源码

// 设置价格货币转换,$是美元货币符号, {{amount}}价格处理格式类型
Shopify.money_format = "${{amount}}",
// 价格货币转换方法
Shopify.formatMoney = function(t, e) {
    function n(t, e) {
        return void 0 === t ? e : t
    }
    function o(t, e, o, i) {
        if (e = n(e, 2),
        o = n(o, ","),
        i = n(i, "."),
        isNaN(t) || null == t)
            return 0;
        var r = (t = (t / 100).toFixed(e)).split(".");
        return r[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + o) + (r[1] ? i + r[1] : "")
    }
    "string" == typeof t && (t = t.replace(".", ""));
    var i = ""
      , r = /\{\{\s*(\w+)\s*\}\}/
      , a = e || this.money_format;
    switch (a.match(r)[1]) {
    case "amount":
        i = o(t, 2);
        break;
    case "amount_no_decimals":
        i = o(t, 0);
        break;
    case "amount_with_comma_separator":
        i = o(t, 2, ".", ",");
        break;
    case "amount_with_space_separator":
        i = o(t, 2, " ", ",");
        break;
    case "amount_with_period_and_space_separator":
        i = o(t, 2, " ", ".");
        break;
    case "amount_no_decimals_with_comma_separator":
        i = o(t, 0, ".", ",");
        break;
    case "amount_no_decimals_with_space_separator":
        i = o(t, 0, " ");
        break;
    case "amount_with_apostrophe_separator":
        i = o(t, 2, "'", ".")
    }
    return a.replace(r, i)
}

currency 货币

  • active 当前所属国家的货币单位
  • rate 税率 image.png

其他

window.ShopifyAnalytics 商店分析,可以打印出来看看,了解一下,这里就不做过多说明了,这个看需求而定

image.png