/** * Action to remove a custom attribute. * @param variantId the variant the attribute value should be unset * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttribute ofUnsetAttribute(final Integer variantId, final String name) { return ofUnsetAttribute(variantId, name, null); }
/** * Action to remove a custom attribute. * * @param variantId the variant the attribute value should be unset * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttribute ofUnsetAttribute(final Integer variantId, final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttribute(variantId, NamedAttributeAccess, null); }
/** * Action to remove a custom attribute. * @param variantId the variant the attribute value should be unset * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttribute ofUnsetAttribute(final Integer variantId, final String name) { return ofUnsetAttribute(variantId, name, null); }
/** * Action to remove a custom attribute. * * @param variantId the variant the attribute value should be unset * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttribute ofUnsetAttribute(final Integer variantId, final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttribute(variantId, NamedAttributeAccess, null); }
@Test public void setAttribute() throws Exception { withUpdateableProduct(client(), product -> { //the setter contains the name and a JSON mapper, declare it only one time in your project per attribute //example for MonetaryAmount attribute final String moneyAttributeName = MONEY_ATTRIBUTE_NAME; final NamedAttributeAccess<MonetaryAmount> moneyAttribute = AttributeAccess.ofMoney().ofName(moneyAttributeName); final MonetaryAmount newValueForMoney = EURO_10; //example for LocalizedEnumValue attribute final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue oldValueForColor = Colors.GREEN; final LocalizedEnumValue newValueForColor = Colors.RED; assertThat(product.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).isEmpty(); assertThat(product.getMasterData().getStaged().getMasterVariant().findAttribute(colorAttribute)).contains(oldValueForColor); final SetAttribute moneyUpdate = SetAttribute.of(MASTER_VARIANT_ID, moneyAttribute, newValueForMoney); final SetAttribute localizedEnumUpdate = SetAttribute.of(MASTER_VARIANT_ID, colorAttribute, newValueForColor); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(moneyUpdate, localizedEnumUpdate))); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).contains(newValueForMoney); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().findAttribute(colorAttribute)).contains(newValueForColor); final SetAttribute unsetAction = SetAttribute.ofUnsetAttribute(MASTER_VARIANT_ID, moneyAttribute); final Product productWithoutMoney = client().executeBlocking(ProductUpdateCommand.of(updatedProduct, unsetAction)); assertThat(productWithoutMoney.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).isEmpty(); return productWithoutMoney; }); }