Skip to content

Commit

Permalink
MAGETWO-52717: [GitHub] Configurable product disabling lowest price a…
Browse files Browse the repository at this point in the history
…ssociated product still shows its price #4419

-- fix main select builder
  • Loading branch information
vnayda committed Oct 5, 2016
1 parent 36b07c7 commit 7c9547e
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 46 deletions.
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Framework\DB\Select;

/**
* Interface BaseSelectProcessorInterface
*/
interface BaseSelectProcessorInterface
{
/**
* Product table alias
*/
const PRODUCT_TABLE_ALIAS = 'child';

/**
* @param Select $select
* @return Select
*/
public function process(Select $select);
}
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Framework\DB\Select;
use Magento\Framework\Exception\InputException;

/**
* Class CompositeBaseSelectProcessor
*/
class CompositeBaseSelectProcessor implements BaseSelectProcessorInterface
{
/**
* @var BaseSelectProcessorInterface[]
*/
private $baseSelectProcessors;

/**
* @param BaseSelectProcessorInterface[] $baseSelectProcessors
* @throws InputException
*/
public function __construct(
array $baseSelectProcessors
) {
foreach ($baseSelectProcessors as $baseSelectProcessor) {
if (!$baseSelectProcessor instanceof BaseSelectProcessorInterface) {
throw new InputException(
__('Processor %1 doesn\'t implement BaseSelectProcessorInterface', get_class($baseSelectProcessor))
);
}
}
$this->baseSelectProcessors = $baseSelectProcessors;
}

/**
* @param Select $select
* @return Select
*/
public function process(Select $select)
{
foreach ($this->baseSelectProcessors as $baseSelectProcessor) {
$select = $baseSelectProcessor->process($select);
}
return $select;
}
}
Expand Up @@ -6,7 +6,8 @@
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\BaseSelectProcessorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;

Expand All @@ -32,22 +33,31 @@ class LinkedProductSelectBuilderByIndexPrice implements LinkedProductSelectBuild
*/
private $metadataPool;

/**
* @var BaseSelectProcessorInterface
*/
private $baseSelectProcessor;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param BaseSelectProcessorInterface $baseSelectProcessor
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResourceConnection $resourceConnection,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\EntityManager\MetadataPool $metadataPool
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
BaseSelectProcessorInterface $baseSelectProcessor = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
$this->customerSession = $customerSession;
$this->metadataPool = $metadataPool;
$this->baseSelectProcessor = (null !== $baseSelectProcessor)
? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
}

/**
Expand All @@ -58,24 +68,27 @@ public function build($productId)
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productTable = $this->resource->getTableName('catalog_product_entity');

return [$this->resource->getConnection()->select()
$priceSelect = $this->resource->getConnection()->select()
->from(['parent' => $productTable], '')
->joinInner(
['link' => $this->resource->getTableName('catalog_product_relation')],
"link.parent_id = parent.$linkField",
[]
)->joinInner(
['child' => $productTable],
"child.entity_id = link.child_id",
['entity_id']
[BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS => $productTable],
sprintf('%s.%s = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
[$linkField]
)->joinInner(
['t' => $this->resource->getTableName('catalog_product_index_price')],
't.entity_id = child.entity_id',
sprintf('t.entity_id = %s.%s', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
[]
)->where('parent.entity_id = ? ', $productId)
)->where("parent.{$linkField} = ?", $productId)
->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId())
->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
->order('t.min_price ' . Select::SQL_ASC)
->limit(1)];
->limit(1);
$priceSelect = $this->baseSelectProcessor->process($priceSelect);

return [$priceSelect];
}
}
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;
use Magento\Store\Model\Store;

Expand Down Expand Up @@ -37,25 +38,34 @@ class LinkedProductSelectBuilderByBasePrice implements LinkedProductSelectBuilde
*/
private $metadataPool;

/**
* @var BaseSelectProcessorInterface
*/
private $baseSelectProcessor;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Catalog\Helper\Data $catalogHelper
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param BaseSelectProcessorInterface $baseSelectProcessor
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResourceConnection $resourceConnection,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Catalog\Helper\Data $catalogHelper,
\Magento\Framework\EntityManager\MetadataPool $metadataPool
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
BaseSelectProcessorInterface $baseSelectProcessor = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
$this->eavConfig = $eavConfig;
$this->catalogHelper = $catalogHelper;
$this->metadataPool = $metadataPool;
$this->baseSelectProcessor = (null !== $baseSelectProcessor)
? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
}

/**
Expand All @@ -74,18 +84,19 @@ public function build($productId)
"link.parent_id = parent.$linkField",
[]
)->joinInner(
['child' => $productTable],
"child.entity_id = link.child_id",
['entity_id']
[BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS => $productTable],
sprintf('%s.%s = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
[$linkField]
)->joinInner(
['t' => $priceAttribute->getBackendTable()],
"t.$linkField = child.$linkField",
sprintf('t.%s = %s.%1$s', $linkField, BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
[]
)->where('parent.entity_id = ? ', $productId)
)->where("parent.{$linkField} = ?", $productId)
->where('t.attribute_id = ?', $priceAttribute->getAttributeId())
->where('t.value IS NOT NULL')
->order('t.value ' . Select::SQL_ASC)
->limit(1);
$priceSelect = $this->baseSelectProcessor->process($priceSelect);

$priceSelectDefault = clone $priceSelect;
$priceSelectDefault->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
Expand Down
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;
use Magento\Store\Model\Store;

Expand Down Expand Up @@ -47,6 +48,11 @@ class LinkedProductSelectBuilderBySpecialPrice implements LinkedProductSelectBui
*/
private $metadataPool;

/**
* @var BaseSelectProcessorInterface
*/
private $baseSelectProcessor;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
Expand All @@ -55,6 +61,7 @@ class LinkedProductSelectBuilderBySpecialPrice implements LinkedProductSelectBui
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param BaseSelectProcessorInterface $baseSelectProcessor
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
Expand All @@ -63,7 +70,8 @@ public function __construct(
\Magento\Catalog\Helper\Data $catalogHelper,
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Framework\EntityManager\MetadataPool $metadataPool
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
BaseSelectProcessorInterface $baseSelectProcessor = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
Expand All @@ -72,6 +80,8 @@ public function __construct(
$this->dateTime = $dateTime;
$this->localeDate = $localeDate;
$this->metadataPool = $metadataPool;
$this->baseSelectProcessor = (null !== $baseSelectProcessor)
? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
}

/**
Expand All @@ -95,12 +105,12 @@ public function build($productId)
"link.parent_id = parent.$linkField",
[]
)->joinInner(
['child' => $productTable],
"child.entity_id = link.child_id",
['entity_id']
[BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS => $productTable],
sprintf('%s.%s = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
[$linkField]
)->joinInner(
['t' => $specialPriceAttribute->getBackendTable()],
"t.$linkField = child.$linkField",
sprintf('t.%s = %s.%1$s', $linkField, BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
[]
)->joinLeft(
['special_from' => $specialPriceFromDate->getBackendTable()],
Expand All @@ -116,7 +126,7 @@ public function build($productId)
$specialPriceToDate->getAttributeId()
),
''
)->where('parent.entity_id = ? ', $productId)
)->where("parent.{$linkField} = ?", $productId)
->where('t.attribute_id = ?', $specialPriceAttribute->getAttributeId())
->where('t.value IS NOT NULL')
->where(
Expand All @@ -127,6 +137,7 @@ public function build($productId)
$currentDate
)->order('t.value ' . Select::SQL_ASC)
->limit(1);
$specialPrice = $this->baseSelectProcessor->process($specialPrice);

$specialPriceDefault = clone $specialPrice;
$specialPriceDefault->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
Expand Down
Expand Up @@ -6,7 +6,7 @@
namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;

class LinkedProductSelectBuilderByTierPrice implements LinkedProductSelectBuilderInterface
Expand Down Expand Up @@ -41,25 +41,34 @@ class LinkedProductSelectBuilderByTierPrice implements LinkedProductSelectBuilde
*/
private $metadataPool;

/**
* @var BaseSelectProcessorInterface
*/
private $baseSelectProcessor;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Catalog\Helper\Data $catalogHelper
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param BaseSelectProcessorInterface $baseSelectProcessor
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResourceConnection $resourceConnection,
\Magento\Customer\Model\Session $customerSession,
\Magento\Catalog\Helper\Data $catalogHelper,
\Magento\Framework\EntityManager\MetadataPool $metadataPool
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
BaseSelectProcessorInterface $baseSelectProcessor = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
$this->customerSession = $customerSession;
$this->catalogHelper = $catalogHelper;
$this->metadataPool = $metadataPool;
$this->baseSelectProcessor = (null !== $baseSelectProcessor)
? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
}

/**
Expand All @@ -77,18 +86,19 @@ public function build($productId)
"link.parent_id = parent.$linkField",
[]
)->joinInner(
['child' => $productTable],
"child.entity_id = link.child_id",
['entity_id']
[BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS => $productTable],
sprintf('%s.%s = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
[$linkField]
)->joinInner(
['t' => $this->resource->getTableName('catalog_product_entity_tier_price')],
"t.$linkField = child.$linkField",
sprintf('t.%s = %s.%1$s', $linkField, BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
[]
)->where('parent.entity_id = ? ', $productId)
)->where("parent.{$linkField} = ?", $productId)
->where('t.all_groups = 1 OR customer_group_id = ?', $this->customerSession->getCustomerGroupId())
->where('t.qty = ?', 1)
->order('t.value ' . Select::SQL_ASC)
->limit(1);
$priceSelect = $this->baseSelectProcessor->process($priceSelect);

$priceSelectDefault = clone $priceSelect;
$priceSelectDefault->where('t.website_id = ?', self::DEFAULT_WEBSITE_ID);
Expand Down

0 comments on commit 7c9547e

Please sign in to comment.