static function subtotal() { $sum = 0; $qty = static::all(); $goods = Product::all(array_keys($qty)); foreach ($goods as $product) { $sum += $qty[$product->id] * $product->retail; } return $sum; }
Event::listen(VANE_NS . 'cart.cleared', function ($product) { }); /*----------------------------------------------------------------------- | CART BLOCK |----------------------------------------------------------------------*/ Event::preview(VANE_NS . 'cart.from_skus', function (array &$models, &$skus) { // Сonverts user-input 'SKU000 SKU001 ...' list into a hash of 'sku' => qty. if (!is_array($skus)) { $skus = array_filter(preg_split('/\\s+/', trim($skus))); $skus = array_count_values($skus); } }); // Fired to transform $skus (hash of 'sku' => int qty) into Product models with // 'sku' attribute set. Event::listen(VANE_NS . 'cart.from_skus', function (array &$models, &$skus) { $goods = Product::where_in('sku', array_keys($skus))->get(); foreach ($goods as $model) { $model->qty = $skus[$model->sku]; $models[] = $model; } }); /*----------------------------------------------------------------------- | CHECKOUT BLOCK |----------------------------------------------------------------------*/ // Fired to determine if the visitor can perform checkout with his current cart. // If it returns exactly false checking out is prohibited. // //= bool Event::listen(VANE_NS . 'checkout.can', function (Block_Checkout $block) { if ($min = Cart::isTooSmall()) { $block->status('small', array('min' => Str::langNum('general.price', $min), 'total' => Str::langNum('general.price', Cart::subtotal())));