public static function availableAddons($id) { $cacheKey = 'Addons4' . $id; $addons = Yii::$app->cache->get($cacheKey); if ($addons === false) { $addons = Addon::findAll(['addon_category_id' => $id]); Yii::$app->cache->set($cacheKey, $addons, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Addon::className()), ActiveRecordHelper::getCommonTag(AddonCategory::className())]])); } return $addons; }
public function getBindedAddons() { $object_id = Object::getForClass($this->className())->id; return $this->hasMany(Addon::className(), ['id' => 'addon_id'])->viaTable('{{%addon_bindings}}', ['object_model_id' => 'id'], function ($query) use($object_id) { /** @var \yii\db\Query $query */ $query->andWhere(['appliance_object_id' => $object_id]); $query->orderBy(['sort_order' => SORT_ASC]); return $query; })->innerJoin('{{%addon_bindings}}', 'addon_id=addon.id AND appliance_object_id=:aoid AND object_model_id=:oid', [':aoid' => $object_id, ':oid' => $this->id])->orderBy(['addon_bindings.sort_order' => SORT_ASC]); }
public function up() { $tableOptions = $this->db->driverName === 'mysql' ? 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=MyISAM' : null; $this->insert('{{%object}}', ['name' => 'Addon', 'object_class' => Addon::className(), 'object_table_name' => Yii::$app->db->schema->getRawTableName(Addon::tableName()), 'column_properties_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_property}}'), 'eav_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_eav}}'), 'categories_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_category}}'), 'link_slug_category' => Yii::$app->db->schema->getRawTableName('{{%addon_category_full_slug}}'), 'link_slug_static_value' => Yii::$app->db->schema->getRawTableName('{{%addon_static_value_category}}'), 'object_slug_attribute' => 'slug']); $this->createTable('{{%addon}}', ['id' => $this->primaryKey(), 'name' => $this->text()->notNull(), 'price' => $this->float()->notNull()->defaultValue(0), 'currency_id' => $this->integer()->notNull()->defaultValue(0), 'price_is_multiplier' => $this->boolean()->notNull()->defaultValue(0), 'is_product_id' => $this->integer()->notNull()->defaultValue(0), 'add_to_order' => $this->boolean()->notNull()->defaultValue(0), 'addon_category_id' => $this->integer()->notNull(), 'can_change_quantity' => $this->boolean()->notNull()->defaultValue(0), 'measure_id' => $this->integer()->notNull()->defaultValue(1)], $tableOptions); $this->createIndex('by_category', '{{%addon}}', ['addon_category_id']); if ($this->db->driverName === 'mysql') { // add fulltext index in mysql for name column :-) $this->execute("ALTER TABLE {{%addon}} ADD FULLTEXT(`name`)"); } $this->createTable('{{%addon_category}}', ['id' => $this->primaryKey(), 'name' => $this->text()->notNull()]); // real bindings of addons to objects $this->createTable('{{%addon_bindings}}', ['id' => $this->primaryKey(), 'addon_id' => $this->integer()->notNull(), 'appliance_object_id' => $this->integer()->notNull(), 'object_model_id' => $this->integer()->notNull(), 'sort_order' => $this->integer()->notNull()->defaultValue(0)], $tableOptions); $this->createIndex('addons4object', '{{%addon_bindings}}', ['appliance_object_id', 'object_model_id']); $this->addColumn('{{%order_item}}', 'addon_id', $this->integer()->notNull()->defaultValue(0)); $tblMenu = '{{%backend_menu}}'; /** @var BackendMenu $shopMenuItem */ $shopMenuItem = BackendMenu::findOne(['name' => 'Shop']); $this->batchInsert($tblMenu, ['parent_id', 'name', 'route', 'icon', 'sort_order', 'added_by_ext', 'rbac_check', 'css_class', 'translation_category'], [[$shopMenuItem->id, 'Addons', 'shop/backend-addons/index', 'cart-plus', 0, 'core', 'product manage', '', 'app']]); \yii\caching\TagDependency::invalidate(Yii::$app->cache, [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(\app\backend\models\BackendMenu::className())]); }
/** * Returns instance of OrderItem entity - product or addon * Used for calculating prices, etc. * @return Product|Addon */ public function sellingItem() { if ($this->addon_id !== 0) { return Addon::findById($this->addon_id); } else { return Product::findById($this->product_id); } }
public function actionAddAddonBinding($remove = '0') { if (Yii::$app->request->isAjax === false) { throw new BadRequestHttpException(); } Yii::$app->response->format = Response::FORMAT_JSON; $addon_id = Yii::$app->request->post('addon_id', null); $object_id = Yii::$app->request->get('object_id', null); $object_model_id = Yii::$app->request->get('object_model_id', null); if ($addon_id === null || $object_id === null || $object_model_id === null) { throw new BadRequestHttpException(); } $addon = Addon::findById($addon_id); $object = Object::findById($object_id); if ($addon === null || $object === null) { throw new NotFoundHttpException(); } $modelClassName = $object->object_class; $model = $this->loadModel($modelClassName, $object_model_id); // ok, now all's ok, addon and model exist! try { if ($remove === '1') { $model->unlink('bindedAddons', $addon, true); } else { $model->link('bindedAddons', $addon, ['sort_order' => count($model->bindedAddons), 'appliance_object_id' => $object->id]); } } catch (\Exception $e) { if (intval($e->getCode()) == 23000) { return ['data' => Html::tag('div', Yii::t('app', 'Addon is already added'), ['class' => 'alert alert-info']) . AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false]; } else { return ['data' => Html::tag('div', $e->getMessage(), ['class' => 'alert alert-danger']), 'error' => $e->getMessage()]; } } TagDependency::invalidate(Yii::$app->cache, [Addon::className()]); return ['data' => AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false]; }
public function getAddon() { return $this->hasOne(Addon::className(), ['id' => 'addon_id']); }
public function rules() { return [['addon_id', 'number', 'integerOnly' => true], ['addon_id', 'exist', 'targetAttribute' => 'id', 'targetClass' => Addon::className()]]; }
/** * @param Order $order * @param array $products * @param $result * @param int $parentId * @return mixed * @throws BadRequestHttpException * @throws NotFoundHttpException */ protected function addProductsToOrder(Order $order, $products = [], $result, $parentId = 0) { $parentId = intval($parentId); if ($parentId !== 0) { // if parent id is set - order item should exist in this order! $parentOrderItem = OrderItem::findOne(['order_id' => $order->id, 'id' => $parentId]); if ($parentOrderItem === null) { throw new BadRequestHttpException(); } } foreach ($products as $product) { $productModel = $addonModel = null; if (!isset($product['id'])) { if (isset($product['addon_id'])) { $addonModel = Addon::findById($product['addon_id']); } } else { $productModel = Product::findById($product['id']); } if ($addonModel === null && $productModel === null) { $result['errors'][] = Yii::t('app', 'Product not found.'); continue; } /** @var Product $productModel */ $quantity = isset($product['quantity']) && (double) $product['quantity'] > 0 ? (double) $product['quantity'] : 1; $condition = ['order_id' => $order->id, 'parent_id' => 0]; if ($productModel !== null) { $condition['product_id'] = $productModel->id; $thisItemModel = $productModel; $quantity = $productModel->measure->ceilQuantity($quantity); } else { $condition['addon_id'] = $addonModel->id; $thisItemModel = $addonModel; if (!$addonModel->can_change_quantity) { $quantity = 1; } } $orderItem = OrderItem::findOne($condition); if ($this->module->allowToAddSameProduct || null === $orderItem) { $orderItem = new OrderItem(); $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'quantity' => $quantity, 'price_per_pcs' => PriceHelper::getProductPrice($thisItemModel, $order, 1, SpecialPriceList::TYPE_CORE)]; if (empty($product['customName']) === false) { $orderItem->custom_name = $product['customName']; } if ($productModel !== null) { $orderItem->product_id = $thisItemModel->id; } else { $orderItem->addon_id = $thisItemModel->id; } } else { /** @var OrderItem $orderItem */ if ($addonModel !== null && !$addonModel->can_change_quantity) { // quantity can not be changed $quantity = 0; } if (null !== $orderItem) { $orderItem->quantity += $quantity; } } if (false === $orderItem->save()) { $result['errors'][] = Yii::t('app', 'Cannot save order item.'); } else { // refresh order Order::clearStaticOrder(); $order = $this->loadOrder(false); } if (null !== $productModel) { $result['products'][] = ['model' => $productModel, 'quantity' => $quantity, 'orderItem' => $orderItem]; } if (isset($product['children']) && is_array($product['children'])) { $result = $this->addProductsToOrder($order, $product['children'], $result, $orderItem->id); } } return $result; }