我想建立一个简单的购物,但我现在有一些问题需要建立。我怎样才能将产品添加到我的购物车中这是我到目前为止所拥有的。我也希望一旦用户决定在模态选择中找到产品或者如果决定使用自动完成搜索框(按产品输入后添加产品),我如何使用会话完成此操作?我不想使用购物车库codeigniter。我不能使用任何其他库,框架而不是codeigniter。
sale_Add
public function add_cart(){
$array = $this->products->search();
$cart_products = [];
foreach ($array as $value) {
$cart_products[] = array(
'id' => md5($value->id),
'name' => $value->description,
'qty' => $value->stock,
'price' => $value->sale_price,
);
$this->session->set_userdata('cart_products',$cart_products);
}
}
ajax
$('body').on('click', '.add_product', function(e) {
alert(1)
});
表
div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Price</th>
<th>Qty.</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
info once the cart if added
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:1)
您正在制作购物车的正确途径。 根据您的请求(会话变量上的购物车),这里有一个带注释的示例代码。
您必须查看codeigniter中有关以下内容的文档:
1.Sessions
2.如何获得用户输入
重要
如果您不想使用除CodeIgniter之外的其他框架,您仍需要检查:
最近添加的商品数量不得超过商品库存
您需要验证价格是否等于dataBase中的实际价格(最后一点将取决于您系统的需求)
最后注意
要调试购物车,您可以使用以下代码行
print_r($this->session->userdata('cart_products'));
将此发送到视图或控制器将显示您的购物车是否正确存储在会话变量中。
请注意,这只是CodeIgniter的解决方案。
毕竟我的演讲是代码,希望这可以帮助你:
/*Your function*/
public function add_cart(){
/*We need to get the data from the user either via AJAX or via form submit*/
$md5Id = md5($this->input->post("id"));
$name = $this->input->post("name");
/*We need to make sure the data received is a number*/
if($this->input->post("qty")>=1){
/*To prevent tricky data we need to round numbers to prevent any undesirable stuff here*/
$qty = ceil($qty);
}else{
$qty = 1;
}
/*You need to get the price from the DATABASE, for the excersice I will use a fixed price*/
$price= 1500;
/*Retreive the cart from the session*/
$userCart = $this->session->userdata('cart_products');
/*Validate if the user has a cart*/
if($userCart!=null){
/*We check if the user added the item before*/
$findItem = $this->findItem($userCart, $md5Id);
/*If findItem is not equals 0*/
if($findItem!=0){
/*We declare the variable of the index to remove*/
$cartIndex = $findItem["arrayIndex"];
/*We get the qty of the item*/
$qtyItem = $userCart[$cartIndex]["qty"];
/*We create the new sum of the */
$qty = $qty + $qtyItem;
/*We establish the new qty for that item*/
$userCart[$cartIndex]["qty"] = $qty;
}
/*We need to replace the older cart with the new one*/
$this->session->set_userdata('cart_products', $userCart);
}else{
$userCart = array();
$item = array(
'id' => $md5Id,
'name' => $name,
'qty' => $qty,
'price' => $price,
);
/*In case the user does not have a cart we create one*/
$userCart[] = $item;
$this->session->set_userdata('cart_products', $userCart);
}
}
/*Search item in cart session*/
public function search_cart(){
/*We need to get the data from the user either via AJAX or via form submit*/
$name = $this->input->post("name");
/*Retreive the cart from the session*/
$userCart = $this->session->userdata('cart_products');
/*Validate if the user has a cart*/
if($userCart!=null){
/*We check if the user added the item before*/
$findItems = $this->findByName($userCart, $name);
/*If findItems is not equals 0*/
if($findItems!=0){
/*We return the items found in the cart as per search criteria, this is returned in json in case this needs to be readed via AJAX for example AND we use ECHO in case this thing is the controller*/
echo json_encode($findItems);
}
}
}
/*Function to find the cart item*/
public function findItem($cart, $md5Id){
/*We iterate over the cart*/
foreach($cart as $key=>$item){
/*Check if any id in the cart match your $md5Id*/
if($item["id"]==$md5Id){
/*In case this is found we return an array with the index of the array and qty*/
return array("arrayIndex"=>$key, "qty"=>$item["qty"]);
}
}
/*In case there is no item found in the cart*/
return 0;
}
/*Function to find the cart when user types a search criteria*/
public function findByName($cart, $userInput){
$itemsFound = array();
/*We iterate over the cart*/
foreach($cart as $key=>$item){
/*Check if any id in the cart match via user Input*/
if (strpos($item["name"], $userInput) !== false) {
$itemsFound[] = $item;
}
}
/*If is not empty the items found we return the array of items*/
if(!empty($itemsFound)){
return $itemsFound;
}
/*In case there is no item found in the cart*/
return 0;
}