/** * @param Struct\Category[] $categories * @param Struct\ShopContextInterface $context * @return Struct\Category[] $categories Indexed by the category id */ private function filterValidCategories($categories, Struct\ShopContextInterface $context) { $customerGroup = $context->getCurrentCustomerGroup(); return array_filter($categories, function (Struct\Category $category) use($customerGroup) { return !in_array($customerGroup->getId(), $category->getBlockedCustomerGroupIds()); }); }
/** * @inheritdoc */ public function getAvailableNumber($number, ShopContextInterface $context, $selection = []) { $productId = $this->getProductIdByNumber($number); if (!$productId) { throw new \RuntimeException("No valid product id found"); } if (!$this->isProductAvailableInShop($productId, $context->getShop())) { throw new \RuntimeException("Product not available in current shop"); } $selected = null; if (!empty($selection)) { $selected = $this->getNumberBySelection($productId, $selection); } if ($selected) { return $selected; } if ($this->isNumberAvailable($number)) { return $number; } $selected = $this->findFallbackById($productId); if (!$selected) { throw new \RuntimeException("No active product variant found"); } return $selected; }
/** * @param int[] $categoryIds * @param ShopContextInterface $context * @return Criteria */ public function createBaseCriteria($categoryIds, ShopContextInterface $context) { $criteria = new Criteria(); $criteria->addBaseCondition(new CategoryCondition($categoryIds)); if ($this->config->get('hideNoInstock')) { $criteria->addBaseCondition(new IsAvailableCondition()); } $criteria->addBaseCondition(new CustomerGroupCondition([$context->getCurrentCustomerGroup()->getId()])); return $criteria; }
/** * @inheritdoc */ public function getList($products, Struct\ShopContextInterface $context) { $group = $context->getCurrentCustomerGroup(); $rules = $this->cheapestPriceGateway->getList($products, $context, $group); $prices = $this->buildPrices($products, $rules, $group); //check if one of the products have no assigned price within the prices variable. $fallbackProducts = array_filter($products, function (Struct\BaseProduct $product) use($prices) { return !array_key_exists($product->getNumber(), $prices); }); if (empty($fallbackProducts)) { return $prices; } //if some product has no price, we have to load the fallback customer group prices for the fallbackProducts. $fallbackPrices = $this->cheapestPriceGateway->getList($fallbackProducts, $context, $context->getFallbackCustomerGroup()); $fallbackPrices = $this->buildPrices($fallbackProducts, $fallbackPrices, $context->getFallbackCustomerGroup()); $prices = $prices + $fallbackPrices; return $prices; }
private function getConfiguratorProduct($number, ShopContextInterface $context) { $product = $this->helper->getSimpleProduct($number, array_shift($context->getTaxRules()), $context->getCurrentCustomerGroup()); $configurator = $this->helper->getConfigurator($context->getCurrentCustomerGroup(), $number, array('farbe' => array('rot', 'blau', 'grün', 'schwarz', 'weiß'))); $product = array_merge($product, $configurator); $product['categories'] = [['id' => $context->getShop()->getCategory()->getId()]]; foreach ($product['variants'] as $index => &$variant) { $offset = ($index + 1) * -10; $variant['prices'] = $this->helper->getGraduatedPrices($context->getCurrentCustomerGroup()->getKey(), $offset); } return $product; }
/** * @param ShopContextInterface $context * @return string */ public function getPriceField(ShopContextInterface $context) { $key = $context->getCurrentCustomerGroup()->getKey(); $currency = $context->getCurrency()->getId(); return 'calculatedPrices.' . $key . '_' . $currency . '.calculatedPrice'; }
/** * @param QueryBuilder $query * @param ShopContextInterface $context */ public function addProductStreamTranslation(QueryBuilder $query, ShopContextInterface $context) { if ($context->getShop()->isDefault()) { return; } $this->addProductStreamTranslationWithSuffix($query); $query->setParameter(':language', $context->getShop()->getId()); if ($context->getShop()->getFallbackId() !== $context->getShop()->getId()) { $this->addProductStreamTranslationWithSuffix($query, 'Fallback'); $query->setParameter(':languageFallback', $context->getShop()->getFallbackId()); } }
/** * @inheritdoc */ public function joinDefaultPrices(QueryBuilder $query, Struct\ShopContextInterface $context) { if ($query->hasState(self::STATE_INCLUDES_DEFAULT_PRICE)) { return; } $this->joinAvailableVariant($query); $graduation = 'defaultPrice.from = 1'; if ($this->config->get('useLastGraduationForCheapestPrice')) { $graduation = "defaultPrice.to = 'beliebig'"; } $query->innerJoin('product', 's_articles_prices', 'defaultPrice', 'defaultPrice.articledetailsID = availableVariant.id AND defaultPrice.pricegroup = :fallbackCustomerGroup AND ' . $graduation); $query->setParameter(':fallbackCustomerGroup', $context->getFallbackCustomerGroup()->getKey()); $query->addState(self::STATE_INCLUDES_DEFAULT_PRICE); }
/** * @param Criteria $criteria * @param ShopContextInterface $context */ private function addCustomerGroupCondition(Criteria $criteria, ShopContextInterface $context) { $condition = new CustomerGroupCondition([$context->getCurrentCustomerGroup()->getId()]); $criteria->addBaseCondition($condition); }
/** * {@inheritdoc} */ public function hydrate(array $elasticResult, ProductNumberSearchResult $result, Criteria $criteria, ShopContextInterface $context) { if (!isset($elasticResult['aggregations'])) { return; } if (!isset($elasticResult['aggregations']['agg_properties'])) { return; } $data = $elasticResult['aggregations']['agg_properties']['buckets']; $ids = array_column($data, 'key'); if (empty($ids)) { return; } $groupIds = $this->getGroupIds($ids); $search = new Search(); $search->addFilter(new IdsFilter($groupIds)); $search->addFilter(new TermFilter('filterable', 1)); $search->addSort(new FieldSort('name')); $index = $this->indexFactory->createShopIndex($context->getShop()); $data = $this->client->search(['index' => $index->getName(), 'type' => PropertyMapping::TYPE, 'body' => $search->toArray()]); $data = $data['hits']['hits']; $properties = $this->hydrateProperties($data, $ids); $actives = $this->getFilteredValues($criteria); $criteriaPart = $this->createCollectionResult($properties, $actives); $result->addFacet($criteriaPart); }
/** * @param Struct\ListProduct $product * @param Struct\ShopContextInterface $context * @param $limit * @return Search */ protected function getSearch($product, Struct\ShopContextInterface $context, $limit) { $search = new Search(); $search->setSize($limit); $search->addQuery($this->getSimilarQuery($product)); $search->addFilter($this->getProductNumberFilter($product)); $search->addFilter($this->getCategoryFilter($context->getShop()->getCategory())); $search->addFilter($this->getCustomerGroupFilter($context->getCurrentCustomerGroup())); return $search; }
/** * @param ShopContextInterface $shopContext * @param Tax[] $taxRules * @param PriceGroup[] $priceGroups * @return ProductContext */ public static function createFromContexts(ShopContextInterface $shopContext, $taxRules, $priceGroups) { return new self($shopContext->getBaseUrl(), $shopContext->getShop(), $shopContext->getCurrency(), $shopContext->getCurrentCustomerGroup(), $shopContext->getFallbackCustomerGroup(), $taxRules, $priceGroups); }
/** * @param Struct\ShopContextInterface $context * @param Struct\Country\Area|null $area * @param Struct\Country|null $country * @param Struct\Country\State|null $state * @return Struct\Tax[] */ protected function createTaxRulesStruct(Struct\ShopContextInterface $context, Struct\Country\Area $area = null, Struct\Country $country = null, Struct\Country\State $state = null) { $rules = $this->taxGateway->getRules($context->getCurrentCustomerGroup(), $area, $country, $state); return $rules; }
/** * @inheritdoc */ public function getListByCategory($products, Struct\ShopContextInterface $context) { if (!$this->config->offsetExists('similarLimit') || $this->config->get('similarLimit') <= 0) { return []; } $ids = []; foreach ($products as $product) { $ids[] = $product->getId(); } $ids = array_unique($ids); $categoryId = 1; if ($context->getShop() && $context->getShop()->getCategory()) { $categoryId = $context->getShop()->getCategory()->getId(); } $query = $this->connection->createQueryBuilder(); $query->select(['main.articleID', "GROUP_CONCAT(subVariant.ordernumber SEPARATOR '|') as similar"]); $query->from('s_articles_categories', 'main'); $query->innerJoin('main', 's_articles_categories', 'sub', 'sub.categoryID = main.categoryID AND sub.articleID != main.articleID'); $query->innerJoin('sub', 's_articles_details', 'subVariant', 'subVariant.articleID = sub.articleID AND subVariant.kind = 1'); $query->innerJoin('main', 's_categories', 'category', 'category.id = sub.categoryID AND category.id = main.categoryID'); $query->where('main.articleID IN (:ids)')->andWhere('category.path LIKE :path'); $query->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY)->setParameter(':path', '%|' . (int) $categoryId . '|'); $query->groupBy('main.articleID'); $statement = $query->execute(); $data = $statement->fetchAll(\PDO::FETCH_ASSOC); $limit = (int) $this->config->get('similarLimit'); $result = []; foreach ($data as $row) { $similar = explode('|', $row['similar']); $result[$row['articleID']] = array_slice($similar, 0, $limit); } return $result; }
/** * @param QueryBuilder $query * @param Struct\ShopContextInterface $context */ private function addTranslations($query, $context) { if ($context->getShop()->isDefault()) { return; } $query->addSelect('attributeTranslations.objectdata as __attribute_translation')->leftJoin('product', 's_core_translations', 'attributeTranslations', 'attributeTranslations.objectkey = product.id AND attributeTranslations.objecttype = "article" AND attributeTranslations.objectlanguage = :language'); $query->setParameter(':language', $context->getShop()->getId()); if (!$context->getShop()->getFallbackId() || $context->getShop()->getFallbackId() === $context->getShop()->getId()) { return; } $query->addSelect('attributeTranslations_fallback.objectdata as __attribute_translation_fallback')->leftJoin('product', 's_core_translations', 'attributeTranslations_fallback', 'attributeTranslations_fallback.objectkey = product.id AND attributeTranslations_fallback.objecttype = "article" AND attributeTranslations_fallback.objectlanguage = :languageFallback'); $query->setParameter(':languageFallback', $context->getShop()->getFallbackId()); }
/** * @param $ids * @param $context */ private function getCategoryIds($ids, ShopContextInterface $context) { $query = $this->connection->createQueryBuilder(); $query->select(['seoCategories.article_id', 'seoCategories.category_id'])->from('s_articles_categories_seo', 'seoCategories')->andWhere('seoCategories.article_id IN (:productIds)')->andWhere('seoCategories.shop_id = :shopId')->setParameter(':shopId', $context->getShop()->getId())->setParameter(':productIds', $ids, Connection::PARAM_INT_ARRAY); return $query->execute()->fetchAll(\PDO::FETCH_KEY_PAIR); }
/** * @param array $numbers * @param Struct\ShopContextInterface $context * @return \Doctrine\DBAL\Query\QueryBuilder */ protected function getQuery(array $numbers, Struct\ShopContextInterface $context) { $esdQuery = $this->getEsdQuery(); $customerGroupQuery = $this->getCustomerGroupQuery(); $availableVariantQuery = $this->getHasAvailableVariantQuery(); $fallbackPriceQuery = $this->getPriceCountQuery(':fallback'); $query = $this->connection->createQueryBuilder(); $query->select($this->fieldHelper->getArticleFields())->addSelect($this->fieldHelper->getTopSellerFields())->addSelect($this->fieldHelper->getVariantFields())->addSelect($this->fieldHelper->getUnitFields())->addSelect($this->fieldHelper->getTaxFields())->addSelect($this->fieldHelper->getPriceGroupFields())->addSelect($this->fieldHelper->getManufacturerFields())->addSelect($this->fieldHelper->getEsdFields())->addSelect('(' . $esdQuery->getSQL() . ') as __product_has_esd')->addSelect('(' . $customerGroupQuery->getSQL() . ') as __product_blocked_customer_groups')->addSelect('(' . $availableVariantQuery->getSQL() . ') as __product_has_available_variants')->addSelect('(' . $fallbackPriceQuery->getSQL() . ') as __product_fallback_price_count'); $query->setParameter(':fallback', $context->getFallbackCustomerGroup()->getKey()); if ($context->getCurrentCustomerGroup()->getId() !== $context->getFallbackCustomerGroup()->getId()) { $customerPriceQuery = $this->getPriceCountQuery(':current'); $query->addSelect('(' . $customerPriceQuery->getSQL() . ') as __product_custom_price_count'); $query->setParameter(':current', $context->getCurrentCustomerGroup()->getKey()); } $query->from('s_articles_details', 'variant')->innerJoin('variant', 's_articles', 'product', 'product.id = variant.articleID')->innerJoin('product', 's_core_tax', 'tax', 'tax.id = product.taxID')->leftJoin('variant', 's_core_units', 'unit', 'unit.id = variant.unitID')->leftJoin('product', 's_articles_supplier', 'manufacturer', 'manufacturer.id = product.supplierID')->leftJoin('product', 's_core_pricegroups', 'priceGroup', 'priceGroup.id = product.pricegroupID')->leftJoin('variant', 's_articles_attributes', 'productAttribute', 'productAttribute.articledetailsID = variant.id')->leftJoin('product', 's_articles_supplier_attributes', 'manufacturerAttribute', 'manufacturerAttribute.supplierID = product.supplierID')->leftJoin('product', 's_articles_top_seller_ro', 'topSeller', 'topSeller.article_id = product.id')->leftJoin('variant', 's_articles_esd', 'esd', 'esd.articledetailsID = variant.id')->leftJoin('esd', 's_articles_esd_attributes', 'esdAttribute', 'esdAttribute.esdID = esd.id')->where('variant.ordernumber IN (:numbers)')->andWhere('variant.active = 1')->andWhere('product.active = 1')->setParameter(':numbers', $numbers, Connection::PARAM_STR_ARRAY); $this->fieldHelper->addProductTranslation($query, $context); $this->fieldHelper->addVariantTranslation($query, $context); $this->fieldHelper->addManufacturerTranslation($query, $context); $this->fieldHelper->addUnitTranslation($query, $context); return $query; }