Search in sources :

Example 1 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getLowestPriceAmongVariants.

public static JsonNode getLowestPriceAmongVariants(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode first = variants.get(0);
            JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney") : first.path("priceMoney");
            Decimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode var = variants.get(i);
                JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney") : var.path("priceMoney");
                Decimal current = getAmountFromMoneyNode(currentMoneyNode);
                if (current.compare(price) < 0) {
                    price = current;
                    moneyNode = currentMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method isOnSale.

public static boolean isOnSale(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
            JsonNode variants = structuredContent.path("variants");
            int size = variants.size();
            for (int i = 0; i < size; i++) {
                JsonNode variant = variants.get(i);
                if (isTruthy(variant.path("onSale"))) {
                    return true;
                }
            }
            break;
        case DIGITAL:
            return isTruthy(structuredContent.path("onSale"));
        case GIFT_CARD:
        default:
            break;
    }
    return false;
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 3 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getSalePriceMoneyNode.

public static JsonNode getSalePriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            Decimal salePrice = null;
            JsonNode salePriceNode = null;
            for (int i = 0; i < variants.size(); i++) {
                JsonNode variant = variants.path(i);
                JsonNode priceMoney = variant.path("salePriceMoney");
                Decimal price = getAmountFromMoneyNode(priceMoney);
                if (isTruthy(variant.path("onSale")) && (salePriceNode == null || price.compare(salePrice) < 0)) {
                    salePrice = price;
                    salePriceNode = priceMoney;
                }
            }
            return (salePriceNode == null) ? DEFAULT_MONEY_NODE : salePriceNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("salePriceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        case GIFT_CARD:
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getTotalStockRemaining.

public static double getTotalStockRemaining(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    if (EnumSet.of(ProductType.DIGITAL, ProductType.GIFT_CARD).contains(type)) {
        return Double.POSITIVE_INFINITY;
    } else {
        int total = 0;
        JsonNode variants = structuredContent.path("variants");
        for (int i = 0; i < variants.size(); i++) {
            JsonNode variant = variants.get(i);
            if (isTruthy(variant.path("unlimited"))) {
                return Double.POSITIVE_INFINITY;
            } else {
                total += variant.path("qtyInStock").asLong();
            }
        }
        return total;
    }
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method isSoldOut.

public static boolean isSoldOut(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
            JsonNode variants = structuredContent.path("variants");
            int size = variants.size();
            for (int i = 0; i < size; i++) {
                JsonNode variant = variants.get(i);
                if (isTruthy(variant.path("unlimited")) || variant.path("qtyInStock").asInt() > 0) {
                    return false;
                }
            }
            return true;
        case DIGITAL:
        case GIFT_CARD:
            return false;
        default:
            return true;
    }
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)10 ProductType (com.squarespace.template.plugins.platform.enums.ProductType)10 Decimal (com.squarespace.cldrengine.api.Decimal)3 BigDecimal (java.math.BigDecimal)2