【发布时间】:2019-01-09 07:53:25
【问题描述】:
我有一个购物车变量作为多维数组,如下所示
$totalCart = {"17":[{"id":"17","name":"New! iPhone 6 64gb GSM Unlocked Smartphone Space Gray, Refurbished","quantity":"1","price":"5000","attributes":[{"Ram Size":"1gb 16gb"},{"Color":"Black"}]},
{"id":"17","name":"New! iPhone 6 64gb GSM Unlocked Smartphone Space Gray, Refurbished","quantity":"1","price":"5000","attributes":[{"Ram Size":" 4gb 64gb"},{"Color":" Gold"}]}]}
只要将商品添加到购物车,它就会检查商品 ID 是否存在于购物车变量中。
1) 如果存在,则检查其子项是否具有与将要添加的产品的属性相同的属性。
a) 如果它具有相同的属性,它应该增加数量或只是添加新价格。
b) 否则它没有相同的属性,它应该添加这个产品作为一个新的孩子,但具有不同的属性
2) 如果它不存在,则应将其添加为带有子的新父产品
我正在开发一个销售具有不同属性的产品的电子商务网站。例如,我有一部 iphone,其属性为 64gb、128gb,同时具有不同颜色的金色、黑色等。现在客户希望通过这种产品但每个变体的数量不同。比如说一个 iphone 64gb,金色,数量为 12,同样的 iphone 128gb,黑色,数量为 5
现在这是我的实现:
public function addToCart(Request $request) {
$productId = $request->productId;
$productName = $request->productName;
$productPrice = $request->productPrice;
$qty = $request->qty;
$productPhoto = $request->productPhoto;
$subTypes = [];
if(isset($request->subtypes)) {
foreach ($request->subtypes as $key => $value) {
$result = $array = explode('_', $value);
$subTypes[] = array($result[0] => $result[1]);
}
}
$cart = session()->get('cart');
// if cart is empty then this the first product
if ($cart == null) {
$cart[$productId][0] = [
"id" => $productId,
"name" => $productName,
"quantity" => $qty,
"price" => $productPrice,
"photo" => $productPhoto,
"attributes" => $subTypes
];
/* $cart = [
$productId[0] => [
"id" => $productId,
"name" => $productName,
"quantity" => $qty,
"price" => $productPrice,
"photo" => $productPhoto,
"attributes" => $subTypes
]
];*/
session()->put('cart', $cart);
return json_encode(['totalCart' => $cart]);
return json_encode(['status' => 'ok','totalCart' => count($cart)]);
}
// if cart not empty then check if this product exist then increment quantity
else if(isset($cart[$productId])) {
foreach ($cart[$productId] as $key => $value2 ){
if ($this->compareArray($value2['attributes'], $subTypes )){
if ((int)$qty > 0) {
$cart[$productId][$key]['quantity'] = $cart[$productId][$key]['quantity'] + (int)$qty;
}
else{
$cart[$productId][$key]['price'] = $cart[$productId][$key]['price'] + (int)$productPrice;
}
}else{
array_push($cart[$productId], [
"id" => $productId,
"name" => $productName,
"quantity" => $qty,
"price" => $productPrice,
"photo" => $productPhoto,
"attributes" => $subTypes
]
);
}
}
session()->put('cart', $cart);
return json_encode(['totalCart' => $cart]);
return json_encode(['status' => 'ok','totalCart' => count($cart)]);
}else {
// if item not exist in cart then add to cart
$cart[$productId][0] = [
"id" => $productId,
"name" => $productName,
"quantity" => $qty,
"price" => $productPrice,
"photo" => $productPhoto,
"attributes" => $subTypes
];
session()->put('cart', $cart);
return json_encode(['totalCart' => $cart]);
return json_encode(['status' => 'ok','totalCart' => count($cart)]);
}
}
公共函数 compareArray ($array1, $array2) { foreach ($array1 as $key => $value) { if ($array2[$key] != $value) { 返回假; } 别的 { 返回真; } } }
但只要满足第一个条件,foreach 循环就会停止。而实际上有一个子产品具有与要添加的产品属性匹配的属性。如何确保检查每个父产品的整个子产品?
【问题讨论】:
-
它会停止,因为您返回 false 或 true。不要返回,只需根据需要增加一个变量并让循环继续。
-
@Chinelo 我更新的帖子解决了你的问题吗?
-
@DavidWinder 谢谢这正是我所做的。它解决了它感谢贡献
标签: php arrays multidimensional-array