Saves product to database if the corresponding property is true.

@param integer $productId
@param integer $count
@param array|null $attributesAndValues
@param array|null $additionalProducts
@param null $combinationId
@throws Exception
544531
private function saveProductToDataBase($productId, $count, $attributesAndValues = null, $additionalProducts = null, $combinationId = null)
    {
        $order = $this->getIncompleteOrderFromDB();

        if (\Yii::$app->getModule('shop')->enableCombinations) {
            if (!empty($attributesAndValues)) {
                $combination = $this->getCombination($attributesAndValues, $productId);
                if (!empty($combination)) {
                    $orderProduct = $this->getOrderProduct($order->id, $productId, $combination->id);
                }
                else {
                    throw new Exception(\Yii::t('cart', 'Such attributes combination does not exist'));
                }
            }
            else if(!empty($combinationId)) {
                $orderProduct = $this->getOrderProduct($order->id, $productId, $combinationId);
            }
            else {
                $orderProduct = $this->getOrderProduct($order->id, $productId, null);
            }
        } else {
            $orderProduct = new OrderProduct();
            $orderProduct->product_id = $productId;
            $orderProduct->order_id = $order->id;
        }

        $orderProduct->count += $count;
        if ($orderProduct->validate()) {
            $orderProduct->save();

            if (!empty($additionalProducts)) {
                foreach ($additionalProducts as $additionalProduct) {
                    $orderProductAdditionalProduct = OrderProductAdditionalProduct::find()
                        ->where(['order_product_id' => $orderProduct->id, 'additional_product_id' => $additionalProduct['productId']])
                        ->one();
                    if (empty($orderProductAdditionalProduct)) {
                        $orderProductAdditionalProduct = new OrderProductAdditionalProduct();
                        $orderProductAdditionalProduct->order_product_id = $orderProduct->id;
                        $orderProductAdditionalProduct->additional_product_id = $additionalProduct['productId'];
                        $orderProductAdditionalProduct->number = $additionalProduct['number'];
                        if ($orderProductAdditionalProduct->validate()) $orderProductAdditionalProduct->save();
                    }
                    else {
                        $orderProductAdditionalProduct->number += $additionalProduct['number'];
                        if ($orderProductAdditionalProduct->validate()) $orderProductAdditionalProduct->save();

                    }
                }
            }
        } else die(var_dump($orderProduct->errors));
    }
195355
private function logEvent($productCollection, int $storeId): void
    {
        $sessionData = $this->getSessionData();

        if (!empty($sessionData)) {
            $pageData = [
                'store_id'  => $storeId,
                'search'    => [
                    'query'             => $this->helper->getEscapedQueryText(),
                    'is_spellchecked'   => (int) $productCollection->isSpellchecked(),
                ],
                'product_list' => [
                    'page_count'    => 1,
                    'current_page'  => 1,
                    'product_count' => 1,
                ],
                'type' => $this->getPageTypeInformations(),
            ];

            $eventData = ['page' => $pageData, 'session' => $sessionData];

            $this->service->addEvent($eventData);
        }
    }
599857
@Override
	public int countByCommerceCountryId(long commerceCountryId) {
		FinderPath finderPath = FINDER_PATH_COUNT_BY_COMMERCECOUNTRYID;

		Object[] finderArgs = new Object[] { commerceCountryId };

		Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_COMMERCEREGION_WHERE);

			query.append(_FINDER_COLUMN_COMMERCECOUNTRYID_COMMERCECOUNTRYID_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(commerceCountryId);

				count = (Long)q.uniqueResult();

				finderCache.putResult(finderPath, finderArgs, count);
			}
			catch (Exception e) {
				finderCache.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}
82184
public function getByProductIds(array $productIds, $storeId)
    {
        $searchQueryTable = $this->getTable('search_query');
        $select = $this->getBaseSelect()
            ->joinInner($searchQueryTable, "main_table.query_id = {$searchQueryTable}.query_id", [])
            ->where('product_id IN(?)', $productIds)
            ->where('store_id = ?', $storeId)
            ->columns(['product_id', 'query_id', 'position', 'is_blacklisted']);

        return $this->getConnection()->fetchAll($select);
    }
946076
public function attributeAdd($attributeId, $attributeSetId, $attributeGroupId = null, $sortOrder = null)
    {
        return $this->__createAction('product_attribute_set.attributeAdd', func_get_args());
    }
674721
protected function persistLeadProduct(DataSetInterface $dataSet, int $productConcreteId): void
    {
        if (!$dataSet[ProductPackagingUnitDataSetInterface::COLUMN_IS_LEAD_PRODUCT]) {
            return;
        }

        $productPackagingLeadProductEntity = $this->getProductPackagingLeadProductQuery()
            ->filterByFkProductAbstract($this->getIdProductAbstractByProductSku($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_CONCRETE_SKU]))
            ->findOneOrCreate();

        $productPackagingLeadProductEntity
            ->setFkProduct($productConcreteId)
            ->save();
    }
1016845
protected function incrementOrDecrementAttributeValue($column, $amount, $extra, $method)
    {
        $this->{$column} = $this->{$column} + ($method === 'increment' ? $amount : $amount * -1);

        $this->forceFill($extra);

        $this->syncOriginalAttribute($column);
    }
750486
public function removeItem(int $productId, int $combinationId = null)
    {
        if (!\Yii::$app->user->isGuest) {
            $order = Order::find()->where([
                'user_id' => \Yii::$app->user->id,
                'status' => OrderStatus::STATUS_INCOMPLETE
            ])->one();
            if (!empty($order)) {
                $orderProduct = OrderProduct::find()->where([
                    'product_id' => $productId,
                    'combination_id' => $combinationId,
                    'order_id' => $order->id
                ])->one();
                if (!empty($orderProduct)) {
                    $orderProduct->delete();
                }
            }
        } else {
            $session = Yii::$app->session;
            if ($session->has(self::SESSION_KEY)) {
                $products = $session[self::SESSION_KEY];
                foreach ($products as $key => $product) {
                    if ($product['id'] == $productId && $product['combinationId'] == $combinationId)
                        unset($_SESSION[self::SESSION_KEY][$key]);
                }
            }
        }
        $this->trigger(self::EVENT_AFTER_REMOVE_PRODUCT);
    }
484155
public function create($data, $productId = null)
    {
        if (isset($productId)) {
            return $this->client()->put($data, 'products/{productId}', ['productId' => $productId]);
        }

        return $this->client()->post($data, 'products');
    }
269463
public function add($related_product_id, $product_id)
    {
        $result = null;
        $this->hook->attach('product.related.add.before', $related_product_id, $product_id, $result, $this);

        if (isset($result)) {
            return (bool) $result;
        }

        $this->db->insert('product_related', array('product_id' => $product_id, 'item_product_id' => $related_product_id));
        $this->db->insert('product_related', array('product_id' => $related_product_id, 'item_product_id' => $product_id));

        $this->hook->attach('product.related.add.after', $related_product_id, $product_id, $result, $this);
        return (bool) $result;
    }
1057559
protected function persistAmount(DataSetInterface $dataSet, SpyProductPackagingUnit $productPackagingUnitEntity): void
    {
        $productPackagingUnitAmountEntity = $productPackagingUnitEntity->getSpyProductPackagingUnitAmounts()->getFirst();

        if ($productPackagingUnitAmountEntity === null) {
            $productPackagingUnitAmountEntity = new SpyProductPackagingUnitAmount();
            $productPackagingUnitAmountEntity
                ->setFkProductPackagingUnit($productPackagingUnitEntity->getIdProductPackagingUnit());
        }

        $productPackagingUnitAmountEntity
            ->setIsVariable($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_IS_VARIABLE])
            ->setDefaultAmount($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_DEFAULT_AMOUNT])
            ->setAmountMin($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_AMOUNT_MIN])
            ->setAmountMax($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_AMOUNT_MAX])
            ->setAmountInterval($dataSet[ProductPackagingUnitDataSetInterface::COLUMN_AMOUNT_INTERVAL])
            ->save();
    }
1026311
public function createVariantAttributeRelation($productId, array $requestData)
    {
        $variant = $this->retrieveProductById($productId);
        $attribute = $this->retrieveAttributeById($this->getProperty($requestData, 'attributeId'));

        // Only add if relation does not already exists.
        if (!$variant->getVariantAttributes()->contains($attribute)) {
            $variant->addVariantAttribute($attribute);
        }

        return $variant;
    }
327731
public function insert($productId, $skuId, Licensing_LicenseAssignmentInsert $postBody, $optParams = array())
  {
    $params = array('productId' => $productId, 'skuId' => $skuId, 'postBody' => $postBody);
    $params = array_merge($params, $optParams);
    return $this->call('insert', array($params), "Licensing_LicenseAssignment");
  }
333042
public function delete($product_id, array $options = array())
    {
        $result = null;
        $this->hook->attach('sku.delete.before', $product_id, $options, $result, $this);

        if (isset($result)) {
            return (bool) $result;
        }

        $sql = 'DELETE FROM product_sku WHERE product_id=?';

        if (!empty($options['combinations'])) {
            $sql .= ' AND LENGTH(combination_id) > 0';
        }

        if (!empty($options['base'])) {
            $sql .= ' AND LENGTH(combination_id) = 0';
        }

        $result = (bool) $this->db->run($sql, array($product_id))->rowCount();
        $this->hook->attach('sku.delete.after', $product_id, $options, $result, $this);
        return (bool) $result;
    }
168285
public function unapprove($enterpriseId, $productId, $optParams = array())
  {
    $params = array('enterpriseId' => $enterpriseId, 'productId' => $productId);
    $params = array_merge($params, $optParams);
    return $this->call('unapprove', array($params));
  }
105239
private function processConfigAttributes($configAttributes, $attributesFromAttributeSet, $productModel, $attributes)
    {
        foreach ($configAttributes as $attributeCode) {
            //if config attribute is in attribute set
            if (in_array($attributeCode, $attributesFromAttributeSet)) {
                //attribute input type
                $inputType = $this->productResource
                    ->getAttribute($attributeCode)
                    ->getFrontend()
                    ->getInputType();

                //fetch attribute value from product depending on input type
                switch ($inputType) {
                    case 'multiselect':
                    case 'select':
                    case 'dropdown':
                        $value = $productModel->getAttributeText($attributeCode);
                        break;
                    case 'date':
                        $value = $productModel->getData($attributeCode);
                        break;
                    default:
                        $value = $productModel->getData($attributeCode);
                        break;
                }

                $attributes = $this->processAttributeValue($attributes, $value, $attributeCode);
            }
        }
        return $attributes;
    }
1009496
public function actionAddCombination(int $productId, int $languageId)
    {
        $combination = new Combination();
        $combinationTranslation = new CombinationTranslation();
        $combinationsList = Combination::find()
            ->with([
                'images',
                'prices',
                'combinationAttributes.productAttribute',
                'combinationAttributes.productAttribute.translation',
                'combinationAttributes.productAttributeValue',
                'combinationAttributes.productAttributeValue.translation',
                'combinationAvailability',
                'combinationAvailability.translation',
            ])
            ->where(['product_id' => $productId])
            ->all();

        $imageForm = new CombinationImageForm();
        $productImages = ProductImage::find()->where(['product_id' => $productId])->all();

        $combinationAttributeForm = new CombinationAttributeForm();
        $combinationAttribute = new CombinationAttribute();

        $userGroups = UserGroup::find()->all();

        $prices = [];
        foreach ($userGroups as $userGroup) {
            $price = new Price();
            $prices[$userGroup->id] = $price;
        }

        if (\Yii::$app->request->isPost) {

            $post = \Yii::$app->request->post();
            if ($combination->load($post)) {
                $combination->product_id = $productId;

                $combination->setDefaultOrNotDefault();
                if ($combination->validate()) $combination->save();

                if ($combinationTranslation->load($post)) {
                    $combinationTranslation->combination_id = $combination->id;
                    $combinationTranslation->language_id = $languageId;
                    if ($combinationTranslation->validate()) $combinationTranslation->save();
                }

                if (Model::loadMultiple($prices, Yii::$app->request->post()) && Model::validateMultiple($prices)) {
                    foreach ($prices as $key => $price) {
                        $price->save(false);
                        $combinationPrice = new CombinationPrice();
                        $combinationPrice->combination_id = $combination->id;
                        $combinationPrice->price_id = $price->id;
                        $combinationPrice->user_group_id = $key;
                        if ($combinationPrice->validate()) $combinationPrice->save();
                    }
                }

                $this->loadCombinationAttributeForm($combination, $post, $combinationAttributeForm, $combinationAttribute);
                $this->saveCombinationImages($imageForm, $post, null, $combination);

                $eventName = self::EVENT_AFTER_EDIT_PRODUCT;
                $this->trigger($eventName, new ProductEvent([
                    'id' => $combination->product_id
                ]));

                $this->redirect(['add-combination',
                    'productId' => $productId,
                    'languageId' => $languageId
                ]);
            }
        }

        return $this->render('../product/save', array(
            'viewName' => '../combination/add-combination',
            'selectedLanguage' => Language::findOne($languageId),
            'product' => Product::findOne($productId),
            'languages' => Language::find()->all(),

            'params' => array(
                'combination' => $combination,
                'combinationTranslation' => $combinationTranslation,
                'combinations' => $combinationsList,
                'product' => Product::findOne($productId),
                'productImages' => $productImages,
                'image_form' => $imageForm,
                'languageId' => $languageId,
                'combinationAttribute' => $combinationAttribute,
                'combinationAttributeForm' => $combinationAttributeForm,
                'prices' => $prices,
            )
        ));
    }