예제 #1
0
        public FacebookShopRequestContainer SyncProduct(ContentItem product)
        {
            var productPart = product.As <ProductPart>();

            try {
                var facebookPart = product.As <FacebookShopProductPart>();

                if (productPart != null && facebookPart != null && facebookPart.SynchronizeFacebookShop)
                {
                    var jsonTemplate = facebookPart.Settings.GetModel <FacebookShopProductPartSettings>().JsonForProductUpdate;
                    _fsssp = _workContext.GetContext().CurrentSite.As <FacebookShopSiteSettingsPart>();
                    if (string.IsNullOrWhiteSpace(jsonTemplate))
                    {
                        // Fallback to FacebookShopSiteSettingsPart
                        jsonTemplate = _fsssp.DefaultJsonForProductUpdate;
                    }

                    if (!string.IsNullOrWhiteSpace(jsonTemplate))
                    {
                        // jsonTemplate typically begins with a double '{' and ends with a double '}' (to make tokens work).
                        // For this reason, before deserialization, I need to replace tokens and replace double parenthesis.
                        string jsonBody = _tokenizer.Replace(jsonTemplate, product);
                        jsonBody = jsonBody.Replace("{{", "{").Replace("}}", "}");

                        var jsonContext = FacebookShopProductUpdateRequest.From(jsonBody);

                        CheckCompliance(jsonContext, product);

                        if (jsonContext != null && jsonContext.Valid)
                        {
                            return(SyncProduct(jsonContext));
                        }
                        else if (jsonContext != null)
                        {
                            // I need to tell it was impossible to synchronize the product on Facebook Shop.
                            Logger.Debug(T("Product {0} can't be synchronized on Facebook catalog.", productPart.Sku).Text);
                            Logger.Debug(jsonContext.Message.Text);

                            var returnValue = new FacebookShopRequestContainer();
                            returnValue.Requests.Add(jsonContext);
                            return(returnValue);
                        }
                    }
                }
            } catch (Exception ex) {
                // I need to tell it was impossible to synchronize the product on Facebook Shop.
                if (productPart != null)
                {
                    Logger.Debug(ex, T("Product {0} can't be synchronized on Facebook catalog.", productPart.Sku).Text);
                }
                else
                {
                    Logger.Debug(ex, T("Product part or Facebook part are not valid.").Text);
                }
                return(null);
            }

            return(null);
        }
예제 #2
0
        public FacebookShopRequestContainer SyncProduct(FacebookShopProductUpdateRequest context)
        {
            FacebookShopRequestContainer requestContainer = new FacebookShopRequestContainer();

            requestContainer.Requests.Add(context);

            // Facebook Shop Site Settings: I need url, catalog id and access token.
            _fsssp = _workContext.GetContext().CurrentSite.As <FacebookShopSiteSettingsPart>();
            FacebookShopServiceContext ctx = new FacebookShopServiceContext()
            {
                ApiBaseUrl  = _fsssp.ApiBaseUrl,
                BusinessId  = _fsssp.BusinessId,
                CatalogId   = _fsssp.CatalogId,
                AccessToken = _fsssp.AccessToken
            };

            FacebookShopProductBatch(requestContainer);

            return(requestContainer);
        }
예제 #3
0
        private IFacebookShopRequest GetJsonContext(ContentItem product)
        {
            var facebookPart = product.As <FacebookShopProductPart>();
            var productPart  = product.As <ProductPart>();

            // Content Item must be a product with the FacebookShopProductPart.
            if (productPart != null && facebookPart != null)
            {
                var jsonTemplate = facebookPart.Settings.GetModel <FacebookShopProductPartSettings>().JsonForProductUpdate;
                if (string.IsNullOrWhiteSpace(jsonTemplate))
                {
                    // Fallback to FacebookShopSiteSettingsPart
                    jsonTemplate = _fsssp.DefaultJsonForProductUpdate;
                }

                // jsonTemplate typically begins with a double '{' and ends with a double '}' (to make tokens work).
                // For this reason, before deserialization, I need to replace tokens and replace double parenthesis.
                string productJson = _tokenizer.Replace(jsonTemplate, facebookPart.ContentItem);
                productJson = productJson.Replace("{{", "{").Replace("}}", "}");

                var jsonContext = FacebookShopProductUpdateRequest.From(productJson);

                CheckCompliance(jsonContext, facebookPart.ContentItem);

                if (jsonContext != null && !jsonContext.Valid)
                {
                    Logger.Debug(jsonContext.Message.Text);
                }

                return(jsonContext);
            }
            else
            {
                Logger.Debug(T("Invalid Product part or Facebook part.").Text);
            }

            return(null);
        }
예제 #4
0
        /// <summary>
        /// This routine checks the compliance of this class with Facebook Shop standards, verifying that default fields are compiled.
        /// It eventually looks for defaults for fields that have been left empty.
        /// </summary>
        private FacebookShopProductUpdateRequest CheckCompliance(FacebookShopProductUpdateRequest context, ContentItem product)
        {
            if (string.IsNullOrWhiteSpace(context.Method))
            {
                context.Method = FacebookShopProductUpdateRequest.METHOD;
            }

            if (string.IsNullOrWhiteSpace(context.RetailerId))
            {
                context.RetailerId = product.As <ProductPart>().Sku;
            }

            if (context.ProductData == null)
            {
                context.ProductData = new FacebookServiceJsonContextData()
                {
                    Valid = true
                };
            }

            CheckCompliance(context.ProductData, product);

            if (context.ProductData == null || !context.ProductData.Valid)
            {
                if (context.ProductData != null && context.ProductData.Message != null)
                {
                    context.Message = context.ProductData.Message;
                }
                else
                {
                    context.Message = T("Invalid data");
                }
                context.Valid = false;
            }

            return(context);
        }