Skip to content

Commit

Permalink
cleanup. bug fix on cart storage save destructor
Browse files Browse the repository at this point in the history
automatic cart storage saving is no longer provided.  you must call ->save on your cart to save the changes to it.
  • Loading branch information
Tim committed Jun 25, 2020
1 parent 32fee90 commit ba92cf3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 115 deletions.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# shopCart
Shopping Cart Core Functionality PHP Library


## Installing

`composer require treehousetim/shopcart`
Expand Down Expand Up @@ -31,7 +30,7 @@ $cart->setTotalTypeLoader( (new \application\cart\myCatalogTotalTypeLoader()))
```

## catalogLoaderInterface
A catalog object must load products into the product catalog. The interface for this is:
A catalog object is used to load products into the product catalog.

```php
interface catalogLoaderInterface
Expand All @@ -46,7 +45,7 @@ interface catalogLoaderInterface

You can use a lot of different storage options for a cart. `treehousetim\shopCart` provides an implementation for session storage.

After creating your implementation of this interface, you would supply it to your cart using `$cart->setStorageHandler( $storageHandler );`
After creating your implementation of this interface, you must supply it to your cart using `$cart->setStorageHandler( $storageHandler );`

```php
interface cartStorageInterface
Expand All @@ -60,6 +59,8 @@ interface cartStorageInterface

## catalogTotalTypeLoaderInterface

You must implement a class that conforms to this interface to support different types of product totals. Typical e-commerce shop carts will probably only need a single catalogTotalType for price, but there are other businesses that need totals of different product properties.

```php
interface catalogTotalTypeLoaderInterface
{
Expand All @@ -69,6 +70,52 @@ interface catalogTotalTypeLoaderInterface
}
```

Here is a sample catalogTotalTypeLoaderInterface implementation for both price and some special case of "points"

```php
<?php namespace application\libraries\cart;

use treehousetim\shopCart\catalogTotalTypeLoaderInterface;
use treehousetim\shopCart\catalogTotalType;

class totalTypeLoader implements catalogTotalTypeLoaderInterface
{
protected $types;

public function __construct()
{
$this->types[] = (new catalogTotalType( catalogTotalType::tPRODUCT_PRICE ) );
$this->types[] = (new catalogTotalType( catalogTotalType::tPRODUCT_FIELD ) )
->setIdentifier( 1 )
->setUnit( 'points' )
->setLabel( 'Point' )
->setProductField( 'productPoints' );
}
}
//------------------------------------------------------------------------
public function nextType() : bool
{
next( $this->types );
if( key( $this->types ) === null )
{
return false;
}

return true;
}
//------------------------------------------------------------------------
public function getType() : catalogTotalType
{
return current( $this->types );
}
//------------------------------------------------------------------------
public function resetType()
{
reset( $this->types );
}
}
```

## totalFormatterInterface

```php
Expand Down
69 changes: 2 additions & 67 deletions src/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ class cart implements totalInterface

public function __construct( catalog $catalog )
{
//$this->metalsCollection = new cartMetalCollection();
$this->catalog = $catalog;
//$this->setFormatter( new productAmountFormatter() );
}
//------------------------------------------------------------------------
public function __destruct()
{
$this->save();
$this->catalog = $catalog;
}
//------------------------------------------------------------------------
public function getCatalog() : catalog
Expand Down Expand Up @@ -210,17 +203,6 @@ public function updateItemQty( $id, $qty )
$this->getItemByProductId( $id )->updateQty( $qty );
}
//------------------------------------------------------------------------
// public function getTotalCategories() : array
// {
// $out = [];
// foreach( $this->items as $item )
// {
// $out = array_merge( $out, $item->getTotalCategories() );
// }

// return $out;
// }
//------------------------------------------------------------------------
public function populate()
{
$model = new productModel();
Expand Down Expand Up @@ -253,9 +235,8 @@ public function getCartData() : array
//------------------------------------------------------------------------
public function getDataByCartDataType( string $type )
{
if(count($this->data) > 0 )
if( count($this->data) > 0 )
{

foreach( $this->data as $cartData )
{
if( $cartData->isValidType( $type ) == false )
Expand All @@ -276,58 +257,12 @@ public function getDataByCartDataType( string $type )
}
else
{
//throw new \Exception("No cart data set yet. ", 1);
return (array) $this->data;
}
}
// //------------------------------------------------------------------------
// public function resetData() : self
// {
// $this->storageHandler->resetCartData();

// $this->data = array();
// return $this;
// }
// //------------------------------------------------------------------------
// public function nextData() : bool
// {
// next( $this->data );
// if( key( $this->data ) === null )
// {
// return false;
// }

// return true;
// }
// //------------------------------------------------------------------------
// public function getData() : cartData
// {
// return current( $this->data );
// }
//------------------------------------------------------------------------
public function getCartItems() : array
{
return $this->items;
}
// //------------------------------------------------------------------------
// public function resetItems()
// {
// reset( $this->items );
// }
//------------------------------------------------------------------------
// public function nextItem() : bool
// {
// next( $this->items );
// if( key( $this->items ) === null )
// {
// return false;
// }

// return true;
// }
// //------------------------------------------------------------------------
// public function getItem() : catalogTotalType
// {
// return current( $this->items );
// }
}
35 changes: 0 additions & 35 deletions src/cartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,6 @@ public function getProduct() : product
{
return $this->product;
}
// //------------------------------------------------------------------------
// public function getTotalWeight()
// {
// return bcmul( $this->qty, $this->product->weight );
// }
// //------------------------------------------------------------------------
// public function getTotalPremium()
// {
// return bcmul( $this->qty, $this->product->premium );
// }
// //------------------------------------------------------------------------
// public function formatWeight()
// {
// return unitFormatAutoScale( $this->getTotalWeight(), $this->product->unit );
// }
// //------------------------------------------------------------------------
// public function formatPremium()
// {
// return moneyFormat( $this->getTotalPremium() );
// }
// //------------------------------------------------------------------------
// public function getTotalCategories() : array
// {
// return $this->product->getTotalCategories();
// }
// //------------------------------------------------------------------------
// public function getTotalFormatted( string $type )
// {
// return $this->formatTotalType( $this->getTotal( $type ), $type, [] );
// }
// //------------------------------------------------------------------------
// public function getTotalTypes() : array
// {
// return $this->product->getTotalTypes();
// }
//------------------------------------------------------------------------
public function formatAmount( string $type )
{
Expand Down
10 changes: 0 additions & 10 deletions src/catalogTotalTypeLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,3 @@ public function nextType() : bool;
public function getType() : catalogTotalType;
public function resetType();
}



// // gold, silver, platinum, palladium (weight)
// // price

// class ownxWeightTotalType extends catalogTotalType
// {

// }

0 comments on commit ba92cf3

Please sign in to comment.