Complet cart system for Phalcon
Phalcon cart provides complet cart system, allows you to create multiple instances to have all the areas we want independently., (*1)
If ocurred any problem with proccess cart, the class store logs into app/logs/shoppingCart.log for more info., (*2)
Cart works with Phalcon sessions, if you would, you can use adapter sessions for manage cart with database, more info.
, (*3)
Installation with Composer
Create a new file composer.json, open and add the next code, (*4)
{
"require": {
"unodepiera/phalcon_cart": "dev-master"
},
"minimum-stability": "dev"
}
Update your packages with composer update or install with composer install., (*5)
Now open app/config/loader.php and replace the code., (*6)
$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();
Installation without Composer
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.
, (*7)
$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();
, (*8)
Example Usage Phalcon Cart
First create a new instance
$this->cart = new ShoppingCart("myShop");
, (*9)
Insert simple product
$product = array(
"id" => 3,
"name" => "Pasta de dientes",
"price" => 1.80,
"qty" => 2,
"description" => "Pasta de dientes......"
);
if($this->cart->add($product) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
, (*10)
Insert multiple products
$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());
}
, (*11)
Update one product
$product = array(
"id" => 3,
"name" => "Pasta de dientes",
"price" => 1.80,
"qty" => 12,
"description" => "Pasta de dientes......"
);
if($this->cart->update($product) != false)
{
echo "<pre>";
var_dump($this->cart->getContent());
}
, (*12)
Update multiple products
$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());
}
, (*13)
Check and print options
Check if product has options and print, need his rowId, (*14)
if($this->cart->hasOptions("0e043c0cd48de80fa4f6ed23a15d6d10") != false)
{
echo "
<
pre>";
var_dump($this->cart->getOptions("0e043c0cd48de80fa4f6ed23a15d6d10"));
}
, (*15)
Get total price cart
echo $this->cart->getTotal();
, (*16)
Get total items cart
echo $this->cart->getTotalItems();
, (*17)
Remove a product
You just need to pass a rowid that there, (*18)
if($this->cart->removeProduct("0e043c0cd48de80fa4f6ed23a15d6d10") != false)
{
echo "
<
pre>";
var_dump($this->cart->getContent());
}
, (*19)
Remove a cart
You just need that there, (*20)
if($this->cart->destroy() != false)
{
echo "
<
pre>";
var_dump($this->cart->getContent());
}
, (*21)
Get cart content
var_dump($this->cart->getContent());
Visit me