Change the primary image of a product
This article explains how to change the primary image of a product with one of the other images that are associated with the product.
To do this, you must perform the following:
- Get instance of the following managers:
- Catalog manager
- ContentLinks manager
- Get an instance of the product.
For more information, see For developers: Query products.
- Form the Images collection, get the new primary image of the product.
- Get the images that are ordered before the new primary image.
To keep the ordinals consistent, you must update the ordinals of the images that are positioned before the selected image.
- Update the ordinal of the new primary image.
To assign the product as a primary image, set the Ordinal property of the product image to 0.
NOTE: The primary image of a product must have its Ordinal property set to 0. The other images must have their Ordinal properties set to 1, 2, 3, etc. You can only have one primary image per product.
- Get the content link for the new primary image and update its Ordinal property.
- Update the ordinal property of the images that were positioned before the new primary image.
You must shift each one of these images one position behind, by incrementing their Ordinal property by 1.
- Update the content link of the images that were positioned before the new primary image.
Get the content link of each of these images and increment its Ordinal property by 1.
- Save the changes to both the managers.
Code example: Change the primary image of a product