コード例 #1
0
        /// <summary>
        /// Adds the items from an EA order to a Cart in Magento.
        /// If an item on the order cannot be found in Magento an exception is thrown.
        /// </summary>
        /// <param name="orderId">Order to get items for</param>
        /// <param name="cartId">Cart to add items to</param>
        public void AddOrderItemsToCart(string orderId, int cartId)
        {
            Guid result;

            if (!Guid.TryParse(orderId, out result))
            {
                throw new ArgumentException("Argument must be a valid Guid.", nameof(orderId));
            }
            IEnumerable <OrderItemResource> orderItems = _eaOrderController.GetOrderItems(orderId);

            foreach (var orderItem in orderItems)
            {
                CatalogItemResource catalogItem = _eaCatalogsController.GetCatalogItem(orderItem.ProductId);
                ProductResource     product     = _magentoProductController.SearchForProducts(ConfigReader.MappingCode, catalogItem.Slug,
                                                                                              ConfigReader.MagentoEqualsCondition).items.FirstOrDefault();

                if (product == null)
                {
                    throw new Exception(string.Format("Magento product could not be found for product {0} on order {1} with mapping code {2}",
                                                      orderItem.SKU, orderId, catalogItem.Slug));
                }

                CartAddItemResource cartAddItem = new CartAddItemResource(cartId, product.sku, orderItem.Quantity);
                _magentoCartController.AddItemToCart(cartId, cartAddItem);
            }
        }
コード例 #2
0
        /**
         * This function creates a master product
         *
         * @param  productMapping          Mapping value, can be null
         * @param  eaCategoryId            Identifier for a category in EA
         * @param  eaClassificationTreeId  Identifier for a classification tree in ea
         * @param  eaManufacturerId        Identifier for a manufacturer in EA
         * @param  fields                  Fields representing changes to product details
         * @param  assets                  Assets for product
         *
         * @return int                     Identifier of a product document in EA
         */
        public int UpsertMasterProduct(string productMapping, int eaCategoryId, int eaClassificationTreeId, int?eaManufacturerId, List <FieldResource> fields, List <AssetResource> assets)
        {
            var productDocumentId = -1;

            if (productMapping != null)
            {
                var slug = _eaCatalogController.GetCatalogItem(productMapping).Slug;

                productDocumentId = _eaProductController.UpdateMasterProduct(GetProductDocumentIdFromSlug(slug),
                                                                             new RevisionEditResource
                {
                    FieldValues       = fields,
                    Assets            = assets,
                    IdentifierGroups  = null,
                    ColorDefinitionId = null
                });
            }
            else
            {
                var requestBody = new ProductDocumentResource
                {
                    Classification = new ProductClassificationRefResource
                    {
                        Id     = eaCategoryId,
                        TreeId = eaClassificationTreeId
                    },
                    OwnerEntityId = ConfigReader.EaCompanyId,
                    RootRevision  = new RootRevisionResource
                    {
                        FieldValues = fields,
                        Assets      = assets
                    }
                };

                if (eaManufacturerId != null)
                {
                    requestBody.Manufacturer = new EntityRefResource {
                        Id = (int)eaManufacturerId
                    };
                }

                productDocumentId = _eaProductController.CreateMasterProduct(requestBody).Id;
            }

            return(productDocumentId);
        }
コード例 #3
0
        /**
         * This function extracts a list of assets from a magento product and returns them as a list of EA assets
         * If the magento product has a mapping, it will cross reference existing assets to prevent assets from being uploaded
         * multiple times
         * Note that this WILL NOT delete assets that were removed
         *
         * @param   magentoProduct          Magento product to parse
         *
         * @return  List<AssetResource>     Assets
         */
        public List <AssetResource> ParseAssetsFromProduct(ProductResource magentoProduct, MediaStorageConfiguration configuration)
        {
            var assets = new List <AssetResource>();

            //Get existing magento assets
            var magentoAssets = magentoProduct.media_gallery_entries;

            //Path to magento assets
            var magentoPath = new UrlFormatter().MagentoCatalogAssetPath(ConfigReader.MagentoServerPath);

            if (ProductHasMapping(magentoProduct))
            {
                //Get master product Id from the mapping slug
                var catalogItemId =
                    GetAttributeByCode(magentoProduct.custom_attributes, ConfigReader.MappingCode).ToString();

                var slug = _eaCatalogsController.GetCatalogItem(catalogItemId).Slug;

                var eaAssets = _eaProductController.GetProductBySlug(slug).Assets.ToList();

                //Loop through magento product assets. This update can only ADD assets, not remove or change
                foreach (var magentoAsset in magentoAssets)
                {
                    bool  hasChanged   = true;
                    Image magentoImage = null;

                    switch (configuration)
                    {
                    case MediaStorageConfiguration.FileSystem:
                        magentoImage = Image.FromFile(magentoPath + magentoAsset.file);
                        break;

                    case MediaStorageConfiguration.Database:
                        magentoImage = ImageUtility.ImageFromBytes(DatabaseConnection.Instance.GetMediaGalleryEntryFile(magentoAsset));
                        break;
                    }

                    //Is there a matching asset in the EA product? Only compare name
                    foreach (var eaAsset in eaAssets)
                    {
                        if (eaAsset.Name == magentoAsset.file.Substring(magentoAsset.file.LastIndexOf('/') + 1) && ImageUtility.AreEqual(magentoImage, ImageUtility.ImageFromUri(eaAsset.Uri)))
                        {
                            //Add asset, no further processing
                            assets.Add(new AssetResource
                            {
                                Id       = eaAsset.Id,
                                Name     = eaAsset.Name,
                                IsHidden = eaAsset.IsHidden,
                                MimeType = _eaAssetsController.GetAsset(eaAsset.Id.ToString()).MimeType
                            });

                            hasChanged = false;
                        }
                    }

                    //new asset! upload + add it to the product
                    if (hasChanged)
                    {
                        assets.Add(_eaAssetsController.CreateAsset(magentoPath + magentoAsset.file));
                    }
                }
            }
            else if (magentoAssets != null)
            {
                assets.AddRange(magentoAssets.Select(magentoAsset => _eaAssetsController.CreateAsset(magentoPath + magentoAsset.file)));
            }

            return(assets);
        }
コード例 #4
0
        /**
         * This function parses the custom properties in a magento product and creates a matching
         * list of FieldResources for an EA product.
         * If the product has a mapped value, it will check if any of the fields were DELETED and if so
         * set the fields appropriately to have them deleted
         *
         * @param   magentoProduct          Magento product to parse
         *
         * @return  List<FieldResource>     Matching field resources
         */
        public List <FieldResource> ParseFieldsFromProduct(ProductResource magentoProduct)
        {
            var fields = new List <FieldResource>
            {
                CreateField(ConfigReader.GetValueForField(ConfigReader.MagentoNameCode),
                            magentoProduct.name)
            };

            //Add fields from custom attributes
            foreach (var attribute in magentoProduct.custom_attributes)
            {
                FieldResource field = null;

                //Skip colors for now, as that requires a seperate API request after the MP is created
                if (attribute.attribute_code != ConfigReader.MagentoColorCode)
                {
                    switch (
                        _magentoCustomAttributesController.GetCustomAttributeIfExists(attribute.attribute_code)
                        .frontend_input)
                    {
                    case "multiselect":
                        field = CreateFieldFromMultiSelect(magentoProduct, attribute.attribute_code);
                        break;

                    case "text":
                        field = CreateFieldFromText(magentoProduct, attribute.attribute_code);
                        break;

                    case "textarea":
                        field = CreateFieldFromText(magentoProduct, attribute.attribute_code);
                        break;

                    case "select":
                        field = CreateFieldFromSingleSelect(magentoProduct, attribute.attribute_code);
                        break;

                    default:
                        Console.WriteLine(
                            "Custom attribute {0} on product {1} (name) {2} (sku) was not synced as it is of a unsupported type. " +
                            "Only multiselect, textarea and select are supported.",
                            attribute.attribute_code, magentoProduct.name, magentoProduct.sku);
                        break;
                    }
                }

                if (field != null)
                {
                    fields.Add(field);
                }
            }

            //If this is an "Upsert"
            if (ProductHasMapping(magentoProduct))
            {
                var catalogItemId     = GetAttributeByCode(magentoProduct.custom_attributes, ConfigReader.MappingCode).ToString();
                var slug              = _eaCatalogsController.GetCatalogItem(catalogItemId).Slug;
                var productDocumentId = GetProductDocumentIdFromSlug(slug);

                //Go through and ensure all product fields no longer on the product are "empty", this will ensure they are deleted
                var eaFields =
                    _eaProductController.GetProductHierarchy(productDocumentId).RootRevision.FieldValues;

                foreach (var eaField in eaFields)
                {
                    var deleted = true;
                    //If matching field is missing from fields, add it but set it to null

                    foreach (var field in fields)
                    {
                        if (eaField.FieldDefinitionId == field.FieldDefinitionId)
                        {
                            deleted = false;
                        }
                    }

                    if (deleted)
                    {
                        fields.Add(new FieldResource
                        {
                            FieldDefinitionId      = eaField.FieldDefinitionId,
                            LanguageInvariantValue = ""
                        });
                    }
                }
            }

            return(fields);
        }