public final class AddAsset extends UpdateActionImpl<T>
See also ProductUpdateCommand.
By variant ID (every variant has a variantId):
withProduct(client(), (Product product) -> {
final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg")
.key("rewe-showcase")
.contentType("image/jpg")
.dimensionsOfWidthAndHeight(1934, 1115)
.build();
final LocalizedString name = LocalizedString.ofEnglish("REWE show case");
final LocalizedString description = LocalizedString.ofEnglish("screenshot of the REWE webshop on a mobile and a notebook");
final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name)
.description(description)
.tags("desktop-sized", "jpg-format", "REWE", "awesome")
.build();
final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, AddAsset.ofVariantId(MASTER_VARIANT_ID, assetDraft)));
final List<Asset> assets = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets();
assertThat(assets).hasSize(1);
final Asset asset = assets.get(0);
assertThat(asset.getId()).isNotEmpty();
assertThat(asset.getDescription()).isEqualTo(description);
assertThat(asset.getName()).isEqualTo(name);
assertThat(asset.getSources()).hasSize(1);
final AssetSource source = asset.getSources().get(0);
assertThat(source.getUri()).isEqualTo("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg");
assertThat(source.getKey()).isEqualTo("rewe-showcase");
assertThat(source.getContentType()).isEqualTo("image/jpg");
assertThat(source.getDimensions()).isEqualTo(AssetDimensions.ofWidthAndHeight(1934, 1115));
});
See the test code.
By SKU (attention, SKU is optional field in a variant):
withProduct(client(), (Product product) -> {
final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg")
.build();
final LocalizedString name = LocalizedString.ofEnglish("REWE show case");
final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name)
.build();
final String sku = product.getMasterData().getStaged().getMasterVariant().getSku();
final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, AddAsset.ofSku(sku, assetDraft).withPosition(0).withStaged(false));
final Product updatedProduct = client().executeBlocking(cmd);
final List<Asset> assets = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets();
assertThat(assets).hasSize(1);
final Asset asset = assets.get(0);
assertThat(asset.getName()).isEqualTo(name);
assertThat(asset.getSources()).hasSize(1);
final AssetSource source = asset.getSources().get(0);
assertThat(source.getUri()).isEqualTo("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg");
});
See the test code.
Modifier and Type | Method and Description |
---|---|
AssetDraft |
getAsset() |
Integer |
getPosition() |
String |
getSku() |
Boolean |
getStaged() |
Integer |
getVariantId() |
Boolean |
isStaged() |
static AddAsset |
ofSku(String sku,
AssetDraft asset) |
static AddAsset |
ofVariantId(Integer variantId,
AssetDraft asset) |
AddAsset |
withPosition(Integer position) |
AddAsset |
withStaged(Boolean staged) |
getAction
public AssetDraft getAsset()
public static AddAsset ofVariantId(Integer variantId, AssetDraft asset)
public static AddAsset ofSku(String sku, AssetDraft asset)