public final class SetAssetCustomType extends SetCustomTypeBase<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
getFields, getType, mapObjectToJsonMap
getAction
public String getAssetId()
public String getAssetKey()
public static SetAssetCustomType ofVariantId(Integer variantId, String assetId, @Nullable CustomFieldsDraft customFieldsDraft)
public static SetAssetCustomType ofVariantId(Integer variantId, String assetId, @Nullable CustomFieldsDraft customFieldsDraft, @Nullable Boolean staged)
public static SetAssetCustomType ofSku(String sku, String assetId, @Nullable CustomFieldsDraft customFieldsDraft)
public static SetAssetCustomType ofSku(String sku, String assetId, @Nullable CustomFieldsDraft customFieldsDraft, @Nullable Boolean staged)
public static SetAssetCustomType ofVariantIdAndAssetKey(Integer variantId, String assetKey, @Nullable CustomFieldsDraft customFieldsDraft)
public static SetAssetCustomType ofVariantIdAndAssetKey(Integer variantId, String assetKey, @Nullable CustomFieldsDraft customFieldsDraft, @Nullable Boolean staged)
public static SetAssetCustomType ofSkuAndAssetKey(String sku, String assetKey, @Nullable CustomFieldsDraft customFieldsDraft)
public static SetAssetCustomType ofSkuAndAssetKey(String sku, String assetKey, @Nullable CustomFieldsDraft customFieldsDraft, @Nullable Boolean staged)