[英]Shopping Cart using object oriented PHP
我最近在OO购物车上在线找到了一些代码。 结果显示如下,购物车中已有物品。
购物车内容(2件)
某些小部件:1个,每个售价$ 23.45。
另一个小部件:4个,每个售价$ 12.39。
我的目标是制作类似以下内容的商品,但该商品列表允许用户自己将商品添加/删除到购物车中(如上),但可以使用按钮并允许用户自行更改数量。 谁能帮助我实现该目标? 我还听说过有人在购物车中使用会话? 任何帮助表示赞赏,谢谢!
产品列表
某些小部件:每个$ 23.45。 数量(1) 加入购物车
另一个小部件:每个$ 12.39。 数量(1) 加入购物车
大车
另一个小工具:12.39美元。 数量(4) 移除
ShoppingCart.php
<?php
class ShoppingCart implements Iterator, Countable {
// Array stores the list of items in the cart:
protected $items = array();
// For tracking iterations:
protected $position = 0;
// For storing the IDs, as a convenience:
protected $ids = array();
// Constructor just sets the object up for usage:
function __construct() {
$this->items = array();
$this->ids = array();
}
// Returns a Boolean indicating if the cart is empty:
public function isEmpty() {
return (empty($this->items));
}
// Adds a new item to the cart:
public function addItem(Item $item) {
// Need the item id:
$id = $item->getId();
// Throw an exception if there's no id:
if (!$id) throw new Exception('The cart requires items with unique ID
values.');
// Add or update:
if (isset($this->items[$id])) {
$this->updateItem($item, $this->items[$item]['qty'] + 1);
} else {
$this->items[$id] = array('item' => $item, 'qty' => 1);
$this->ids[] = $id; // Store the id, too!
}
} // End of addItem() method.
// Changes an item already in the cart:
public function updateItem(Item $item, $qty) {
// Need the unique item id:
$id = $item->getId();
// Delete or update accordingly:
if ($qty === 0) {
$this->deleteItem($item);
} elseif ( ($qty > 0) && ($qty != $this->items[$id]['qty'])) {
$this->items[$id]['qty'] = $qty;
}
} // End of updateItem() method.
// Removes an item from the cart:
public function deleteItem(Item $item) {
// Need the unique item id:
$id = $item->getId();
// Remove it:
if (isset($this->items[$id])) {
unset($this->items[$id]);
// Remove the stored id, too:
$index = array_search($id, $this->ids);
unset($this->ids[$index]);
// Recreate that array to prevent holes:
$this->ids = array_values($this->ids);
}
} // End of deleteItem() method.
// Required by Iterator; returns the current value:
public function current() {
// Get the index for the current position:
$index = $this->ids[$this->position];
// Return the item:
return $this->items[$index];
} // End of current() method.
// Required by Iterator; returns the current key:
public function key() {
return $this->position;
}
// Required by Iterator; increments the position:
public function next() {
$this->position++;
}
// Required by Iterator; returns the position to the first spot:
public function rewind() {
$this->position = 0;
}
public function valid() {
return (isset($this->ids[$this->position]));
}
// Required by Countable:
public function count() {
return count($this->items);
}
} // End of ShoppingCart class.
?>
Item.php
<?php
class Item {
// Item attributes are all protected:
protected $id;
protected $name;
protected $price;
// Constructor populates the attributes:
public function __construct($id, $name, $price) {
$this->id = $id;
$this->name = $name;
$this->price = $price;
}
// Method that returns the ID:
public function getId() {
return $this->id;
}
// Method that returns the name:
public function getName() {
return $this->name;
}
// Method that returns the price:
public function getPrice() {
return $this->price;
}
} // End of Item class.
?>
cart.php
<?php
// Create the cart:
try {
require('includes/classes/ShoppingCart.php');
$cart = new ShoppingCart();
// Create some items:
require('includes/classes/Item.php');
$w1 = new Item('W139', 'Some Widget', 23.45);
$w2 = new Item('W384', 'Another Widget', 12.39);
$w3 = new Item('W55', 'Cheap Widget', 5.00);
// Add the items to the cart:
$cart->addItem($w1);
$cart->addItem($w2);
$cart->addItem($w3);
// Update some quantities:
$cart->updateItem($w2, 4);
$cart->updateItem($w1, 1);
// Delete an item:
$cart->deleteItem($w3);
// Show the cart contents:
echo '<h2>Cart Contents (' . count($cart) . ' items)</h2>';
if (!$cart->isEmpty()) {
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the item:
printf('<p><strong>%s</strong>: %d @ $%0.2f each.<p>', $item->getName(),
$arr['qty'], $item->getPrice());
} // End of foreach loop!
} // End of IF.
} catch (Exception $e) {
// Handle the exception.
}
?>
更新我尝试将下面的代码添加到创建的项目下面的“ cart.php”中
<table>
<tr><td><?php echo $w1; ?></td><td><button onclick="$cart-
>addItem($w1);">Add to cart</button></td></tr>
<tr><td><?php echo $w2; ?></td><td><button onclick="$cart-
>addItem($w2);">Add to cart</button></td></tr>
<tr><td><?php echo $w3; ?></td><td><button onclick="$cart-
>addItem($w3);">Add to cart</button></td></tr>
</table>
但是,我得到这个错误...
可捕获的致命错误:无法在第26行的cart.php中将类Item的对象转换为字符串
您的课程做得很好。 现在,您只需要编辑“模板”或cart.php。
如果要打印所有项目,则必须使用“添加到购物车”按钮打印每个项目(在html中,您可以使用“添加到购物车”按钮为每个项目制作一张表格,并传递金额和ID并查看您在cart.php中完成的购物车中的内容
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the item:
printf('<p><strong>%s</strong>: %d @ $%0.2f each.<p>', $item->getName(),
$arr['qty'], $item->getPrice());
} // End of foreach loop!
因此,如果我理解您的问题,则只需要一种方法即可将项目添加到卡中。 我会那样做。
添加:当然,您会收到该错误。 您不能打印这样的对象。 我建议您在Item类(Item.php)中添加一个函数,例如:
public function printItem(){
return $this->name." ".$this->price //Or whatever you want to print.
}
我输入的函数名称是printItem,以避免保留字(只是“ print”可能很危险)
然后,您可以在模板(表格)中替换
echo $w1 by echo $w1->printItem() //Or something like that
我希望这能帮到您。 =)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.