Close

September 24, 2012

Magento C# Adding Product Images

We are full of quick “tips” this month!

We have been doing some automated product system integrations using a third party data feed and a pre-existing Magento install. The C# Sharp calls to Magento’s API work quite well (seemingly a bit slow), but we ran into a brick wall recently. Essentially, while adding images to a product, they would NOT load automatically into the default view of the store. So on the front-end the images would always appear “broken”.

Warning to other developers: always, always, always make sure you apply your images to the exact store view that you want. Magento’s API is very specific about this.

All of the code samples we found we used “default” for the store view, which works great, but its really only using the top store view. If you want the image to be set for EVERY store view, then you have to specify the admin store view of “O”. After that, the image will be applied throughout the entire store.

   /* ... stuff above. */
                var imageEntity = new catalogProductImageFileEntity();
                imageEntity.content = enc;
                imageEntity.mime = "image/jpeg";
                imageEntity.name = "image name";
                imageStream.Close();
                var entityP = new catalogProductAttributeMediaCreateEntity();
                entityP.file = imageEntity;
                entityP.types = new[] { "thumbnail", "small_image", "image" };
                entityP.position = "1";
                entityP.exclude = "0";
                entityP.label = "image name here";
 
  //FROM _MagentoService.catalogProductAttributeMediaCreate(_sessionID, sku, entityP, "default", null);
  _MagentoService.catalogProductAttributeMediaCreate(_sessionID, sku, entityP, "0", null);