public final class AddVariant extends UpdateActionImpl<T>
See also ProductUpdateCommand.
final NamedAttributeAccess<MonetaryAmount> moneyAttribute =
AttributeAccess.ofMoney().ofName(MONEY_ATTRIBUTE_NAME);
final AttributeDraft moneyAttributeValue = AttributeDraft.of(moneyAttribute, EURO_10);
final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE;
final LocalizedEnumValue color = Colors.RED;
final AttributeDraft colorAttributeValue = AttributeDraft.of(colorAttribute, color);
final NamedAttributeAccess<EnumValue> sizeAttribute = Sizes.ATTRIBUTE;
final AttributeDraft sizeValue = AttributeDraft.of(sizeAttribute, Sizes.M);
withUpdateableProduct(client(), product -> {
assertThat(product.getMasterData().getStaged().getVariants()).isEmpty();
assertThat(product.getMasterData().hasStagedChanges()).isFalse();
final PriceDraft price = PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE);
final List<PriceDraft> prices = asList(price);
final List<AttributeDraft> attributeValues = asList(moneyAttributeValue, colorAttributeValue, sizeValue);
final String sku = randomKey();
final String key = randomKey();
final Image image = Image.of("url", ImageDimensions.of(3, 5));
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();
product = client().executeBlocking(ProductUpdateCommand.of(product, AddAsset.ofSku(product.getMasterData().getStaged().getMasterVariant().getSku(), assetDraft)));
final List<Asset> assets = product.getMasterData().getStaged().getMasterVariant().getAssets();
assertThat(assets).hasSize(1);
final AddVariant updateAction = AddVariant.of(attributeValues, prices, sku)
.withKey(key)
.withImages(singletonList(image))
.withAssets(assets);
final ProductUpdateCommand addVariantCommand =
ProductUpdateCommand.of(product, updateAction);
final Product productWithVariant = client().executeBlocking(addVariantCommand);
final ProductVariant variant = productWithVariant.getMasterData().getStaged().getVariants().get(0);
assertThat(productWithVariant.getMasterData().hasStagedChanges()).isTrue();
assertThat(variant.getId()).isEqualTo(2);
assertThat(variant.findAttribute(moneyAttribute).get()).isEqualTo(EURO_10);
assertThat(variant.findAttribute(colorAttribute).get()).isEqualTo(color);
assertThat(variant.findAttribute(sizeAttribute).get()).isEqualTo(Sizes.M);
assertThat(variant.getSku()).isEqualTo(sku);
assertThat(variant.getKey()).isEqualTo(key);
assertThat(variant.getImages()).containsExactly(image);
assertThat(variant.getAssets().get(0).getKey()).isEqualTo(assets.get(0).getKey());
final Product productWithoutVariant = client().executeBlocking(ProductUpdateCommand.of(productWithVariant, RemoveVariant.of(variant)));
assertThat(productWithoutVariant.getMasterData().getStaged().getVariants()).isEmpty();
final List<AssetDraft> assetDrafts = product.getMasterData().getStaged().getMasterVariant().getAssets().stream().map(a -> AssetDraftBuilder.of(a).build()).collect(toList());
assertThat(assets).hasSize(1);
final AddVariant updateAction2 = AddVariant.of(attributeValues, prices, sku)
.withKey(key)
.withImages(singletonList(image))
.withAssetDrafts(assetDrafts);
final ProductUpdateCommand addVariantCommand2 =
ProductUpdateCommand.of(productWithoutVariant, updateAction2);
final Product productWithVariant2 = client().executeBlocking(addVariantCommand2);
final ProductVariant variant2 = productWithVariant2.getMasterData().getStaged().getVariants().get(0);
assertThat(productWithVariant2.getMasterData().hasStagedChanges()).isTrue();
assertThat(variant2.getId()).isEqualTo(3);
assertThat(variant2.findAttribute(moneyAttribute).get()).isEqualTo(EURO_10);
assertThat(variant2.findAttribute(colorAttribute).get()).isEqualTo(color);
assertThat(variant2.findAttribute(sizeAttribute).get()).isEqualTo(Sizes.M);
assertThat(variant2.getSku()).isEqualTo(sku);
assertThat(variant2.getKey()).isEqualTo(key);
assertThat(variant2.getImages()).containsExactly(image);
assertThat(variant2.getAssets().get(0).getKey()).isEqualTo(assets.get(0).getKey());
return productWithVariant2;
});
See the test code.
Modifier and Type | Method and Description |
---|---|
List<Asset> |
getAssets()
Deprecated.
|
List<AssetDraft> |
getAssetsDrafts() |
List<AttributeDraft> |
getAttributes() |
List<Image> |
getImages() |
String |
getKey() |
List<PriceDraft> |
getPrices() |
String |
getSku() |
Boolean |
isStaged() |
static AddVariant |
of(List<AttributeDraft> attributes,
List<PriceDraft> prices) |
static AddVariant |
of(List<AttributeDraft> attributes,
List<PriceDraft> prices,
Boolean staged) |
static AddVariant |
of(List<AttributeDraft> attributes,
List<PriceDraft> prices,
String sku) |
static AddVariant |
of(List<AttributeDraft> attributes,
List<PriceDraft> prices,
String sku,
Boolean staged) |
AddVariant |
withAssetDrafts(List<AssetDraft> assets) |
AddVariant |
withAssets(List<Asset> assets) |
AddVariant |
withImages(List<Image> images) |
AddVariant |
withKey(String key) |
AddVariant |
withSku(String sku) |
getAction
public List<AttributeDraft> getAttributes()
public List<PriceDraft> getPrices()
@Nullable @Deprecated public List<Asset> getAssets()
public List<AssetDraft> getAssetsDrafts()
public AddVariant withSku(@Nullable String sku)
public AddVariant withKey(@Nullable String key)
public AddVariant withImages(List<Image> images)
public AddVariant withAssets(List<Asset> assets)
public AddVariant withAssetDrafts(List<AssetDraft> assets)
public static AddVariant of(List<AttributeDraft> attributes, List<PriceDraft> prices, @Nullable String sku)
public static AddVariant of(List<AttributeDraft> attributes, List<PriceDraft> prices, @Nullable String sku, @Nullable Boolean staged)
public static AddVariant of(List<AttributeDraft> attributes, List<PriceDraft> prices)
public static AddVariant of(List<AttributeDraft> attributes, List<PriceDraft> prices, @Nullable Boolean staged)