All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.client.shop.model.CartUpdate Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.client.shop.model;

import io.sphere.client.model.LocalizedString;
import io.sphere.client.model.Money;
import io.sphere.client.model.Reference;
import io.sphere.client.model.ReferenceId;
import io.sphere.internal.command.CartCommands;
import io.sphere.internal.command.Update;
import com.neovisionaries.i18n.CountryCode;

import java.util.List;

/**
 * CartUpdate is object used to update a {@link Cart} in the backend.
 */
public class CartUpdate extends Update {

    /** Adds a product variant in the given quantity to the cart. */
    public CartUpdate addLineItem(int quantity, String productId, int variantId) {
        return addLineItem(quantity, productId, variantId, null);
    }

    /** Adds a product variant in the given quantity to the cart with a reference to a Channel. */
    public CartUpdate addLineItem(int quantity, String productId, int variantId, final String channelId) {
        assertNotNegative(quantity);
        add(new CartCommands.AddLineItem(productId, quantity, variantId, channelId));
        return this;
    }

    /** Adds a product master variant in the given quantity to the cart. */
    public CartUpdate addLineItem(int quantity, String productId) {
        return addLineItem(quantity, productId, null);
    }

    /** Adds a product master variant in the given quantity to the cart with a reference to a Channel. */
    public CartUpdate addLineItem(int quantity, String productId, final String channelId) {
        assertNotNegative(quantity);
        add(new CartCommands.AddLineItemFromMasterVariant(productId, quantity, channelId));
        return this;
    }

    /** Removes the line item from the cart. */
    public CartUpdate removeLineItem(String lineItemId) {
        add(new CartCommands.RemoveLineItem(lineItemId));
        return this;
    }

    /** Adds a custom line item in the given quantity to the cart. */
    public CartUpdate addCustomLineItem(LocalizedString name, Money money, String slug, ReferenceId taxCategory, int quantity) {
        assertNotNegative(quantity);
        add(new CartCommands.AddCustomLineItem(name, money, slug, taxCategory, quantity));
        return this;
    }

    /** Removes the custom line item from the cart. */
    public CartUpdate removeCustomLineItem(String lineItemId) {
        add(new CartCommands.RemoveCustomLineItem(lineItemId));
        return this;
    }

    /** Decreases the quantity of the given line item. If after the update the quantity of the line item is not greater than 0
     * the line item is removed from the cart. */
    public CartUpdate decreaseLineItemQuantity(String lineItemId, int quantity) {
        assertNotNegative(quantity);
        add(new CartCommands.DecreaseLineItemQuantity(lineItemId, quantity));
        return this;
    }

    /** Sets the quantity of the given line item. If quantity is 0, line item is removed from the cart. */
    public CartUpdate setLineItemQuantity(String lineItemId, int quantity) {
        assertNotNegative(quantity);
        add(new CartCommands.ChangeLineItemQuantity(lineItemId, quantity));
        return this;
    }

    /** Sets the customer email in the cart. */
    public CartUpdate setCustomerEmail(String email) {
        add(new CartCommands.SetCustomerEmail(email));
        return this;
    }

    /** Sets the shipping address of the cart. Setting the shipping address also sets the tax rates of the line items
     * and calculates the taxed price. */
    public CartUpdate setShippingAddress(Address address) {
        add(new CartCommands.SetShippingAddress(address));
        return this;
    }

    /** Sets the billing address of the cart. */
    public CartUpdate setBillingAddress(Address address) {
        add(new CartCommands.SetBillingAddress(address));
        return this;
    }

    /** Sets the country of the cart. When the country is set, the line item prices are updated. */
    public CartUpdate setCountry(CountryCode country) {
        add(new CartCommands.SetCountry(country));
        return this;
    }

    /** Sets the shipping method of the cart. When the shipping method is set, 
     * the backend will set the cart shippingInfo and updates the cart total. */
    public CartUpdate setShippingMethod(ReferenceId shippingMethod) {
        add(new CartCommands.SetShippingMethod(shippingMethod));
        return this;
    }

    /** Sets the custom shipping method (not using project shipping methods). When the shipping method is set, 
     * the backend will set the cart shippingInfo and updates the cart total.*/
    public CartUpdate setCustomShippingMethod(String shippingMethodName, ShippingRate shippingRate, ReferenceId taxCategory) {
        add(new CartCommands.SetCustomShippingMethod(shippingMethodName, shippingRate, taxCategory));
        return this;
    }

    /** Sets an existing customer ID. According to the new customer group, the backend will update the cart total. */
    public CartUpdate setCustomerId(String customerId) {
        add(new CartCommands.SetCustomerId(customerId));
        return this;
    }

    /** Updates line item prices and tax rates. */
    public CartUpdate recalculate() {
        add(new CartCommands.RecalculateCartPrices());
        return this;
    }

    private void assertNotNegative(int quantity) {
        if (quantity < 0) throw new IllegalArgumentException("Negative quantity not allowed.");
    }

    @Override
    public List getActions() {
        return super.getActions();
    }

    public CartUpdate addDiscountCode(final String discountCode) {
        add(new CartCommands.AddDiscountCode(discountCode));
        return this;
    }

    public CartUpdate removeDiscountCode(final Reference discountCode) {
        add(new CartCommands.RemoveDiscountCode(discountCode.toUnexpandedReference()));
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy