danujafernando / cart
这是购物车包
v1.0.3
2020-12-21 19:02 UTC
Requires
- php: >=5.5.9
README
这是一个简单的 Laravel 购物车包
安装
通过 Composer 安装此包。
composer require danujafernando/cart
配置
1. 发布配置文件
php artisan vendor:publish --tag='cart-config'
2. 迁移表
php artisan migrate
使用示例
1. 添加商品到购物车
$product_id = 2; $user_id = 1; // If user_id is null or zero then it takes session value Session::get('_token'); // it will be help for user add to cart items before he log in $quantity = 3; $unit_price = 15.00; $attributes = [ [ 'name' => 'Color', 'value' => 'Red', 'price' => '2.00' ] ]; Cart::add($product_id, $user_id, $quantity, $unit_price, $attributes);
2. 检查属性数组是否有效
$attributes = [ [ 'name' => 'Color', 'value' => 'Blue', 'price' => '3.00' ], [ 'name' => 'Size', 'value' => 'XL', 'price' => '5.00' ] ]; Cart::validateAttribute($attributes); // return value is true. $attributes = [ [ 'name' => 'Color', 'price' => '3.00' ], [ 'name' => 'Size', 'value' => 'XL', 'price' => '5.00' ] ]; Cart::validateAttribute($attributes); // return value is false because the value is missing from first attribute.
重要提示!
默认情况下,属性数组需要名称、值和价格。如果需要更改,可以通过 config/cart.php 文件进行操作。
'product_attributes_keys' => ['name', 'value', 'price']
3. 用户登录后分配购物车项目
// before user log in, Session::get('_token') value store another name /* * $previous_session_id = Session::get('_token'); * Session::put('previous_session_id', $previous_session_id); * This should be run showLoginForm() in your Logincontroller */ // After user logged in /* * $previous_session_id = Session::get('previous_session_id'); * This should be run authenticated() in your Logincontroller */ Cart::assignCartToLoggedUser($user_id, $previous_session_id);
4. 检索购物车项目
$user_id = 3; $carts = Cart::getCart($user_id);
5. 更新购物车项目数量
$cart_id = 3; $quantity = 2; Cart::updateCartQuantity($cart_id, $quantity);
6. 更新购物车项目价格
$cart_id = 3; $unit_price = 29.99; Cart::updateCartUnitPrice($cart_id, $unit_price);
7. 更新购物车项目属性
//you have to pass full attributes array Cart::updateCartAttribute($cart_id, $attributes);
8. 删除整个购物车项目
$user_id = 2; // this function will be delete all items by user_id Cart::deleteCart($user_id);
9. 删除单个购物车项目
$cart_id = 30; Cart::removeCartItem($user_id);
10. 获取购物车总价。
$user_id = 2; get_subtotal($user_id);
许可证
此 Laravel 购物车软件是开源软件,许可协议为 MIT 协议
免责声明
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任是基于合同、侵权或其他原因,以及软件或其使用或其他方式。