|
@@ -9,11 +9,10 @@
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
function BxShopifyShop(oOptions) {
|
|
function BxShopifyShop(oOptions) {
|
|
|
- this._sAPIKey = oOptions.sAPIKey;
|
|
|
|
|
- this._sDomain = oOptions.sDomain;
|
|
|
|
|
- this._sAppId = oOptions.sAppId;
|
|
|
|
|
|
|
+ this._sDomain = oOptions.sDomain;
|
|
|
|
|
+ this._sAccessToken = oOptions.sAccessToken;
|
|
|
|
|
|
|
|
- this._sActionsUrl = oOptions.sActionUrl;
|
|
|
|
|
|
|
+ this._sActionsUrl = oOptions.sActionUrl;
|
|
|
this._sObjName = oOptions.sObjName == undefined ? 'oBxShopifyShop' : oOptions.sObjName;
|
|
this._sObjName = oOptions.sObjName == undefined ? 'oBxShopifyShop' : oOptions.sObjName;
|
|
|
|
|
|
|
|
this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
|
|
this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
|
|
@@ -27,162 +26,236 @@ function BxShopifyShop(oOptions) {
|
|
|
this._sClassLoading = 'bx-spf-loading';
|
|
this._sClassLoading = 'bx-spf-loading';
|
|
|
this._sClassLoaded = 'bx-spf-loaded';
|
|
this._sClassLoaded = 'bx-spf-loaded';
|
|
|
|
|
|
|
|
- this._oShopClient = ShopifyBuy.buildClient({
|
|
|
|
|
- accessToken: this._sAPIKey,
|
|
|
|
|
- domain: this._sDomain,
|
|
|
|
|
- appId: this._sAppId
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ this._oShopClient = window.ShopifyBuy.buildClient({
|
|
|
|
|
+ domain: this._sDomain,
|
|
|
|
|
+ storefrontAccessToken: this._sAccessToken
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-BxShopifyShop.prototype.getUnit = function(iProduct) {
|
|
|
|
|
- var $this = this;
|
|
|
|
|
|
|
+BxShopifyShop.prototype.getUnit = function(sProduct) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- var oUnit = $('.' + this._aHtmlIds['unit'] + iProduct + ':not(.' + this._sClassLoading + ', .' + this._sClassLoaded + '):first');
|
|
|
|
|
- if(!oUnit.length)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ var oUnit = $('.' + this._aHtmlIds['unit'] + sProduct + ':not(.' + this._sClassLoading + ', .' + this._sClassLoaded + '):first');
|
|
|
|
|
+ if(!oUnit.length)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
- bx_loading(oUnit.addClass(this._sClassLoading), true);
|
|
|
|
|
|
|
+ bx_loading(oUnit.addClass(this._sClassLoading), true);
|
|
|
|
|
|
|
|
- this.fetchProduct(iProduct, function (oProduct) {
|
|
|
|
|
- $this.onGetUnit(oProduct);
|
|
|
|
|
- }, function() {
|
|
|
|
|
- bx_loading(oUnit.removeClass(this._sClassLoading), false);
|
|
|
|
|
|
|
+ this.fetchProductByHandle(sProduct, function (oProduct) {
|
|
|
|
|
+ $this.onGetUnit(oProduct);
|
|
|
|
|
+ }, function() {
|
|
|
|
|
+ bx_loading(oUnit.removeClass(this._sClassLoading), false);
|
|
|
|
|
|
|
|
- bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
BxShopifyShop.prototype.onGetUnit = function(oProduct) {
|
|
BxShopifyShop.prototype.onGetUnit = function(oProduct) {
|
|
|
- if(!oProduct)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- var oUnit = $('.' + this._aHtmlIds['unit'] + oProduct.id + '.' + this._sClassLoading + ':first');
|
|
|
|
|
- if(!oUnit.length)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- //--- Fetch product main image.
|
|
|
|
|
- if(oProduct.selectedVariantImage.src) {
|
|
|
|
|
- //--- for Extended view
|
|
|
|
|
- oUnit.find('.bx-spf-unit-cover img').attr('src', oProduct.selectedVariantImage.src).parents('.bx-spf-unit-cover:first').show();
|
|
|
|
|
-
|
|
|
|
|
- //--- for Gallery view
|
|
|
|
|
- oUnit.find('.bx-spf-unit-thumb').css('background-image', 'url(' + oProduct.selectedVariantImage.src + ')');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //--- Fetch product description.
|
|
|
|
|
- oUnit.find('.bx-spf-unit-summary').html(oProduct.description);
|
|
|
|
|
-
|
|
|
|
|
- //--- Fetch price.
|
|
|
|
|
- if(oProduct.selectedVariant.formattedPrice) {
|
|
|
|
|
- var oBuy = oUnit.find('.bx-shopify-buy');
|
|
|
|
|
- var sBuyContent = oBuy.html();
|
|
|
|
|
- oBuy.html(sBuyContent.replace('{price}', oProduct.selectedVariant.formattedPrice));
|
|
|
|
|
- oBuy.attr('href', oProduct.selectedVariant.checkoutUrl(1));
|
|
|
|
|
- oBuy.show();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bx_loading(oUnit.removeClass(this._sClassLoading).addClass(this._sClassLoaded), false);
|
|
|
|
|
-};
|
|
|
|
|
|
|
+ if(!oProduct)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ var oUnit = $('.' + this._aHtmlIds['unit'] + oProduct.handle + '.' + this._sClassLoading + ':first');
|
|
|
|
|
+ if(!oUnit.length)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
-BxShopifyShop.prototype.getEntry = function(iProduct) {
|
|
|
|
|
- var $this = this;
|
|
|
|
|
- var oLoading = $('.' + this._sClassCover);
|
|
|
|
|
|
|
+ //--- Fetch product description.
|
|
|
|
|
+ oUnit.find('.bx-spf-unit-title').html(oProduct.title);
|
|
|
|
|
|
|
|
- bx_loading(oLoading, true);
|
|
|
|
|
|
|
+ //--- Fetch product description.
|
|
|
|
|
+ oUnit.find('.bx-spf-unit-summary').html(oProduct.description);
|
|
|
|
|
+ oUnit.find('.bx-base-text-unit-strecher').html(oProduct.description);
|
|
|
|
|
|
|
|
- this.fetchProduct(iProduct, function (oProduct) {
|
|
|
|
|
- $this.onGetEntry(oProduct);
|
|
|
|
|
- }, function() {
|
|
|
|
|
- bx_loading(oLoading, false);
|
|
|
|
|
|
|
+ //--- Fetch price.
|
|
|
|
|
+ if(oProduct.variants.length > 0) {
|
|
|
|
|
+ var oBuy = oUnit.find('.bx-shopify-buy');
|
|
|
|
|
+ var sBuyContent = oBuy.html();
|
|
|
|
|
+ oBuy.html(sBuyContent.replace('{price}', oProduct.variants[0].price)).show();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ bx_loading(oUnit.removeClass(this._sClassLoading).addClass(this._sClassLoaded), false);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-BxShopifyShop.prototype.onGetEntry = function(oProduct) {
|
|
|
|
|
- if(!oProduct)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+BxShopifyShop.prototype.getEntry = function(sProduct) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
+ var oLoading = $('.' + this._sClassCover);
|
|
|
|
|
|
|
|
- //--- Fetch product main image.
|
|
|
|
|
- var iCoverImageId = oProduct.selectedVariantImage.id;
|
|
|
|
|
- $('.' + this._sClassCover).css('background-image', 'url(' + oProduct.selectedVariantImage.src + ')');
|
|
|
|
|
|
|
+ bx_loading(oLoading, true);
|
|
|
|
|
|
|
|
- //--- Fetch product description.
|
|
|
|
|
- $('#' + this._aHtmlIds['entry_content']).find('.bx-spf-text').html(oProduct.description);
|
|
|
|
|
|
|
+ this.fetchProductByHandle(sProduct, function(oProduct) {
|
|
|
|
|
+ $this.onGetEntry(oProduct);
|
|
|
|
|
+ }, function() {
|
|
|
|
|
+ bx_loading(oLoading, false);
|
|
|
|
|
|
|
|
- //--- Fetch product images.
|
|
|
|
|
- var oSample = $('#' + this._aHtmlIds['entry_attachment_sample']);
|
|
|
|
|
- var oContainer = $('#' + this._aHtmlIds['entry_attachments']);
|
|
|
|
|
- oProduct.images.map(function(oImage) {
|
|
|
|
|
- if(oImage.id == iCoverImageId)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- var oAttachment = oSample.clone().removeAttr('id').removeClass('bx-spf-attachment-sample');
|
|
|
|
|
|
|
+BxShopifyShop.prototype.onGetEntry = function(oProduct) {
|
|
|
|
|
+ if(!oProduct)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ //--- Fetch product title.
|
|
|
|
|
+ $('#' + this._aHtmlIds['entry_content']).find('.bx-spf-title').html(oProduct.title);
|
|
|
|
|
+
|
|
|
|
|
+ //--- Fetch product description.
|
|
|
|
|
+ $('#' + this._aHtmlIds['entry_content']).find('.bx-spf-text').html(oProduct.description);
|
|
|
|
|
+
|
|
|
|
|
+ //--- Fetch product images.
|
|
|
|
|
+ var oSample = $('#' + this._aHtmlIds['entry_attachment_sample']);
|
|
|
|
|
+ var oContainer = $('#' + this._aHtmlIds['entry_attachments']);
|
|
|
|
|
+ var iImage = 0;
|
|
|
|
|
+ oProduct.images.map(function(oImage) {
|
|
|
|
|
+ var oAttachment = oSample.clone().removeAttr('id').removeClass('bx-spf-attachment-sample');
|
|
|
|
|
+
|
|
|
|
|
+ var oLink = oAttachment.find('.bx-spf-attachment-link');
|
|
|
|
|
+ var sLinkOnClick = oLink.attr('onclick');
|
|
|
|
|
+ oLink.attr('onclick', sLinkOnClick.replace('{id}', iImage).replace('{url}', oImage.src));
|
|
|
|
|
+ oLink.find('.bx-spf-attachment-img').attr('src', oImage.src);
|
|
|
|
|
+
|
|
|
|
|
+ var oPopup = oAttachment.find('.bx-popup-wrapper');
|
|
|
|
|
+ var sPopupId = oPopup.attr('id');
|
|
|
|
|
+ oPopup.attr('id', sPopupId.replace('{id}', iImage));
|
|
|
|
|
+ oPopup.find('.bx-spf-attachment-popup-img').attr('src', oImage.src);
|
|
|
|
|
+
|
|
|
|
|
+ oContainer.prepend(oAttachment);
|
|
|
|
|
+
|
|
|
|
|
+ iImage += 1;
|
|
|
|
|
+ });
|
|
|
|
|
+ oSample.remove();
|
|
|
|
|
+
|
|
|
|
|
+ //--- Fetch price.
|
|
|
|
|
+ if(oProduct.variants.length > 0) {
|
|
|
|
|
+ var oBuy = $('#' + this._aHtmlIds['entry_buy']);
|
|
|
|
|
+ var sBuyContent = oBuy.html();
|
|
|
|
|
+ oBuy.html(sBuyContent.replace('{price}', oProduct.variants[0].price)).show();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bx_loading($('.' + this._sClassCover), false);
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- var oLink = oAttachment.find('.bx-spf-attachment-link');
|
|
|
|
|
- var sLinkOnClick = oLink.attr('onclick');
|
|
|
|
|
- oLink.attr('onclick', sLinkOnClick.replace('{id}', oImage.id).replace('{url}', oImage.src));
|
|
|
|
|
- oLink.find('.bx-spf-attachment-img').attr('src', oImage.src);
|
|
|
|
|
|
|
+BxShopifyShop.prototype.buyProductByHandle = function(sProduct, oLink) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
+ oLink = $(oLink);
|
|
|
|
|
+
|
|
|
|
|
+ bx_loading_btn(oLink, true);
|
|
|
|
|
+
|
|
|
|
|
+ $this.fetchProductByHandle(sProduct, function(oProduct) {
|
|
|
|
|
+ if(oProduct.variants.length <= 0)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ $this.createCheckout(function(oCheckout) {
|
|
|
|
|
+ $this.addItems(oCheckout.id, [{variantId:oProduct.variants[0].id, quantity: 1}], function(oCheckout){
|
|
|
|
|
+ bx_loading_btn(oLink, false);
|
|
|
|
|
+
|
|
|
|
|
+ location.href = oCheckout.webUrl;
|
|
|
|
|
+ }, function() {
|
|
|
|
|
+ bx_loading_btn(oLink, false);
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_purchase_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+ }, function() {
|
|
|
|
|
+ bx_loading_btn(oLink, false);
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_purchase_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+ }, function() {
|
|
|
|
|
+ bx_loading_btn(oLink, false);
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+BxShopifyShop.prototype.fetchProduct = function(iProduct, onComplete, onError) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- var oPopup = oAttachment.find('.bx-popup-wrapper');
|
|
|
|
|
- var sPopupId = oPopup.attr('id');
|
|
|
|
|
- oPopup.attr('id', sPopupId.replace('{id}', oImage.id));
|
|
|
|
|
- oPopup.find('.bx-spf-attachment-popup-img').attr('src', oImage.src);
|
|
|
|
|
|
|
+ this._oShopClient.product.fetch(iProduct).then(function(oProduct) {
|
|
|
|
|
+ if(typeof(onComplete) == 'function')
|
|
|
|
|
+ onComplete(oProduct);
|
|
|
|
|
+ else
|
|
|
|
|
+ $this.onFetchProduct(oProduct);
|
|
|
|
|
+ }).catch(function() {
|
|
|
|
|
+ if(typeof(onError) == 'function')
|
|
|
|
|
+ onError();
|
|
|
|
|
+ else
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- oContainer.prepend(oAttachment);
|
|
|
|
|
- });
|
|
|
|
|
- oSample.remove();
|
|
|
|
|
|
|
+BxShopifyShop.prototype.fetchProductByHandle = function(sProduct, onComplete, onError) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- bx_center_content('.bx-base-text-attachments', '.bx-base-text-attachment', true);
|
|
|
|
|
|
|
+ this._oShopClient.product.fetchByHandle(sProduct).then(function(oProduct) {
|
|
|
|
|
+ if(typeof(onComplete) == 'function')
|
|
|
|
|
+ onComplete(oProduct);
|
|
|
|
|
+ else
|
|
|
|
|
+ $this.onFetchProduct(oProduct);
|
|
|
|
|
+ }).catch(function(oError) {
|
|
|
|
|
+ if(typeof(onError) == 'function')
|
|
|
|
|
+ onError();
|
|
|
|
|
+ else
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- //--- Fetch price.
|
|
|
|
|
- var oBuy = $('#' + this._aHtmlIds['entry_buy']);
|
|
|
|
|
- var sBuyContent = oBuy.html();
|
|
|
|
|
- oBuy.html(sBuyContent.replace('{price}', oProduct.selectedVariant.formattedPrice));
|
|
|
|
|
- oBuy.attr('href', oProduct.selectedVariant.checkoutUrl(1));
|
|
|
|
|
- oBuy.show();
|
|
|
|
|
|
|
+BxShopifyShop.prototype.fetchCollection = function(iCollection, onComplete, onError) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- bx_loading($('.' + this._sClassCover), false);
|
|
|
|
|
|
|
+ this._oShopClient.fetchQueryProducts({collection_id: iCollection}).then(function(oProducts) {
|
|
|
|
|
+ if(typeof(onComplete) == 'function')
|
|
|
|
|
+ onComplete(oProduct);
|
|
|
|
|
+ else
|
|
|
|
|
+ $this.onFetchCollection(oProducts);
|
|
|
|
|
+ }).catch(function() {
|
|
|
|
|
+ if(typeof(onError) == 'function')
|
|
|
|
|
+ onError();
|
|
|
|
|
+ else
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_load_collection'));
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-BxShopifyShop.prototype.fetchProduct = function(iProduct, onComplete, onError) {
|
|
|
|
|
- var $this = this;
|
|
|
|
|
|
|
+BxShopifyShop.prototype.createCheckout = function(onComplete, onError) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- this._oShopClient.fetchProduct(iProduct).then(function(oProduct) {
|
|
|
|
|
- if(typeof(onComplete) == 'function')
|
|
|
|
|
- onComplete(oProduct);
|
|
|
|
|
|
|
+ this._oShopClient.checkout.create().then(function(oCheckout) {
|
|
|
|
|
+ if(typeof(onComplete) == 'function')
|
|
|
|
|
+ onComplete(oCheckout);
|
|
|
else
|
|
else
|
|
|
- $this.onFetchProduct(oProduct);
|
|
|
|
|
- }).catch(function() {
|
|
|
|
|
- if(typeof(onError) == 'function')
|
|
|
|
|
- onError();
|
|
|
|
|
|
|
+ $this.onCreateCheckout(oCheckout);
|
|
|
|
|
+ }).catch(function() {
|
|
|
|
|
+ if(typeof(onError) == 'function')
|
|
|
|
|
+ onError();
|
|
|
else
|
|
else
|
|
|
- bx_alert(_t('_bx_shopify_err_load_product'));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_purchase_product'));
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-BxShopifyShop.prototype.fetchCollection = function(iCollection, onComplete, onError) {
|
|
|
|
|
- var $this = this;
|
|
|
|
|
|
|
+BxShopifyShop.prototype.addItems = function(sCheckoutId, aLineItems, onComplete, onError) {
|
|
|
|
|
+ var $this = this;
|
|
|
|
|
|
|
|
- this._oShopClient.fetchQueryProducts({collection_id: iCollection}).then(function(oProducts) {
|
|
|
|
|
- if(typeof(onComplete) == 'function')
|
|
|
|
|
- onComplete(oProduct);
|
|
|
|
|
|
|
+ this._oShopClient.checkout.addLineItems(sCheckoutId, aLineItems).then(function(oCheckout) {
|
|
|
|
|
+ if(typeof(onComplete) == 'function')
|
|
|
|
|
+ onComplete(oCheckout);
|
|
|
else
|
|
else
|
|
|
- $this.onFetchCollection(oProducts);
|
|
|
|
|
- }).catch(function() {
|
|
|
|
|
- if(typeof(onError) == 'function')
|
|
|
|
|
- onError();
|
|
|
|
|
|
|
+ $this.onAddItems(oCheckout);
|
|
|
|
|
+ }).catch(function() {
|
|
|
|
|
+ if(typeof(onError) == 'function')
|
|
|
|
|
+ onError();
|
|
|
else
|
|
else
|
|
|
- bx_alert(_t('_bx_shopify_err_load_collection'));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ bx_alert(_t('_bx_shopify_err_purchase_product'));
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
BxShopifyShop.prototype.onFetchProduct = function(oProduct) {
|
|
BxShopifyShop.prototype.onFetchProduct = function(oProduct) {
|
|
|
- //--- Some default action can be put here.
|
|
|
|
|
|
|
+ //--- Some default action can be put here.
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
BxShopifyShop.prototype.onFetchCollection = function(oProducts) {
|
|
BxShopifyShop.prototype.onFetchCollection = function(oProducts) {
|
|
|
- //--- Some default action can be put here.
|
|
|
|
|
|
|
+ //--- Some default action can be put here.
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+BxShopifyShop.prototype.onCreateCheckout = function(oCheckout) {
|
|
|
|
|
+ //--- Some default action can be put here.
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+BxShopifyShop.prototype.onAddItems = function(oCheckout) {
|
|
|
|
|
+ //--- Some default action can be put here.
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/** @} */
|
|
/** @} */
|