Esempio n. 1
0
        public async Task <ApiResponse> CreateOrUpdatePage(string html, string title, string url, string id, string referPageId = null)
        {
            ApiResponse res = new ApiResponse();

            try
            {
                var serviceT = new ThemeService(_shopUrl, _accessToken);
                var themes   = await serviceT.ListAsync();

                var theme = themes.FirstOrDefault(x => x.Role == "main");
                if (theme == null)
                {
                    return(res);
                }
                var    serviceA  = new AssetService(_shopUrl, _accessToken);
                string name      = $"punnel-{id}";
                string asset_key = $"templates/page.{name}.liquid";
                var    asset     = new Asset()
                {
                    ContentType = "text/x-liquid",
                    Key         = asset_key,
                    Value       = @"{% layout none %} " + html
                };
                asset = await serviceA.CreateOrUpdateAsync(theme.Id.Value, asset);

                var serviceP = new PageService(_shopUrl, _accessToken);

                Page page = null;

                long?referId = null;
                if (string.IsNullOrEmpty(referPageId) == false)
                {
                    referId = long.Parse(referPageId);
                }
                if (referId != null)
                {
                    page = await serviceP.GetAsync(referId.Value);
                }

                if (page == null)
                {
                    page = await serviceP.CreateAsync(new Page()
                    {
                        CreatedAt      = DateTime.UtcNow,
                        Title          = title,
                        BodyHtml       = $"Landing page {title}. Click [View Page] to view",
                        Handle         = url,
                        TemplateSuffix = name
                    });
                }
                else
                {
                    page = await serviceP.UpdateAsync(referId.Value, new Page()
                    {
                        CreatedAt      = DateTime.UtcNow,
                        Title          = title,
                        BodyHtml       = $"Landing page {title}. Click [View Page] to view",
                        Handle         = url,
                        TemplateSuffix = name
                    });
                }

                res.Data = page.Id;
                res.Code = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                res.Message = "Không thể kết nối đến Shopify, vui lòng thử lại";
            }
            return(res);
        }
Esempio n. 2
0
        public async Task createTemplateAsync(string shop, string token)
        {
            var service  = new ThemeService(shop, token);
            var service1 = new AssetService(shop, token);
            var service2 = new PageService(shop, token);

            var page = new Page()
            {
                CreatedAt      = DateTime.UtcNow,
                Title          = "Wishlist",
                TemplateSuffix = "wishlist",
            };


            var asset = new Asset()
            {
                ContentType = "page.wishlist/x-liquid",
                Key         = "templates/page.wishlist.liquid",
                Value       = "<input type=\"hidden\" name = \"customer_id\" value =\"{{ customer.id }}\" id =\"customer_id\">\n<table>\n<tr>\n<th> Product Image </th>\n<th> Product Title </th>\n<th> SKU </th>\n<th> Barcode </th>\n<th> Customer Name </th>\n<th> Customer Email </th>\n</tr>\n<tbody id = \"todos\"></tbody>\n</table>\n " +
                              "<script type =\"text/javascript \">\n" +
                              "$(document).ready(function() { \n" +
                              "$.ajax({" +
                              "type: 'GET',\n" +
                              "url: 'http://4f9ff7c8.ngrok.io/api/wishlist', \n" +
                              "cache: false,\n" +
                              "crossDomain: true,\n" +
                              "data: { \n" +
                              "customer_id:$('#customer_id').val()\n" +
                              " },\n" +
                              "success: function(data) {\n" +
                              "const tBody = $('#todos');\n" +
                              " $(tBody).empty();\n" +

                              "$.each(data, function(key, item) {\n" +
                              "const tr = $('<tr></tr>')\n" +
                              ".append($('<td></td>').append( $('<img />', {\n" +
                              " src: item.variant_image ,\n" +
                              "style: 'width: 20%'\n" +
                              "})))\n" +
                              ".append($('<td></td>').text(item.product_title))\n" +
                              ".append($('<td></td>').text(item.variant_sku))\n" +
                              ".append($('<td></td>').text(item.variant_barcode))\n" +
                              ".append($('<td></td>').text(item.customer_name))\n" +
                              ".append($('<td></td>').text(item.customer_email));\n" +
                              "tr.appendTo(tBody);\n" +
                              "});\n" +

                              "},\n" +
                              "error: function()\n" +
                              "{\n" +
                              "alert('Error!');\n" +
                              "}\n" +
                              "});\n" +
                              "});\n" +
                              "</script>"
            };

            var themes = await service.ListAsync();


            for (int i = 0; i < themes.Count(); i++)
            {
                if (themes.ElementAt(i).Role == "main")
                {
                    asset = await service1.CreateOrUpdateAsync((long)themes.ElementAt(i).Id, asset);

                    page = await service2.CreateAsync(page);
                }
            }
        }