/** * Добавление продукта в корзину. * * @return array|\yii\web\Response */ public function actionAddProduct() { $rr = new RequestResponse(); if ($rr->isRequestAjaxPost()) { $product_id = \Yii::$app->request->post('product_id'); $quantity = \Yii::$app->request->post('quantity'); /** * @var ShopProduct $product */ $product = ShopProduct::find()->where(['id' => $product_id])->one(); if (!$product) { $rr->message = \skeeks\cms\shop\Module::t('app', 'This product is not found, it may be removed.'); return (array) $rr; } $shopBasket = ShopBasket::find()->where(['fuser_id' => \Yii::$app->shop->shopFuser->id, 'product_id' => $product_id, 'order_id' => null])->one(); if (!$shopBasket) { $shopBasket = new ShopBasket(['fuser_id' => \Yii::$app->shop->shopFuser->id, 'product_id' => $product->id, 'quantity' => 0]); } $shopBasket->quantity = $shopBasket->quantity + $quantity; if (!$shopBasket->recalculate()->save()) { $rr->success = false; $rr->message = \skeeks\cms\shop\Module::t('app', 'Failed to add item to cart'); } else { $rr->success = true; $rr->message = \skeeks\cms\shop\Module::t('app', 'Item added to cart'); } \Yii::$app->shop->shopFuser->link('site', \Yii::$app->cms->site); $rr->data = \Yii::$app->shop->shopFuser->toArray([], \Yii::$app->shop->shopFuser->extraFields()); return (array) $rr; } else { return $this->goBack(); } }
public function run() { /** * @var $contentElement CmsContentElement */ $contentElement = $this->controller->model; $model = ShopProduct::find()->where(['id' => $contentElement->id])->one(); $productPrices = []; if (!$model) { $model = new ShopProduct(['id' => $contentElement->id]); } else { if ($typePrices = ShopTypePrice::find()->where(['!=', 'def', Cms::BOOL_Y])->all()) { foreach ($typePrices as $typePrice) { $productPrice = ShopProductPrice::find()->where(['product_id' => $model->id, 'type_price_id' => $typePrice->id])->one(); if (!$productPrice) { $productPrice = new ShopProductPrice(['product_id' => $model->id, 'type_price_id' => $typePrice->id]); } if ($post = \Yii::$app->request->post()) { $data = ArrayHelper::getValue($post, 'prices.' . $typePrice->id); $productPrice->load($data, ""); } $productPrices[] = $productPrice; } } } $rr = new RequestResponse(); if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { return $rr->ajaxValidateForm($model); } if ($rr->isRequestPjaxPost()) { /** * @var $productPrice ShopProductPrice */ foreach ($productPrices as $productPrice) { if ($productPrice->save()) { } else { \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Check the correctness of the prices')); } } if ($model->load(\Yii::$app->request->post()) && $model->save()) { \Yii::$app->getSession()->setFlash('success', 'Saved'); if (\Yii::$app->request->post('submit-btn') == 'apply') { } else { return $this->controller->redirect($this->controller->indexUrl); } $model->refresh(); } else { \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Failed to save')); } } $this->viewParams = ['model' => $model, 'productPrices' => $productPrices]; return parent::run(); }
/** * @return \yii\db\ActiveQuery */ public function getShopProduct() { return $this->hasOne(ShopProduct::className(), ['id' => 'shop_product_id']); }
if ($parent->isRoot()) { $path[] = "[" . $parent->site->name . "] " . $parent->name; } else { $path[] = $parent->name; } } } $path = implode(" / ", $path); return "<small><a href='{$model->cmsTree->url}' target='_blank' data-pjax='0'>{$path} / {$model->cmsTree->name}</a></small>"; }, 'format' => 'raw', 'filter' => \skeeks\cms\helpers\TreeOptions::getAllMultiOptions(), 'attribute' => 'tree_id'], ['class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { $result = []; if ($model->cmsContentElementTrees) { foreach ($model->cmsContentElementTrees as $contentElementTree) { $site = $contentElementTree->tree->root->site; $result[] = "<small><a href='{$contentElementTree->tree->url}' target='_blank' data-pjax='0'>[{$site->name}]/.../{$contentElementTree->tree->name}</a></small>"; } } return implode('<br />', $result); }, 'format' => 'raw', 'label' => \Yii::t('app', 'Additional sections')], ['label' => \skeeks\cms\shop\Module::t('app', 'Base price'), 'class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { $shopProduct = \skeeks\cms\shop\models\ShopProduct::getInstanceByContentElement($model); if ($shopProduct) { return \Yii::$app->money->intlFormatter()->format($shopProduct->baseProductPrice->money); } return null; }], ['attribute' => 'active', 'class' => \skeeks\cms\grid\BooleanColumn::className()], ['class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { return \yii\helpers\Html::a('<i class="glyphicon glyphicon-arrow-right"></i>', $model->absoluteUrl, ['target' => '_blank', 'title' => \Yii::t('app', 'Watch to site (opens new window)'), 'data-pjax' => '0', 'class' => 'btn btn-default btn-sm']); }, 'format' => 'raw']]; ?>
$('#sx-shop-product-tradeOffers').hide(); $('input', $('#sx-shop-product-simple')).removeAttr('disabled'); } } $('#{$id}').on("change", function() { initProductType($(this)); }); initProductType($('#{$id}')); JS ) ?> <?php echo $form->fieldSelect($shopProduct, 'product_type', \skeeks\cms\shop\models\ShopProduct::possibleProductTypes()); ?> <? endif; ?> <div id="sx-shop-product-simple"> <?php echo \skeeks\cms\modules\admin\widgets\BlockTitleWidget::widget(['content' => \Yii::t('skeeks/shop/app', 'Main prices')]); ?> <div class="row"> <div class="col-md-3"> <?php echo $form->field($shopProduct, 'purchasing_price')->textInput(); ?> </div>
/** * @return null|ShopProduct */ public function getProduct() { return ShopProduct::findOne(['id' => $this->product_id]); }
protected function _updateShopProduct($product = [], ShopCmsContentElement $cmsContentElement) { $shopProduct = $cmsContentElement->shopProduct; if (!$shopProduct) { $shopProduct = new ShopProduct(); $shopProduct->id = $cmsContentElement->id; $shopProduct->save(); $this->stdout("\tadded ShopProduct\n", Console::FG_GREEN); } else { $this->stdout("\texist ShopProduct\n"); } $shopProduct->baseProductPriceValue = ArrayHelper::getValue($product, 'price'); $shopProduct->baseProductPriceCurrency = "RUB"; $shopProduct->purchasing_price = ArrayHelper::getValue($product, 'purchase_price'); $shopProduct->purchasing_currency = "RUB"; $shopProduct->quantity = (int) ArrayHelper::getValue($product, 'quantity'); if ($shopProduct->save()) { $this->stdout("\tShopProduct saved: quantity={$shopProduct->quantity}; price={$shopProduct->baseProductPriceValue}\n", Console::FG_GREEN); } else { $this->stdout("\tShopProduct not saved\n", Console::FG_RED); } }
/** * @param CmsContent $cmsContent * @return array */ public static function getDefaultColumns($cmsContent = null) { $columns = [['class' => \skeeks\cms\grid\ImageColumn2::className()], 'name', ['class' => \skeeks\cms\grid\UpdatedAtColumn::className()], ['class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { if (!$model->cmsTree) { return null; } $path = []; if ($model->cmsTree->parents) { foreach ($model->cmsTree->parents as $parent) { if ($parent->isRoot()) { $path[] = "[" . $parent->site->name . "] " . $parent->name; } else { $path[] = $parent->name; } } } $path = implode(" / ", $path); return "<small><a href='{$model->cmsTree->url}' target='_blank' data-pjax='0'>{$path} / {$model->cmsTree->name}</a></small>"; }, 'format' => 'raw', 'filter' => \skeeks\cms\helpers\TreeOptions::getAllMultiOptions(), 'attribute' => 'tree_id'], ['class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { $result = []; if ($model->cmsContentElementTrees) { foreach ($model->cmsContentElementTrees as $contentElementTree) { $site = $contentElementTree->tree->root->site; $result[] = "<small><a href='{$contentElementTree->tree->url}' target='_blank' data-pjax='0'>[{$site->name}]/.../{$contentElementTree->tree->name}</a></small>"; } } return implode('<br />', $result); }, 'format' => 'raw', 'label' => \Yii::t('skeeks/shop/app', 'Advanced Topics')], ['attribute' => 'active', 'class' => \skeeks\cms\grid\BooleanColumn::className()], ['label' => \Yii::t('skeeks/shop/app', 'Base price'), 'class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { $shopProduct = \skeeks\cms\shop\models\ShopProduct::getInstanceByContentElement($model); if ($shopProduct && $shopProduct->baseProductPrice) { return \Yii::$app->money->intlFormatter()->format($shopProduct->baseProductPrice->money); } return null; }], ['class' => \yii\grid\DataColumn::className(), 'value' => function (\skeeks\cms\models\CmsContentElement $model) { return \yii\helpers\Html::a('<i class="glyphicon glyphicon-arrow-right"></i>', $model->absoluteUrl, ['target' => '_blank', 'title' => \Yii::t('skeeks/shop/app', 'View online (opens new window)'), 'data-pjax' => '0', 'class' => 'btn btn-default btn-sm']); }, 'format' => 'raw']]; $typeColumn = ['class' => \yii\grid\DataColumn::className(), 'label' => 'Тип товара', 'value' => function (\skeeks\cms\shop\models\ShopCmsContentElement $shopCmsContentElement) { if ($shopCmsContentElement->shopProduct) { return \yii\helpers\ArrayHelper::getValue(\skeeks\cms\shop\models\ShopProduct::possibleProductTypes(), $shopCmsContentElement->shopProduct->product_type); } }]; if ($cmsContent) { /** * @var $shopContent \skeeks\cms\shop\models\ShopContent */ $shopContent = \skeeks\cms\shop\models\ShopContent::findOne(['content_id' => $cmsContent->id]); if ($shopContent) { if ($shopContent->childrenContent) { $columns = \yii\helpers\ArrayHelper::merge([$typeColumn], $columns); } } } return $columns; }