使用 if 语句将重量添加到 laravel 购物车
adding weight to laravel cart with if statement
我试着把我的产品重量放到我的购物车里,我可以拿到,但是有两个问题:
- 重量将添加到我的购物车仅当用户添加一些属性
产品。
It has to be add no matter user select any attribute or not.
- Wight 会添加到我的每个属性中,而它必须添加
分别为
third attribute
.
这是当用户select其他属性
代码
这是我的控制器:
//finding product
$product = Product::findOrFail($id);
$weight = $product->weight; //get wight of product
// get product options in cart
$customAttributes = [];
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
array_push($customAttributes, [
'attr' => [
'name' => $sub->title,
'value' => $sub->price,
'weight' => $weight, // add weight to attributes
]
]);
}
}
}
//adding product and options to cart
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes, // attributes will add to cart in array
));
更新
根据 bipin patel
答案更改我的数据库后,我得到了我想要的东西,但出现了另一个问题。
那是重量会出现在产品属性列表中,我无法将其分开 because I only need weight for shipment calculation
所以 我决定将我的产品 attributes
发送到 conditions
并得到我的体重作为我的 attribute
.
在这种情况下有 2 个好处:
我的产品属性价格会自动加入购物车。 (作为条件)
我可以在任何我需要的地方得到我的体重而不会过度循环。 (作为属性)
So here is my code now
//finding product
$product = Product::findOrFail($id);
$weight = $product->weight;
// get product options in cart
$weightArray = [
'attr' => [
'name' => 'weight',
'value' => $weight,
]
];
$customAttributes = [];
// array_push($customAttributes,$weightArray);
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
array_push($customAttributes, [
'name' => $sub->title,
'value' => $sub->price,
'type' => 'additional',
'target' => 'item',
]);
}
}
}
// dd($customAttributes);
//adding product and options to cart
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $weightArray,
'conditions' => $customAttributes,
));
截图
without choosing attributes
When I choose attributes
Here is dd of error above:
您可以创建权重数组并在开始前推送 customAttributes if(!empty($request->attr))
喜欢:
$weightArray = ['weight'=>$weight];
$customAttributes = [];
array_push($customAttributes,$weightArray); <--- push here
if(!empty($request->attr)){
...... rest of code
更新条件添加
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
'name' => $sub->title,
'type' => 'additional',
'target' => 'item',
'value' => $sub->price,
));
array_push($customAttributes, $itemCondition1);
}
}
}
你为添加条件做这样的事情
我试着把我的产品重量放到我的购物车里,我可以拿到,但是有两个问题:
- 重量将添加到我的购物车仅当用户添加一些属性
产品。
It has to be add no matter user select any attribute or not.
- Wight 会添加到我的每个属性中,而它必须添加
分别为
third attribute
.
这是当用户select其他属性
代码
这是我的控制器:
//finding product
$product = Product::findOrFail($id);
$weight = $product->weight; //get wight of product
// get product options in cart
$customAttributes = [];
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
array_push($customAttributes, [
'attr' => [
'name' => $sub->title,
'value' => $sub->price,
'weight' => $weight, // add weight to attributes
]
]);
}
}
}
//adding product and options to cart
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes, // attributes will add to cart in array
));
更新
根据 bipin patel
答案更改我的数据库后,我得到了我想要的东西,但出现了另一个问题。
那是重量会出现在产品属性列表中,我无法将其分开 because I only need weight for shipment calculation
所以 我决定将我的产品 attributes
发送到 conditions
并得到我的体重作为我的 attribute
.
在这种情况下有 2 个好处:
我的产品属性价格会自动加入购物车。 (作为条件)
我可以在任何我需要的地方得到我的体重而不会过度循环。 (作为属性)
So here is my code now
//finding product
$product = Product::findOrFail($id);
$weight = $product->weight;
// get product options in cart
$weightArray = [
'attr' => [
'name' => 'weight',
'value' => $weight,
]
];
$customAttributes = [];
// array_push($customAttributes,$weightArray);
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
array_push($customAttributes, [
'name' => $sub->title,
'value' => $sub->price,
'type' => 'additional',
'target' => 'item',
]);
}
}
}
// dd($customAttributes);
//adding product and options to cart
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $weightArray,
'conditions' => $customAttributes,
));
截图
without choosing attributes
When I choose attributes
Here is dd of error above:
您可以创建权重数组并在开始前推送 customAttributes if(!empty($request->attr))
喜欢:
$weightArray = ['weight'=>$weight];
$customAttributes = [];
array_push($customAttributes,$weightArray); <--- push here
if(!empty($request->attr)){
...... rest of code
更新条件添加
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
'name' => $sub->title,
'type' => 'additional',
'target' => 'item',
'value' => $sub->price,
));
array_push($customAttributes, $itemCondition1);
}
}
}
你为添加条件做这样的事情