Skip to content

Commit

Permalink
Merge pull request #4485 from magento-arcticfoxes/MC-5777
Browse files Browse the repository at this point in the history
MC-5777: Catalog rule does not apply as expected in EE
  • Loading branch information
Joan He authored Jul 18, 2019
2 parents 2e7f039 + 3856ff6 commit 17183cf
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 76 deletions.
25 changes: 18 additions & 7 deletions app/code/Magento/Catalog/Model/Product/Type/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Type;

use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -91,6 +93,11 @@ class Price
*/
private $tierPriceExtensionFactory;

/**
* @var \Magento\CatalogRule\Model\RuleDateFormatterInterface
*/
private $ruleDateFormatter;

/**
* Constructor
*
Expand All @@ -104,6 +111,7 @@ class Price
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory
* @param \Magento\CatalogRule\Model\RuleDateFormatterInterface|null $ruleDateFormatter
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -116,7 +124,8 @@ public function __construct(
GroupManagementInterface $groupManagement,
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null,
\Magento\CatalogRule\Model\RuleDateFormatterInterface $ruleDateFormatter = null
) {
$this->_ruleFactory = $ruleFactory;
$this->_storeManager = $storeManager;
Expand All @@ -129,6 +138,8 @@ public function __construct(
$this->config = $config;
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory ?: ObjectManager::getInstance()
->get(ProductTierPriceExtensionFactory::class);
$this->ruleDateFormatter = $ruleDateFormatter ?: ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\RuleDateFormatterInterface::class);
}

/**
Expand Down Expand Up @@ -502,10 +513,10 @@ public function getFormattedTierPrice($qty, $product)
/**
* Get formatted by currency tier price
*
* @param float $qty
* @param Product $product
* @param float $qty
* @param Product $product
*
* @return array|float
* @return array|float
*
* @deprecated
* @see getFormattedTierPrice()
Expand All @@ -529,8 +540,8 @@ public function getFormattedPrice($product)
/**
* Get formatted by currency product price
*
* @param Product $product
* @return array || float
* @param Product $product
* @return array || float
*
* @deprecated
* @see getFormattedPrice()
Expand Down Expand Up @@ -611,7 +622,7 @@ public function calculatePrice(
);

if ($rulePrice === false) {
$date = $this->_localeDate->scopeDate($sId);
$date = $this->ruleDateFormatter->getDate($sId);
$rulePrice = $this->_ruleFactory->create()->getRulePrice($date, $wId, $gId, $productId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogRule\Model\ResourceModel\Product;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\CatalogRule\Pricing\Price\CatalogRulePrice;
use Magento\Framework\App\ObjectManager;

/**
* Add catalog rule prices to collection
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CollectionProcessor
{
Expand Down Expand Up @@ -39,28 +44,39 @@ class CollectionProcessor
*/
private $localeDate;

/**
* @var \Magento\CatalogRule\Model\RuleDateFormatterInterface
*/
private $ruleDateFormatter;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\CatalogRule\Model\RuleDateFormatterInterface|null $ruleDateFormatter
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResourceConnection $resourceConnection,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\CatalogRule\Model\RuleDateFormatterInterface $ruleDateFormatter = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
$this->customerSession = $customerSession;
$this->dateTime = $dateTime;
$this->localeDate = $localeDate;
$this->ruleDateFormatter = $ruleDateFormatter ?: ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\RuleDateFormatterInterface::class);
}

/**
* Join prices to collection
*
* @param ProductCollection $productCollection
* @param string $joinColumn
* @return ProductCollection
Expand All @@ -73,18 +89,21 @@ public function addPriceData(ProductCollection $productCollection, $joinColumn =
$productCollection->getSelect()
->joinLeft(
['catalog_rule' => $this->resource->getTableName('catalogrule_product_price')],
implode(' AND ', [
'catalog_rule.product_id = ' . $connection->quoteIdentifier($joinColumn),
$connection->quoteInto('catalog_rule.website_id = ?', $store->getWebsiteId()),
$connection->quoteInto(
'catalog_rule.customer_group_id = ?',
$this->customerSession->getCustomerGroupId()
),
$connection->quoteInto(
'catalog_rule.rule_date = ?',
$this->dateTime->formatDate($this->localeDate->scopeDate($store->getId()), false)
),
]),
implode(
' AND ',
[
'catalog_rule.product_id = ' . $connection->quoteIdentifier($joinColumn),
$connection->quoteInto('catalog_rule.website_id = ?', $store->getWebsiteId()),
$connection->quoteInto(
'catalog_rule.customer_group_id = ?',
$this->customerSession->getCustomerGroupId()
),
$connection->quoteInto(
'catalog_rule.rule_date = ?',
$this->dateTime->formatDate($this->ruleDateFormatter->getDate($store->getId()), false)
),
]
),
[CatalogRulePrice::PRICE_CODE => 'rule_price']
);
$productCollection->setFlag('catalog_rule_loaded', true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogRule\Model\ResourceModel\Product;

use Magento\Catalog\Api\Data\ProductInterface;
Expand All @@ -11,6 +13,11 @@
use Magento\Framework\DB\Select;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;

/**
* Provide Select object for retrieve product id with minimal price
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class LinkedProductSelectBuilderByCatalogRulePrice implements LinkedProductSelectBuilderInterface
{
/**
Expand Down Expand Up @@ -48,6 +55,11 @@ class LinkedProductSelectBuilderByCatalogRulePrice implements LinkedProductSelec
*/
private $baseSelectProcessor;

/**
* @var \Magento\CatalogRule\Model\RuleDateFormatterInterface
*/
private $ruleDateFormatter;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
Expand All @@ -56,6 +68,7 @@ class LinkedProductSelectBuilderByCatalogRulePrice implements LinkedProductSelec
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param BaseSelectProcessorInterface $baseSelectProcessor
* @param \Magento\CatalogRule\Model\RuleDateFormatterInterface|null $ruleDateFormatter
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
Expand All @@ -64,7 +77,8 @@ public function __construct(
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
BaseSelectProcessorInterface $baseSelectProcessor = null
BaseSelectProcessorInterface $baseSelectProcessor = null,
\Magento\CatalogRule\Model\RuleDateFormatterInterface $ruleDateFormatter = null
) {
$this->storeManager = $storeManager;
$this->resource = $resourceConnection;
Expand All @@ -74,14 +88,16 @@ public function __construct(
$this->metadataPool = $metadataPool;
$this->baseSelectProcessor = (null !== $baseSelectProcessor)
? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
$this->ruleDateFormatter = $ruleDateFormatter ?: ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\RuleDateFormatterInterface::class);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function build($productId)
{
$timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
$timestamp = $this->ruleDateFormatter->getTimeStamp($this->storeManager->getStore());
$currentDate = $this->dateTime->formatDate($timestamp, false);
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productTable = $this->resource->getTableName('catalog_product_entity');
Expand Down
43 changes: 43 additions & 0 deletions app/code/Magento/CatalogRule/Model/RuleDateFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogRule\Model;

/**
* Local date for catalog rule
*/
class RuleDateFormatter implements \Magento\CatalogRule\Model\RuleDateFormatterInterface
{
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
private $localeDate;

/**
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
*/
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
{
$this->localeDate = $localeDate;
}

/**
* @inheritdoc
*/
public function getDate($scope = null): \DateTime
{
return $this->localeDate->scopeDate($scope, null, true);
}

/**
* @inheritdoc
*/
public function getTimeStamp($scope = null): int
{
return $this->localeDate->scopeTimeStamp($scope);
}
}
30 changes: 30 additions & 0 deletions app/code/Magento/CatalogRule/Model/RuleDateFormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogRule\Model;

/**
* Local date for catalog rule
*/
interface RuleDateFormatterInterface
{
/**
* Create \DateTime object with date converted to scope timezone for catalog rule
*
* @param mixed $scope Information about scope
* @return \DateTime
*/
public function getDate($scope = null): \DateTime;

/**
* Get scope timestamp for catalog rule
*
* @param mixed $scope Information about scope
* @return int
*/
public function getTimeStamp($scope = null): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/**
* Catalog Price rules observer model
*/
declare(strict_types=1);

namespace Magento\CatalogRule\Observer;

use Magento\Catalog\Model\Product;
Expand All @@ -17,9 +19,13 @@
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Customer\Api\GroupManagementInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;

/**
* Observer for applying catalog rules on product collection
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class PrepareCatalogProductCollectionPricesObserver implements ObserverInterface
{
Expand Down Expand Up @@ -53,28 +59,37 @@ class PrepareCatalogProductCollectionPricesObserver implements ObserverInterface
*/
protected $groupManagement;

/**
* @var \Magento\CatalogRule\Model\RuleDateFormatterInterface
*/
private $ruleDateFormatter;

/**
* @param RulePricesStorage $rulePricesStorage
* @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory
* @param StoreManagerInterface $storeManager
* @param TimezoneInterface $localeDate
* @param CustomerModelSession $customerSession
* @param GroupManagementInterface $groupManagement
* @param \Magento\CatalogRule\Model\RuleDateFormatterInterface|null $ruleDateFormatter
*/
public function __construct(
RulePricesStorage $rulePricesStorage,
\Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory,
StoreManagerInterface $storeManager,
TimezoneInterface $localeDate,
CustomerModelSession $customerSession,
GroupManagementInterface $groupManagement
GroupManagementInterface $groupManagement,
\Magento\CatalogRule\Model\RuleDateFormatterInterface $ruleDateFormatter = null
) {
$this->rulePricesStorage = $rulePricesStorage;
$this->resourceRuleFactory = $resourceRuleFactory;
$this->storeManager = $storeManager;
$this->localeDate = $localeDate;
$this->customerSession = $customerSession;
$this->groupManagement = $groupManagement;
$this->ruleDateFormatter = $ruleDateFormatter ?: ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\RuleDateFormatterInterface::class);
}

/**
Expand All @@ -101,7 +116,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
if ($observer->getEvent()->hasDate()) {
$date = new \DateTime($observer->getEvent()->getDate());
} else {
$date = (new \DateTime())->setTimestamp($this->localeDate->scopeTimeStamp($store));
$date = (new \DateTime())->setTimestamp($this->ruleDateFormatter->getTimeStamp($store));
}

$productIds = [];
Expand Down
Loading

0 comments on commit 17183cf

Please sign in to comment.