0
def shoppingCartService

def produceShoppingCartInfo(){
    def shoppingItems = shoppingCartService.getItems()
    def summary = new ShoppingCartSummary(items: [:], total: BigDecimal.ZERO)
    shoppingItems.each { shoppingItem ->
        def part = Shoppable.findByShoppingItem(shoppingItem)
        def quantity = new BigDecimal(shoppingCartService.getQuantity(part))
        BigDecimal lineTotal = part.price.multiply(quantity)
        summary.items[part.id] = new Expando(quantity:quantity, item:part, lineTotal:lineTotal)
        summary.total = summary.total.add(lineTotal)
    }
    summary
}

def updateCartQuantities(newQuantities) {
    def summary =  produceShoppingCartInfo()
    newQuantities.each {  
        def partId = it.key
        def newQuantity = it.value
        if(summary.items[partId]) {
            def currentQuantity = summary.items[partId].quantity
            def delta = newQuantity - currentQuantity
            if(delta > 0) {
                shoppingCartService.addToShoppingCart(summary.items[partId].item, delta)
//              shoppingCartService.addToShoppingCart(summary.items[partId].item)
            } else if (delta < 0) {
                shoppingCartService.removeFromShoppingCart(summary.items[partId].item, Math.abs(delta))
//              shoppingCartService.removeFromShoppingCart(summary.items[partId].item)
            }
        }
    }
    produceShoppingCartInfo()

}

def getTotalItemsForCart(){
    def summary =  produceShoppingCartInfo()
    def totalItems = 0

    summary.items.each { partId, itemInfo  ->
        def currentItemQuantity = itemInfo.quantity
        totalItems = totalItems+currentItemQuantity
    }

    totalItems

}
4

1 回答 1

0

这意味着您没有将正确的参数传递给addToShoppingCart. 它期望IShoppableInteger

def addToShoppingCart(IShoppable product, Integer qty = 1, ShoppingCart previousShoppingCart = null)

您的域类实现IShoppable?您是否将域实例作为第一个参数传递?

插件服务的来源在这里

编辑

您的问题可能与变量的类型有关。在这种情况下,最好使用具体类型而不是 Groovy def

def delta = newQuantity - currentQuantity
println delta.class.name //here you will see if this is really a Integer

你可能想要这样的东西:

Integer delta = newQuantity.asInteger() - currentQuantity.asInteger()

def仅当您不需要知道对象的类型时才选择使用。如果你知道这是一个Long, Integer,BigDecimal等等,倾向于用类型声明你的变量。

于 2013-10-25T17:51:47.137 回答