darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.34k stars 438 forks source link

getting Item Conditions #336

Closed n1crack closed 2 years ago

n1crack commented 2 years ago

Hey,

Can we get the conditions that are set on items ? Since the taxes for each product are different, I want to show the total taxes in the summary. I got the global conditions without any problem.

what I am trying to achieve is something like this.

image

I came up a solution like below but I don't think this is a good practice. Is there a better way to do it ?

        return \Cart::getContent()
            ->map(function ($item) {
                return collect($item->conditions)->map(function ($condition) use ($item) {
                    $negative = ((preg_match('/\-/', $condition->getValue()) == 1) ? -1 : 1);
                    $value = $negative * $condition->getCalculatedValue($item->price);
                    $condition->calculatedValue = $value;
                    $condition->name = $condition->getName();

                    return $condition;
                });
            })
            ->flatten(1)->groupBy('name')->map->sum('calculatedValue');