Example #1
0
// present in cart and only its quantity has changed.
Event::listen(VANE_NS . 'cart.added', function (Product $product, &$oldQty) {
});
// Fired when a new item was removed from cart.
Event::listen(VANE_NS . 'cart.removed', function (Product $product, &$oldQty) {
});
// Fired when given $product (ID) is removed from cart or entire cart is cleared (null).
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
|----------------------------------------------------------------------*/