public final class RemoveVariant extends UpdateActionImpl<T>
See also ProductUpdateCommand.
By variant ID (every variant has a variantId):
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 ProductUpdateCommand addVariantCommand =
ProductUpdateCommand.of(product, AddVariant.of(attributeValues, prices, randomKey(), false));
final Product productWithVariant = client().executeBlocking(addVariantCommand);
final ProductVariant variant = productWithVariant.getMasterData().getStaged().getVariants().get(0);
assertThat(productWithVariant.getMasterData().hasStagedChanges()).isFalse();
final Product productWithoutVariant = client().executeBlocking(ProductUpdateCommand.of(productWithVariant, RemoveVariant.ofVariantId(variant.getId())));
assertThat(productWithoutVariant.getMasterData().getStaged().getVariants()).isEmpty();
assertThat(productWithoutVariant.getMasterData().hasStagedChanges()).isTrue();
return productWithoutVariant;
});
See the test code.
By SKU (attention, SKU is optional field in a variant):
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 ProductUpdateCommand addVariantCommand =
ProductUpdateCommand.of(product, AddVariant.of(attributeValues, prices, randomKey(), false));
final Product productWithVariant = client().executeBlocking(addVariantCommand);
final ProductVariant variant = productWithVariant.getMasterData().getStaged().getVariants().get(0);
assertThat(productWithVariant.getMasterData().hasStagedChanges()).isFalse();
final Product productWithoutVariant = client().executeBlocking(ProductUpdateCommand.of(productWithVariant, RemoveVariant.ofSku(variant.getSku())));
assertThat(productWithoutVariant.getMasterData().getStaged().getVariants()).isEmpty();
assertThat(productWithoutVariant.getMasterData().hasStagedChanges()).isTrue();
return productWithoutVariant;
});
See the test code.
AddVariant
Modifier and Type | Method and Description |
---|---|
Integer |
getId() |
String |
getSku() |
Boolean |
isStaged() |
static RemoveVariant |
of(Integer id) |
static RemoveVariant |
of(ProductVariant variant) |
static RemoveVariant |
ofSku(String sku) |
static RemoveVariant |
ofSku(String sku,
Boolean staged) |
static RemoveVariant |
ofVariantId(Integer id) |
static RemoveVariant |
ofVariantId(Integer id,
Boolean staged) |
getAction
public static RemoveVariant of(ProductVariant variant)
public static RemoveVariant of(Integer id)
public static RemoveVariant ofVariantId(Integer id)
public static RemoveVariant ofVariantId(Integer id, @Nullable Boolean staged)
public static RemoveVariant ofSku(String sku)
public static RemoveVariant ofSku(String sku, @Nullable Boolean staged)