public final class ChangeMasterVariant extends UpdateActionImpl<T>
See also ProductUpdateCommand.
By variant ID (every variant has a variantId):
withUpdateableProductProjectionOfMultipleVariants(client(), (ProductProjection productProjection) -> {
//given
final String sizeAttributeName = Sizes.ATTRIBUTE.getName();
final List<EnumValue> allVariantsSizeValues = productProjection.getAllVariants()
.stream()
.map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue())
.collect(Collectors.toList());
assertThat(allVariantsSizeValues)
.as("master variant is of size M")
.containsExactly(Sizes.M, Sizes.S, Sizes.X);
//when
final Integer variantIdOfSizeX = productProjection.getAllVariants().get(2).getId();
final ChangeMasterVariant updateAction =
ChangeMasterVariant.ofVariantId(variantIdOfSizeX);
client().executeBlocking(ProductUpdateCommand.of(productProjection, updateAction));
//then
final ProductProjection productProjectionWithNewMasterVariant =
client().executeBlocking(ProductProjectionByIdGet.of(productProjection, STAGED));
final ProductVariant newMasterVariant = productProjectionWithNewMasterVariant.getMasterVariant();
assertThat(newMasterVariant)
.as("third variant is now the master variant")
.isEqualTo(productProjection.getAllVariants().get(2));
assertThat(newMasterVariant.getAttribute(sizeAttributeName).getValueAsEnumValue())
.isEqualTo(Sizes.X);
final List<EnumValue> reorderedVariantsSizeValues = productProjectionWithNewMasterVariant.getAllVariants()
.stream()
.map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue())
.collect(Collectors.toList());
assertThat(reorderedVariantsSizeValues).containsExactly(Sizes.X, Sizes.S, Sizes.M);
return productProjectionWithNewMasterVariant;
});
See the test code.
By SKU (attention, SKU is optional field in a variant):
withUpdateableProductProjectionOfMultipleVariants(client(), (ProductProjection productProjection) -> {
final int originalMasterVariantId = productProjection.getMasterVariant().getId();
final ProductVariant variantSupposedToBeMaster = productProjection.getAllVariants().get(2);
final String sku = variantSupposedToBeMaster.getSku();
final ChangeMasterVariant updateAction = ChangeMasterVariant.ofSku(sku);
client().executeBlocking(ProductUpdateCommand.of(productProjection, updateAction));
final ProductProjection productProjectionWithNewMasterVariant =
client().executeBlocking(ProductProjectionByIdGet.of(productProjection, STAGED));
assertThat(productProjectionWithNewMasterVariant.getMasterVariant().getSku()).isEqualTo(sku);
assertThat(productProjectionWithNewMasterVariant.getMasterVariant().getId())
.as("variant IDs don't change in reordering")
.isNotEqualTo(originalMasterVariantId)
.isEqualTo(variantSupposedToBeMaster.getId());
return productProjectionWithNewMasterVariant;
});
See the test code.
Modifier and Type | Method and Description |
---|---|
String |
getSku() |
Integer |
getVariantId() |
Boolean |
isStaged() |
static ChangeMasterVariant |
ofSku(String sku) |
static ChangeMasterVariant |
ofSku(String sku,
Boolean staged) |
static ChangeMasterVariant |
ofVariantId(Integer variantId) |
static ChangeMasterVariant |
ofVariantId(Integer variantId,
Boolean staged) |
getAction
public static ChangeMasterVariant ofSku(String sku)
public static ChangeMasterVariant ofSku(String sku, @Nullable Boolean staged)
public static ChangeMasterVariant ofVariantId(Integer variantId)
public static ChangeMasterVariant ofVariantId(Integer variantId, @Nullable Boolean staged)