Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 3.3.0 #142

Merged
merged 19 commits into from Sep 17, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Merge pull request #132 from PierreRambaud/fix/15056

Calculate the number of available products with the stock management
  • Loading branch information
PierreRambaud committed Sep 13, 2019
commit 94f6dff568d4f0030deaa66a3726785254b383d7
@@ -73,6 +73,8 @@ public function __clone()
{
$this->filters = clone $this->filters;
$this->operationsFilters = clone $this->operationsFilters;
$this->groupFields = clone $this->groupFields;
$this->selectFields = clone $this->selectFields;
}
/**
@@ -195,7 +197,8 @@ public function getOperationsFilters()
*/
public function copyFilters(InterfaceAdapter $adapter)
{
$this->filters = $adapter->getFilters();
$this->filters = clone $adapter->getFilters();
$this->operationsFilters = clone $adapter->getOperationsFilters();
}
/**
@@ -190,7 +190,7 @@ public function addSelectField($fieldName);
*
* @return mixed
*/
public function valueCount($fieldName);
public function valueCount($fieldName = null);
/**
* Reset the operations filters
@@ -48,11 +48,6 @@ class MySQL extends AbstractAdapter
*/
const INNER_JOIN = 'INNER JOIN';
/**
* @var string
*/
const STRAIGHT_JOIN = 'STRAIGHT_JOIN';
/**
* {@inheritdoc}
*/
@@ -111,38 +106,28 @@ public function getQuery()
$referenceTable = '(' . $this->getInitialPopulation()->getQuery() . ')';
}
if (empty($this->getSelectFields())
&& empty($this->getFilters())
&& empty($this->getGroupFields())
&& empty($this->getOrderField())
) {
// avoid adding an extra SELECT FROM (SELECT ...) if it's not needed
$query = $referenceTable;
$this->setOrderField('');
} else {
$query = 'SELECT ';
$query = 'SELECT ';
$selectFields = $this->computeSelectFields($filterToTableMapping);
$whereConditions = $this->computeWhereConditions($filterToTableMapping);
$joinConditions = $this->computeJoinConditions($filterToTableMapping);
$groupFields = $this->computeGroupByFields($filterToTableMapping);
$selectFields = $this->computeSelectFields($filterToTableMapping);
$whereConditions = $this->computeWhereConditions($filterToTableMapping);
$joinConditions = $this->computeJoinConditions($filterToTableMapping);
$groupFields = $this->computeGroupByFields($filterToTableMapping);
$query .= implode(', ', $selectFields) . ' FROM ' . $referenceTable . ' p';
$query .= implode(', ', $selectFields) . ' FROM ' . $referenceTable . ' p';
foreach ($joinConditions as $joinAliasInfos) {
foreach ($joinAliasInfos as $tableAlias => $joinInfos) {
$query .= ' ' . $joinInfos['joinType'] . ' ' . _DB_PREFIX_ . $joinInfos['tableName'] . ' ' .
$tableAlias . ' ON ' . $joinInfos['joinCondition'];
}
foreach ($joinConditions as $joinAliasInfos) {
foreach ($joinAliasInfos as $tableAlias => $joinInfos) {
$query .= ' ' . $joinInfos['joinType'] . ' ' . _DB_PREFIX_ . $joinInfos['tableName'] . ' ' .
$tableAlias . ' ON ' . $joinInfos['joinCondition'];
}
}
if (!empty($whereConditions)) {
$query .= ' WHERE ' . implode(' AND ', $whereConditions);
}
if (!empty($whereConditions)) {
$query .= ' WHERE ' . implode(' AND ', $whereConditions);
}
if ($groupFields) {
$query .= ' GROUP BY ' . implode(', ', $groupFields);
}
if ($groupFields) {
$query .= ' GROUP BY ' . implode(', ', $groupFields);
}
if ($orderField) {
@@ -174,20 +159,20 @@ protected function getFieldMapping()
'tableName' => 'product_attribute',
'tableAlias' => 'pa',
'joinCondition' => '(p.id_product = pa.id_product)',
'joinType' => self::STRAIGHT_JOIN,
'joinType' => self::LEFT_JOIN,
],
'id_attribute' => [
'tableName' => 'product_attribute_combination',
'tableAlias' => 'pac',
'joinCondition' => '(pa.id_product_attribute = pac.id_product_attribute)',
'joinType' => self::STRAIGHT_JOIN,
'joinType' => self::LEFT_JOIN,
'dependencyField' => 'id_product_attribute',
],
'id_attribute_group' => [
'tableName' => 'attribute',
'tableAlias' => 'a',
'joinCondition' => '(a.id_attribute = pac.id_attribute)',
'joinType' => self::STRAIGHT_JOIN,
'joinType' => self::INNER_JOIN,
'dependencyField' => 'id_attribute',
],
'id_feature' => [
@@ -259,16 +244,18 @@ protected function getFieldMapping()
'out_of_stock' => [
'tableName' => 'stock_available',
'tableAlias' => 'sa',
'joinCondition' => '(p.id_product = sa.id_product AND 0 = sa.id_product_attribute ' .
'joinCondition' => '(p.id_product = sa.id_product AND IFNULL(pac.id_product_attribute, 0) = sa.id_product_attribute' .
$stockCondition . ')',
'joinType' => self::LEFT_JOIN,
'dependencyField' => 'id_attribute',
],
'quantity' => [
'tableName' => 'stock_available',
'tableAlias' => 'sa',
'joinCondition' => '(p.id_product = sa.id_product AND 0 = sa.id_product_attribute ' .
'joinCondition' => '(p.id_product = sa.id_product AND IFNULL(pac.id_product_attribute, 0) = sa.id_product_attribute' .
$stockCondition . ')',
'joinType' => self::LEFT_JOIN,
'dependencyField' => 'id_attribute',
],
'price_min' => [
'tableName' => 'layered_price_index',
@@ -601,7 +588,7 @@ private function addJoinConditions(ArrayCollection $joinList, array $joinMapping
private function computeGroupByFields(array $filterToTableMapping)
{
$groupFields = [];
if (empty($this->getGroupFields())) {
if ($this->getGroupFields()->isEmpty()) {
return $groupFields;
}
@@ -646,27 +633,29 @@ public function count()
{
$mysqlAdapter = $this->getFilteredSearchAdapter();
$mysqlAdapter->copyFilters($this);
$mysqlAdapter->setSelectFields(['COUNT(DISTINCT p.id_product) c']);
$mysqlAdapter->setLimit(null);
$mysqlAdapter->setOrderField('');
$result = $mysqlAdapter->execute();
$result = $mysqlAdapter->valueCount();
return isset($result[0]['c']) ? $result[0]['c'] : 0;
return isset($result[0]['c']) ? (int) $result[0]['c'] : 0;
}
/**
* {@inheritdoc}
*/
public function valueCount($fieldName)
public function valueCount($fieldName = null)
{
$this->resetGroupBy();
$this->addGroupBy($fieldName);
$this->addSelectField($fieldName);
if ($fieldName !== null) {
$this->addGroupBy($fieldName);
$this->addSelectField($fieldName);
}
$this->addSelectField('COUNT(DISTINCT p.id_product) c');
$this->setLimit(null);
$this->setOrderField('');
$this->copyOperationsFilters();
return $this->execute();
}
@@ -707,4 +696,24 @@ protected function getDatabase()
{
return Db::getInstance();
}
/**
* Copy stock management operation filters
* to make sure quantity is also used
*/
protected function copyOperationsFilters()
{
$initialPopulation = $this->getInitialPopulation();
if (null === $initialPopulation) {
return;
}
$operationsFilters = clone $initialPopulation->getOperationsFilters();
foreach ($operationsFilters as $operationName => $operations) {
$this->addOperationsFilter(
$operationName,
$operations
);
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.