<?php if ($product->productSize) {?>
<select name="product__select" class="product-select__size" id="order-size" name="size">
<?php foreach ($product->productSize as $v) {?>
<?php if ($v->size) {?>
<option value="<?=$v->size->id;?>"><?=$v->size->name;?></option>
<?php }?>
<?php }?>
</select>
<?php }?>
$order_items->size_id = $item['size_id']; // Как здесь получить otion value?
foreach ($items as $id=> $item){
$order_items = new OrderItems();
$order_items->order_id = $order_id;
$order_items->product_id = $id;
$order_items->name = $item['name'];
$order_items->price = $item['price'];
$order_items->qty_item = $item['qty'];
$order_items->size_id = ;
$order_items->sum_item = $item['price'] * $item['qty'];
debug($order_items);
$order_items->save();
}
public function actionView(){
$session = Yii::$app->session;
$session->open();
$this->setMeta('Корзина');
$order = new Order();
if($order->load(Yii::$app->request->post())){
$order->qty = $session['cart.qty'];
$order->sum = $session['cart.sum'];
if($order->save()){
$this->saveOrderItems($session['cart'], $order->id);
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->redirect(['/cart/success', 'id'=>$order->id]);
}
else{
Yii::$app->session->setFlash('Error', 'Ошибка');
}
}
return $this->render('view', compact('session', 'order'));
}
protected function saveOrderItems($items, $order_id){
foreach ($items as $id=> $item){
$order_items = new OrderItems();
$order_items->order_id = $order_id;
$order_items->product_id = $id;
$order_items->name = $item['name'];
$order_items->price = $item['price'];
$order_items->qty_item = $item['qty'];
$order_items->size_id = $item['size_id'];
$order_items->sum_item = $item['price'] * $item['qty'];
debug($order_items);
$order_items->save();
}
}
class ProductSize extends ActiveRecord
{
public static function tableName()
{
return 'product_size';
}
public function rules()
{
return [
[['product_id', 'size_id'], 'integer'],
[['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'id']],
[['size_id'], 'exist', 'skipOnError' => true, 'targetClass' => Size::className(), 'targetAttribute' => ['size_id' => 'id']],
];
}
public function getSize()
{
return $this->hasOne(Size::className(), ['id' => 'size_id']);
}
}
public function actionView($id){
$id = Yii::$app->request->get('id');
$product = Product::find()->where(['id' => $id])->one();
if(empty($product)){
throw new HttpException(404, 'Такого продукта нет');
}
return $this->render('view', compact('product'));
}