视图:
代码语言:javascript复制<div id="order-item-template" class="hidden">
<div class="item">
<?= yiibootstrapHtml::activeHiddenInput($model, 'items[product_id][]', ['id' => false, 'class' => 'product_id']); ?>
<?= yiibootstrapHtml::activeHiddenInput($model, 'items[product_price_id][]', ['id' => false, 'class' => 'product_price_id']); ?>
<?= yiibootstrapHtml::activeHiddenInput($model, 'items[qty][]', ['id' => false, 'class' => 'qty']); ?>
</div>
</div>
数组格式:
array ( 'product_id' => array ( 0 => '1', 1 => '16', ), 'product_price_id' => array ( 0 => '2', 1 => '', ), 'qty' => array ( 0 => '1', 1 => '1', ), )
循环:
代码语言:javascript复制public function validateItems()
{
$productIdList = $this->items['product_id'];
$qtyList = $this->items['qty'];
$productPriceIds = $this->items['product_price_id'];
foreach ($productIdList as $i => $product_id)
{
$price = null;
$qty = $qtyList[$i];
$product_price_id = $productPriceIds[$i];
if(isset($product_price_id) && isset($qty) && $qty > 0)
{
$product = Product::findOne($product_id);
if(null != $product && $product->isOnline())
{
if($product->isAreaPrice())
{
if(!isset($item['product_price_id']))
{
$this->addError('items', '编号为'.$i.'的商品地区未选择。');
}
else
{
/** @var ProductPrice $pp */
$pp = ProductPrice::find()->where(['id' => $product_price_id, 'product_id' => $product_id])->one();
if(null != $pp)
{
$price = $pp->price;
}
}
}
else
{
$price = $product->price;
}
$this->products[] = [
'product' => $product,
'qty' => (int)$qty,
'price' => $price
];
}
else
{
$this->addError('items', '编号为'.$i.'的商品信息不正确。');
}
}
else
{
$this->addError('items', '编号为'.$i.'的商品信息不正确。');
}
}
}