sinbadxiii/phalcon-cart

购物车Phalcon包

1.3 2021-07-21 05:55 UTC

This package is auto-updated.

Last update: 2024-09-21 13:00:37 UTC


README

为Phalcon提供的简单购物车实现。

安装

通过Composer安装此包。

在终端运行Composer require命令

composer require sinbadxiii/phalcon-cart

如何使用

在服务中添加

$di->set(
      'cart',
      function () use ($di) {
          return new Sinbadxiii\Phalcon\Cart\CartShopping(
              $di->getSession()
          );
      }
  );

或通过名称创建实例

$di->set(
      'compare',
      function () use ($di) {
          return new Sinbadxiii\Phalcon\Cart\CartShopping(
              $di->getSession(), 'compare'
          );
      }
  );
#add()
$this->cart->add('1', 'Product Name 1', 1, 100.99);

#update()
$rowId = '5d12249fdca4cb0fff77f49bbffc128c';
$this->cart->update($rowId, 10);

#remove()
$rowId = '5d12249fdca4cb0fff77f49bbffc128c';
$this->cart->remove($rowId);

#content()
$this->cart->content();

#destroy()
$this->cart->destroy();

#total() with Tax
$this->cart->total();

#total() without Tax
$this->cart->subtotal();

#count()
$this->cart->count();

#countTotal()
$this->cart->countTotal();

实例

此包支持多个购物车实例。其工作方式如下

您可以通过调用 $this->cart->instance('newInstance') 来设置当前购物车实例。从此时起,活动的购物车实例将是 newInstance,因此当您添加、删除或获取购物车内容时,您正在与购物车的新实例一起工作。如果您想切换实例,只需再次调用 $this->cart->instance('otherInstance'),您将再次使用 otherInstance。

所以有一个小例子

$this->cart->instance('shop')->add('100', 'Product #1', 1, 100.00);

// Get the content of the 'shop' cart
$this->cart->content();

$this->cart->instance('wishlist')->add('200', 'Product #2', 1, 20.00);

// Get the content of the 'wishlist' cart
$this->cart->content();

// If you want to get the content of the 'shopping' cart again
$this->cart->instance('shop')->content();

// And the count of the 'wishlist' cart again
$this->cart->instance('wishlist')->count();

注意:请记住,在脚本执行期间,只要您不设置不同的实例,购物车就会保持在最后一个设置的实例。

注意2:默认购物车实例称为 default,因此当您不使用实例时,$this->cart->content(); 与 $this->cart->instance('shop')->content() 相同。