pwrsrg/codeigniter4-cart-module

CodeIgniter 4 的 CodeIgniter 3 购物车模块的基本移植。

1.3.0 2024-04-17 15:09 UTC

This package is auto-updated.

Last update: 2024-09-17 16:21:56 UTC


README

这是一个可使用 composer 安装的 CodeIgniter 4 模块,它几乎直接移植了 Codeigniter 3 购物车库类。当然,它已经进行了轻微更新,并与框架的新版本保持一致。

这意味着,除了可以在项目中使用的 外,此库已更新为命名空间,重构以符合 CodeIgniter 风格指南,并构建为使用 CodeIgniter 4 的服务容器,以允许在整个应用程序中共享购物车实例。

更详细的文档可以在 这里 找到。请注意,该文档是为 CodeIgniter 3 库编写的,但库的基本原理和内部工作方式仍然是相同的。最显著的变化是使用方法(请参阅下文),以及要返回购物车中的商品总数,现在调用 $cart->totalItems() 而不是 $this->cart->total_items()

请注意,此包没有测试,因为它只是从现有的 CodeIgniter 库移植过来的,并且工作方式相同。

注意

此分支是为了任何想要能够更新购物车项目名称属性的人而创建的。

安装

  • 通过 composer 安装
composer require pwrsrg/codeigniter4-cart-module
composer update
  • 将其添加到 app/Config/Autoload.php 中的 $psr4 数组中
$psr4 = [
   // OTHER PSR4 ENTRIES
   ...
   
   // CodeIgniter4 Cart Module
   'CodeIgniter4Cart' => ROOTPATH . 'vendor/pwrsrg/codeigniter4-cart-module/src'
];

使用

// Call the cart service
$cart = \Config\Services::cart();

// Insert an array of values
$cart->insert(array(
   'id'      => 'sku_1234ABCD',
   'qty'     => 1,
   'price'   => '19.56',
   'name'    => 'T-Shirt',
   'options' => array('Size' => 'L', 'Color' => 'Red')
));

// Update an array of values
$cart->update(array(
   'rowid'   => '4166b0e7fc8446e81e16883e9a812db8',
   'id'      => 'sku_1234ABCD',
   'qty'     => 3,
   'price'   => '24.89',
   'name'    => 'T-Shirt',
   'options' => array('Size' => 'L', 'Color' => 'Red')
));

// Get the total items. Formerly known as total_items()
$cart->totalItems();

// Remove an item using its `rowid`
$cart->remove('4166b0e7fc8446e81e16883e9a812db8');
  
// Clear the shopping cart
$cart->destroy();

// Get the cart contents as an array
$cart->contents();

许可证

MIT 许可证

版权所有 (c) 2024 Sergio Vazquez II

特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人,免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、转授和/或销售软件副本的权利,并允许向软件提供方提供软件的人行使其权利,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担责任,无论这些责任是因合同、侵权或其他方式引起的,无论这些责任是否与软件或软件的使用或其他交易有关。