开发者

How would you go to "design" a cart within a Zend Framework project?

I know ZF well, and a little bit of Magento, but I'm new to E-commerce, and I'm sure there are best practice to follow when designing a cart model.

How would go to design a cart开发者_Python百科?

I though of two models, Model_Cart and Model_Cart_Item used in conjonction with Zend_Session to store the cart in session.

What are your feedbacks?

How would you go to do that? What should I know about writing a cart system?

Note that I need a simple system, I even don't need to work with quantity


If your models are easily serialised, you can simply add them to the session data, eg

// storage
$cartNamespace = new Zend_Session_Namespace('cart');
$cartNamespace->cart = $cart; // $cart is a Model_Cart with items

// retrieval
$cartNamespace = new Zend_Session_Namespace('cart');
if (isset($cartNamespace->cart) {
    $cart = $cartNamespace->cart;
}

POPO (Plain Old PHP Objects) are serialised very easily. If your models contain any runtime specific properties (DB connections, file handles, etc), you should implement the Serializable interface to handle any complexity.

You could also consider storing your model data in a database. There's some discussions on SO regarding shopping cart data storage that may help you decide.

  • Storing shopping cart in session


You are on the right track.

I suggest creating a controller action helper class for the assisting with this task. This will make your controller code less bulky. Here is a good tutorial about action helpers.

Give helper class some basic method:

$this->_helper->card->add($product, $qty);
$this->_helper->card->remove($product);
$this->_helper->card->getItems();

Something along these lines.

What should I know about writing a cart system?

This is more for the checkout part... Applies to US. Client has to pay taxes if your business location is same as user's state. E.g. If your business is registered in Florida and client is Florida resident client has to pay taxes, but clients form NY don't.


Create Cart and Cart_Item classes. The Cart class should contain methods for adding and removing Cart_Items which are stored as an array within the Cart class. Also create a save method which serialises the state of the cart into the session, I like to serialise the Cart into a database table as well, using a standard Zend_Db_Table_Abstract model.

I create controller actions which take the product id and quantity and pass them to the Cart class's add/remove methods, persisting the cart afterwards.

I find it useful to implement a singleton pattern for this kind of system, especially if the Cart is updating itself from the database on construct.

As Alex mentioned helpers are useful as well, I like to use a view helper which will retrieve the current cart instance and allow you to easily pull out subtotals and totals ect. in the view.

Also, condsider using the Iterator, Countable and ArrayAccess interfaces so you can loop through your cart to retrieve all your items.

I hope this is some help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜