public function getDisplayAttributeAssignmentPairs() { $displayPairs = []; foreach ($this->attributeAssignments as $attributeAssignment) { $productAttribute = ProductAttribute::findOne($attributeAssignment['attribute_id']); $displayPairs[$productAttribute->name] = $attributeAssignment['attribute_option']; } return $displayPairs; }
public function actionAddToCart() { $productId = Yii::$app->request->post('productId', false); $quantity = abs((int) Yii::$app->request->post('quantity', false)); $attributeAssignments = []; foreach (Yii::$app->request->post('attributeAssignments', []) as $attributeAssignment) { $attributeId = ArrayHelper::getValue($attributeAssignment, 'attributeId'); $attributeOption = trim(ArrayHelper::getValue($attributeAssignment, 'attributeOption')); $productAttribute = ProductAttribute::findOne($attributeId); if (!$productAttribute->isValidOption($attributeOption)) { continue; } $attributeAssignments[] = ['attribute_id' => $attributeId, 'attribute_option' => $attributeOption]; } $productInCart = ProductInCart::findOne($productId); $productInCart->attributeAssignments = $attributeAssignments; if ($productInCart) { Yii::$app->cart->put($productInCart, $quantity); return ['status' => self::STATUS_SUCCESS, 'message' => Yii::t('app', 'Add Success.')]; } else { throw new HttpException(Yii::t('app', 'Please try again later.')); } }
/** * Finds the ProductAttribute model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ProductAttribute the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ProductAttribute::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
echo $product->name; ?> </li> <li><span>商品编号:</span><?php echo $product->id; ?> </li> <li><span>上架时间:</span><?php echo Yii::$app->formatter->asDate($product->created_at); ?> </li> <?php foreach ($product->attributeAssignmentsAttributeIdMap as $attributeId => $attributeAssignments) { ?> <?php $productAttribute = ProductAttribute::findOne($attributeId); ?> <?php if (!$productAttribute || $productAttribute->isMultiple) { continue; } ?> <li><span><?php echo $productAttribute->name; ?> :</span><?php echo reset($attributeAssignments)->attribute_option; ?> </li> <?php }