Exemplo n.º 1
0
        public void SyncProducts()
        {
            // 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
            };

            // Query on published products, to send them all in a single request to Facebook api.
            // Every call to Facebook api sends the update of 20 products.
            int step = 20;
            // I look for the FacebookShopPart because I may have some products I don't need to synchronize on Facebook Shop (without the FacebookShopProductPart or with the SynchronizeFacebookShopFlag disabled).
            var query = _contentManager.Query <FacebookShopProductPart, FacebookShopProductPartRecord>(VersionOptions.Published)
                        .Where(fp => fp.SynchronizeFacebookShop == true);

            for (int count = 0; count < query.Count(); count += step)
            {
                var facebookParts = _contentManager.Query <FacebookShopProductPart, FacebookShopProductPartRecord>(VersionOptions.Published)
                                    .Where(fp => fp.SynchronizeFacebookShop == true)
                                    .Slice(count, step);

                // I build a container for each slice of the query results.
                FacebookShopRequestContainer requestContainer = new FacebookShopRequestContainer();

                foreach (var facebookPart in facebookParts)
                {
                    var jsonContext = GetJsonContext(facebookPart.ContentItem);

                    if (jsonContext != null && jsonContext.Valid)
                    {
                        requestContainer.Requests.Add(jsonContext);
                    }
                    else if (jsonContext != null)
                    {
                        Logger.Error(T("Product {0} can't be synchronized on Facebook catalog.", jsonContext.RetailerId).Text);
                        Logger.Error(jsonContext.Message.Text);
                    }
                }

                // I can now send the request to Facebook api.
                // This call contains every valid product in the slice from the query.
                FacebookShopProductBatch(requestContainer);
            }
        }
Exemplo n.º 2
0
        public FacebookShopRequestContainer RemoveProduct(FacebookShopProductDeleteRequest 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);
        }
Exemplo n.º 3
0
        public bool CheckCatalog(FacebookShopServiceContext context)
        {
            string url = context.ApiBaseUrl + (context.ApiBaseUrl.EndsWith("/") ? context.CatalogId : "/" + context.CatalogId);

            url = string.Format(url + "?access_token={0}", context.AccessToken);

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            try {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    var          json   = JObject.Parse(reader.ReadToEnd());
                    if (json["error"] == null && json["id"].ToString().Equals(context.CatalogId, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                }
            } catch {
                return(false);
            }
            return(false);
        }