Phalcon cart provides complet cart system, allows you to create multiple instances to have all the areas we want independently.
If ocurred any problem with proccess cart, the class store logs into app/logs/shoppingCart.log for more info.
Cart works with Phalcon sessions, if you would, you can use adapter sessions for manage cart with database, more info.
Create a new file composer.json, open and add the next code
```json { "require": { "unodepiera/phalcon_cart": "dev-master" }, "minimum-stability": "dev" } ```Update your packages with composer update or install with composer install.
Now open app/config/loader.php and replace the code.
```php$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
$config->application->libraryDir
)
);
//register the new class ShoppingCart
$loader->registerClasses(
array(
"ShoppingCart" => "../vendor/unodepiera/phalcon_cart/ShoppingCart.php",
)
);
$loader->register();
<h1>Installation without Composer</h1>
<p>
Download file ShoppingCart.php and create a new directory library in app dir.
Save file into library dir and open app/config/loader.php, now update this file.
</p>
```php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
$config->application->libraryDir//register dir library dir
)
);
$loader->register();
if($this->cart->add($product) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
<h2>Insert multiple products</h2>
```php
$products = array(
array(
"id" => 1,
"name" => "Almendras",
"price" => 2.5,
"qty" => 8,
"description" => "Almendras saladas"
),
array(
"id" => 2,
"name" => "Galletas pou",
"price" => 2.7,
"qty" => 5,
"description" => "Galletas del amigo pou"
),
array(
"id" => 3,
"name" => "Pasta de dientes",
"price" => 1.80,
"qty" => 8,
"description" => "Pasta de dientes......"
)
);
if($this->cart->addMulti($products) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
if($this->cart->update($product) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
<h2>Update multiple products</h2>
```php
$products = array(
array(
"id" => 1,
"name" => "Almendras",
"price" => 2.5,
"qty" => 1,
"description" => "Almendras saladas"
),
array(
"id" => 2,
"name" => "Galletas pou",
"price" => 2.7,
"qty" => 1,
"description" => "Galletas del amigo pou"
),
array(
"id" => 3,
"name" => "Pasta de dientes",
"price" => 1.80,
"qty" => 1,
"description" => "Pasta de dientes......"
)
);
if($this->cart->updateMulti($products) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
Check if product has options and print, need his rowId
```php if($this->cart->hasOptions("0e043c0cd48de80fa4f6ed23a15d6d10") != false) { echo ""; var_dump($this->cart->getOptions("0e043c0cd48de80fa4f6ed23a15d6d10")); } ``` ```php echo $this->cart->getTotal(); ``` ```php echo $this->cart->getTotalItems(); ```You just need to pass a rowid that there
```php if($this->cart->removeProduct("0e043c0cd48de80fa4f6ed23a15d6d10") != false) { echo ""; var_dump($this->cart->getContent()); } ```You just need that there
```php if($this->cart->destroy() != false) { echo ""; var_dump($this->cart->getContent()); } ``` ```php var_dump($this->cart->getContent()); ```