From ba92cf3c57c2c232cc405c909f112797fe8ff24e Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 25 Jun 2020 16:13:21 -0500 Subject: [PATCH] cleanup. bug fix on cart storage save destructor automatic cart storage saving is no longer provided. you must call ->save on your cart to save the changes to it. --- README.md | 53 +++++++++++++++++-- src/cart.php | 69 +------------------------ src/cartItem.php | 35 ------------- src/catalogTotalTypeLoaderInterface.php | 10 ---- 4 files changed, 52 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 0034b11..2ce637e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # shopCart Shopping Cart Core Functionality PHP Library - ## Installing `composer require treehousetim/shopcart` @@ -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 @@ -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 @@ -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 { @@ -69,6 +70,52 @@ interface catalogTotalTypeLoaderInterface } ``` +Here is a sample catalogTotalTypeLoaderInterface implementation for both price and some special case of "points" + +```php +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 diff --git a/src/cart.php b/src/cart.php index 9e1aae2..233ca5c 100644 --- a/src/cart.php +++ b/src/cart.php @@ -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 @@ -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(); @@ -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 ) @@ -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 ); - // } } \ No newline at end of file diff --git a/src/cartItem.php b/src/cartItem.php index 2d17b52..3c004b9 100644 --- a/src/cartItem.php +++ b/src/cartItem.php @@ -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 ) { diff --git a/src/catalogTotalTypeLoaderInterface.php b/src/catalogTotalTypeLoaderInterface.php index fad2972..8781e21 100644 --- a/src/catalogTotalTypeLoaderInterface.php +++ b/src/catalogTotalTypeLoaderInterface.php @@ -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 -// { - -// } \ No newline at end of file