public final class SetAssetCustomField extends SetCustomFieldBase<Product>
See also ProductUpdateCommand.
By variant ID (every variant has a variantId):
withUpdateableType(client(), (Type type) -> {
withProductHavingAssets(client(), product -> {
assertThat(product.getMasterData().hasStagedChanges()).isFalse();
final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant();
final Asset assetWithoutCustomType = masterVariant.getAssets().get(0);
final String assetId = assetWithoutCustomType.getId();
final String firstFieldValue = "commercetools";
final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofType(type)
.addObject(STRING_FIELD_NAME, firstFieldValue)
.build();
final Integer variantId = masterVariant.getId();
final ProductUpdateCommand cmd = ProductUpdateCommand.of(product,
SetAssetCustomType.ofVariantId(variantId, assetId, customFieldsDraft));
final Product updatedProductWithCustomTypeInAssets = client().executeBlocking(cmd);
final String actualFieldValue = updatedProductWithCustomTypeInAssets.getMasterData()
.getStaged().getMasterVariant()
.getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME);
assertThat(actualFieldValue).isEqualTo(firstFieldValue);
final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(updatedProductWithCustomTypeInAssets,
SetAssetCustomField.ofVariantId(variantId, assetId, STRING_FIELD_NAME, "new")));
assertThat(updatedProduct.getMasterData()
.getStaged().getMasterVariant()
.getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME))
.isEqualTo("new");
assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue();
return updatedProduct;
});
return type;
});
See the test code.
By SKU (attention, SKU is optional field in a variant):
withUpdateableType(client(), (Type type) -> {
withProductHavingAssets(client(), product -> {
assertThat(product.getMasterData().hasStagedChanges()).isFalse();
final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant();
final Asset assetWithoutCustomType = masterVariant.getAssets().get(0);
final String assetId = assetWithoutCustomType.getId();
final String firstFieldValue = "commercetools";
final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofType(type)
.addObject(STRING_FIELD_NAME, firstFieldValue)
.build();
final String sku = masterVariant.getSku();
final ProductUpdateCommand cmd = ProductUpdateCommand.of(product,
SetAssetCustomType.ofSku(sku, assetId, customFieldsDraft));
final Product updatedProductWithCustomTypeInAssets = client().executeBlocking(cmd);
final String actualFieldValue = updatedProductWithCustomTypeInAssets.getMasterData()
.getStaged().getMasterVariant()
.getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME);
assertThat(actualFieldValue).isEqualTo(firstFieldValue);
final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(updatedProductWithCustomTypeInAssets,
SetAssetCustomField.ofSku(sku, assetId, STRING_FIELD_NAME, "new")));
assertThat(updatedProduct.getMasterData()
.getStaged().getMasterVariant()
.getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME))
.isEqualTo("new");
assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue();
return updatedProduct;
});
return type;
});
See the test code.
Custom
Modifier and Type | Method and Description |
---|---|
String |
getAssetId() |
String |
getAssetKey() |
String |
getSku() |
Boolean |
getStaged() |
Integer |
getVariantId() |
static SetAssetCustomField |
ofSku(String sku,
String assetId,
String name,
Object value) |
static SetAssetCustomField |
ofSku(String sku,
String assetId,
String name,
Object value,
Boolean staged) |
static SetAssetCustomField |
ofSkuAndAssetKey(String sku,
String assetKey,
String name,
Object value) |
static SetAssetCustomField |
ofSkuAndAssetKey(String sku,
String assetKey,
String name,
Object value,
Boolean staged) |
static SetAssetCustomField |
ofSkuUsingJson(String sku,
String assetId,
String name,
com.fasterxml.jackson.databind.JsonNode value) |
static SetAssetCustomField |
ofSkuUsingJson(String sku,
String assetId,
String name,
com.fasterxml.jackson.databind.JsonNode value,
Boolean staged) |
static SetAssetCustomField |
ofSkuUsingJsonAndAssetKey(String sku,
String assetKey,
String name,
com.fasterxml.jackson.databind.JsonNode value) |
static SetAssetCustomField |
ofSkuUsingJsonAndAssetKey(String sku,
String assetKey,
String name,
com.fasterxml.jackson.databind.JsonNode value,
Boolean staged) |
static SetAssetCustomField |
ofVariantId(Integer variantId,
String assetId,
String name,
Object value) |
static SetAssetCustomField |
ofVariantId(Integer variantId,
String assetId,
String name,
Object value,
Boolean staged) |
static SetAssetCustomField |
ofVariantIdAndAssetKey(Integer variantId,
String assetKey,
String name,
Object value) |
static SetAssetCustomField |
ofVariantIdAndAssetKey(Integer variantId,
String assetKey,
String name,
Object value,
Boolean staged) |
static SetAssetCustomField |
ofVariantIdUsingJson(Integer variantId,
String assetId,
String name,
com.fasterxml.jackson.databind.JsonNode value) |
static SetAssetCustomField |
ofVariantIdUsingJson(Integer variantId,
String assetId,
String name,
com.fasterxml.jackson.databind.JsonNode value,
Boolean staged) |
static SetAssetCustomField |
ofVariantIdUsingJsonAndAssetKey(Integer variantId,
String assetKey,
String name,
com.fasterxml.jackson.databind.JsonNode value) |
static SetAssetCustomField |
ofVariantIdUsingJsonAndAssetKey(Integer variantId,
String assetKey,
String name,
com.fasterxml.jackson.databind.JsonNode value,
Boolean staged) |
getName, getValue
getAction
public String getAssetId()
public String getAssetKey()
public static SetAssetCustomField ofVariantIdUsingJson(Integer variantId, String assetId, String name, com.fasterxml.jackson.databind.JsonNode value)
public static SetAssetCustomField ofVariantIdUsingJson(Integer variantId, String assetId, String name, com.fasterxml.jackson.databind.JsonNode value, @Nullable Boolean staged)
public static SetAssetCustomField ofSkuUsingJson(String sku, String assetId, String name, com.fasterxml.jackson.databind.JsonNode value)
public static SetAssetCustomField ofSkuUsingJson(String sku, String assetId, String name, com.fasterxml.jackson.databind.JsonNode value, @Nullable Boolean staged)
public static SetAssetCustomField ofVariantId(Integer variantId, String assetId, String name, Object value)
public static SetAssetCustomField ofVariantId(Integer variantId, String assetId, String name, Object value, @Nullable Boolean staged)
public static SetAssetCustomField ofSku(String sku, String assetId, String name, Object value)
public static SetAssetCustomField ofSku(String sku, String assetId, String name, Object value, @Nullable Boolean staged)
public static SetAssetCustomField ofVariantIdUsingJsonAndAssetKey(Integer variantId, String assetKey, String name, com.fasterxml.jackson.databind.JsonNode value)
public static SetAssetCustomField ofVariantIdUsingJsonAndAssetKey(Integer variantId, String assetKey, String name, com.fasterxml.jackson.databind.JsonNode value, @Nullable Boolean staged)
public static SetAssetCustomField ofSkuUsingJsonAndAssetKey(String sku, String assetKey, String name, com.fasterxml.jackson.databind.JsonNode value)
public static SetAssetCustomField ofSkuUsingJsonAndAssetKey(String sku, String assetKey, String name, com.fasterxml.jackson.databind.JsonNode value, @Nullable Boolean staged)
public static SetAssetCustomField ofVariantIdAndAssetKey(Integer variantId, String assetKey, String name, Object value)
public static SetAssetCustomField ofVariantIdAndAssetKey(Integer variantId, String assetKey, String name, Object value, @Nullable Boolean staged)
public static SetAssetCustomField ofSkuAndAssetKey(String sku, String assetKey, String name, Object value)
public static SetAssetCustomField ofSkuAndAssetKey(String sku, String assetKey, String name, Object value, @Nullable Boolean staged)