unodepiera/phalcon_cart

There is no license information available for the latest version (dev-master) of this package.

A shopping cart that allows multiple instances to work with PHP Phalcon

dev-master 2014-04-21 09:02 UTC

This package is not auto-updated.

Last update: 2024-11-19 08:08:11 UTC


README

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.

Installation with Composer

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();

Example Usage Phalcon Cart

First create a new instance

```php $this->cart = new ShoppingCart("myShop"); ```

Insert simple product

```php $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());
}
<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());
	}

Update one product

```php $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());
}
<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 and print options

Check if product has options and print, need his rowId

```php if($this->cart->hasOptions("0e043c0cd48de80fa4f6ed23a15d6d10") != false) { echo "
";
		var_dump($this->cart->getOptions("0e043c0cd48de80fa4f6ed23a15d6d10"));
	}
```

Get total price cart

```php echo $this->cart->getTotal(); ```

Get total items cart

```php echo $this->cart->getTotalItems(); ```

Remove a product

You just need to pass a rowid that there

```php if($this->cart->removeProduct("0e043c0cd48de80fa4f6ed23a15d6d10") != false) { echo "
";
		var_dump($this->cart->getContent());
	}
```

Remove a cart

You just need that there

```php if($this->cart->destroy() != false) { echo "
";
		var_dump($this->cart->getContent());
	}
```

Get cart content

```php var_dump($this->cart->getContent()); ```

Visit me