我只需要在我的控制器中获取我的产品属性import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
title
和price
,以便在我的购物车::添加属性中使用它们。
这是我将产品添加到购物车id
currently
这就是我所做的,显然我的代码并没有起作用,这就是我在这里问的原因! :)
public function addingItem($id)
{
$product = Product::where('id', $id)->firstOrFail();
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => 1,
'attributes' => array(),
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
这是我的包默认样本:
public function addingItem($id)
{
$product = Product::where('id', $id)->firstOrFail();
//newly added
foreach($product->suboptions as $subs) {
$title = $subs->title;
$price = $subs->price;
}
$customAttributes = [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
];
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => 1,
'attributes' => array($customAttributes), //and added here
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
正如你所看到的,我尝试获取此代码的属性部分,为此我已经在我的函数中创建了另一个循环(你可以在我的第二个代码中看到它),我会在我的内容中加载属性信息产品细节和thos的值将添加到我的控制器属性数组。
答案 0 :(得分:0)
您正在将数组包装在一个数组中,这是打算吗?
$customAttributes = [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
];
然后:
'attributes' => array($customAttributes), //and added here
此外,这会覆盖每个传递循环的变量$title
和$price
:
foreach($product->suboptions as $subs) {
$title = $subs->title;
$price = $subs->price;
}
然后您尝试在循环外访问$subs
:
$customAttributes = [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
];
编辑:
更改
'attributes' => array($customAttributes), //and added here
到
'attributes' => $customAttributes, //and added here
并在循环中移动$ customAttributes:
foreach($product->suboptions as $subs) {
$customAttributes[] = [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
];
}
请注意
$customAttributes = ...
变为
$customAttributes[] = ...
以便每次都不会覆盖$ customAttributes。