public final class AddCustomLineItem extends UpdateActionImpl<Cart> implements CustomDraft
See also CartInStoreUpdateCommand.
Add a simple CustomLineItem
:
withTaxCategory(client(), taxCategory -> {
final Cart cart = createCartWithCountry(client());
assertThat(cart.getCustomLineItems()).isEmpty();
final MonetaryAmount money = MoneyImpl.of("23.50", EUR);
final String slug = "thing-slug";//you handle to identify the custom line item
final LocalizedString name = en("thing");
final long quantity = 5;
final CustomLineItemDraft item = CustomLineItemDraft.of(name, slug, money, taxCategory, quantity, null);
final Cart cartWith5 = client().executeBlocking(CartUpdateCommand.of(cart, AddCustomLineItem.of(item)));
assertThat(cartWith5.getCustomLineItems()).hasSize(1);
final CustomLineItem customLineItem = cartWith5.getCustomLineItems().get(0);
assertThat(customLineItem.getMoney()).isEqualTo(money);
assertThat(customLineItem.getName()).isEqualTo(name);
assertThat(customLineItem.getQuantity()).isEqualTo(quantity);
assertThat(customLineItem.getSlug()).isEqualTo(slug);
final Set<ItemState> state = customLineItem.getState();
assertThat(state).hasSize(1);
assertThat(state).extracting("quantity").containsOnly(quantity);
assertThat(customLineItem.getTaxCategory()).isEqualTo(taxCategory.toReference());
final CartQuery cartQuery = CartQuery.of()
.withPredicates(m -> m.customLineItems().slug().is(customLineItem.getSlug())
.and(m.id().is(cart.getId())));
assertThat(client().executeBlocking(cartQuery).head().get().getId()).isEqualTo(cart.getId());
});
See the test code.
Add a CustomLineItem
with CustomFields
:
withUpdateableType(client(), type -> {
withCartAndTaxedProduct(client(), (cart, product) -> {
final CustomFieldsDraft customFieldsDraft =
CustomFieldsDraftBuilder
.ofType(type)
.addObject(STRING_FIELD_NAME, "a value")
.build();
final AddCustomLineItem updateAction = AddCustomLineItem.of(en("custom line item"), "foo", EURO_30, product.getTaxCategory(), 3L).withCustom(customFieldsDraft);
final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, updateAction));
final CustomFields customFields = updatedCart.getCustomLineItems().get(0).getCustom();
assertThat(customFields.getFieldAsString(STRING_FIELD_NAME)).isEqualTo("a value");
return updatedCart;
});
return type;
});
See the test code.
Cart.getCustomLineItems()
,
RemoveCustomLineItem
getAction
public static AddCustomLineItem of(LocalizedString name, String slug, javax.money.MonetaryAmount money, Referenceable<TaxCategory> taxCategory, long quantity)
public static AddCustomLineItem of(LocalizedString name, String slug, javax.money.MonetaryAmount money, Referenceable<TaxCategory> taxCategory, long quantity, @Nullable CustomFieldsDraft custom)
public static AddCustomLineItem of(LocalizedString name, String slug, javax.money.MonetaryAmount money, ResourceIdentifier<TaxCategory> taxCategory, long quantity, @Nullable CustomFieldsDraft custom)
public static AddCustomLineItem of(CustomLineItemDraft draft)
public LocalizedString getName()
public Long getQuantity()
public javax.money.MonetaryAmount getMoney()
public String getSlug()
@Nullable public ResourceIdentifier<TaxCategory> getTaxCategory()
@Nullable public CustomFieldsDraft getCustom()
getCustom
in interface CustomDraft
@Nullable public ExternalTaxRateDraft getExternalTaxRate()
public AddCustomLineItem withCustom(@Nullable CustomFieldsDraft custom)
public AddCustomLineItem withExternalTaxRate(@Nullable ExternalTaxRateDraft externalTaxRate)