/** * Action to remove a custom attribute. * @param sku * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttribute ofUnsetAttributeForSku(final String sku, final String name) { return ofUnsetAttributeForSku(sku, name, null); }
/** * Action to remove a custom attribute. * * @param sku * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttribute ofUnsetAttributeForSku(final String sku, final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttributeForSku(sku, NamedAttributeAccess, null); }
/** * Action to remove a custom attribute. * @param sku * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttribute ofUnsetAttributeForSku(final String sku, final String name) { return ofUnsetAttributeForSku(sku, name, null); }
/** * Action to remove a custom attribute. * * @param sku * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttribute ofUnsetAttributeForSku(final String sku, final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttributeForSku(sku, NamedAttributeAccess, null); }
@Test public void setAttributeBySku() throws Exception { withUpdateableProduct(client(), product -> { final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); //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.ofSku(sku, moneyAttribute, newValueForMoney); final SetAttribute localizedEnumUpdate = SetAttribute.ofSku(sku, 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.ofUnsetAttributeForSku(sku, moneyAttribute); final Product productWithoutMoney = client().executeBlocking(ProductUpdateCommand.of(updatedProduct, unsetAction)); assertThat(productWithoutMoney.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).isEmpty(); return productWithoutMoney; }); }