public final class Recalculate extends UpdateActionImpl<Cart>
See also CartInStoreUpdateCommand.
Update only prices and taxes:
withEmptyCartAndProduct(client(), (emptyCart, product) -> { final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 1L); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(emptyCart, action)); final Price oldPrice = cartWithLineItem.getLineItems().get(0).getPrice(); final PriceDraft priceDraft = PriceDraft.of(oldPrice).withValue(oldPrice.getValue().multiply(2)); final Product productWithChangedPrice = client().executeBlocking(ProductUpdateCommand.of(product, asList(ChangePrice.of(oldPrice, priceDraft), Publish.of()))); final List<Price> prices = productWithChangedPrice.getMasterData().getCurrent().getMasterVariant().getPrices(); assertThat(prices.stream().map(price -> PriceDraft.of(price)).collect(Collectors.toList())) .as("we updated the price of the product") .isEqualTo(asList(priceDraft)); final LineItem lineItemOfTheChangedProduct = client().executeBlocking(CartByIdGet.of(cartWithLineItem)).getLineItems().get(0); assertThat(lineItemOfTheChangedProduct.getPrice()) .as("the new product price is not automatically propagated to the line item in the cart") .isEqualTo(oldPrice).isNotEqualTo(priceDraft); final Cart recalculatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, Recalculate.of())); assertThat(PriceDraft.of(recalculatedCart.getLineItems().get(0).getPrice())) .as("recalculate updated the price of the line item in the cart") .isEqualTo(priceDraft); assertThat(recalculatedCart.getTotalPrice()) .as("recalculate also updated the total price of the cart") .isEqualTo(priceDraft.getValue()).isNotEqualTo(cartWithLineItem.getTotalPrice()); });
See the test code.
Update prices, taxes and product data
withEmptyCartAndProduct(client(), (emptyCart, product) -> { //create cart with line item final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 1L); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(emptyCart, action)); final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue oldColor = cartWithLineItem.getLineItems().get(0).getVariant().findAttribute(colorAttribute).get(); //update the product final LocalizedEnumValue newValueForColor = Colors.RED; final SetAttribute localizedEnumUpdate = SetAttribute.of(MASTER_VARIANT_ID, colorAttribute, newValueForColor); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(localizedEnumUpdate, Publish.of()))); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().findAttribute(colorAttribute)).contains(newValueForColor); //check the line item in the cart, the product data will not be updated final LineItem lineItemOfTheChangedProduct = client().executeBlocking(CartByIdGet.of(cartWithLineItem)).getLineItems().get(0); assertThat(lineItemOfTheChangedProduct.getVariant().findAttribute(colorAttribute)) .as("the new product attribute value is not automatically propagated to the line item in the cart") .contains(oldColor); //use recalculate with updateProductData flag, the product data will be updated final Cart recalculatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, Recalculate.of().withUpdateProductData(Boolean.TRUE))); assertThat(recalculatedCart.getLineItems().get(0).getVariant().findAttribute(colorAttribute)) .contains(newValueForColor); });
See the test code.
Modifier and Type | Method and Description |
---|---|
Boolean |
isUpdateProductData() |
static Recalculate |
of() |
Recalculate |
withUpdateProductData(Boolean updateProductData) |
getAction
public static Recalculate of()
public Recalculate withUpdateProductData(@Nullable Boolean updateProductData)