Example #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);
        }
Example #2
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);
        }