Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rule.php
Go to the documentation of this file.
1 <?php
13 
17 
22 {
26  const SECONDS_IN_DAY = 86400;
27 
31  protected $_logger;
32 
38  protected $_catalogRuleData = null;
39 
45  protected $_eventManager = null;
46 
50  protected $_eavConfig;
51 
55  protected $_coreDate;
56 
60  protected $_conditionFactory;
61 
65  protected $_storeManager;
66 
70  protected $dateTime;
71 
75  protected $priceCurrency;
76 
80  protected $entityManager;
81 
97  public function __construct(
98  \Magento\Framework\Model\ResourceModel\Db\Context $context,
100  \Magento\Catalog\Model\Product\ConditionFactory $conditionFactory,
101  \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
102  \Magento\Eav\Model\Config $eavConfig,
103  \Magento\Framework\Event\ManagerInterface $eventManager,
104  \Magento\CatalogRule\Helper\Data $catalogRuleData,
105  \Psr\Log\LoggerInterface $logger,
106  \Magento\Framework\Stdlib\DateTime $dateTime,
108  $connectionName = null
109  ) {
110  $this->_storeManager = $storeManager;
111  $this->_conditionFactory = $conditionFactory;
112  $this->_coreDate = $coreDate;
113  $this->_eavConfig = $eavConfig;
114  $this->_eventManager = $eventManager;
115  $this->_catalogRuleData = $catalogRuleData;
116  $this->_logger = $logger;
117  $this->dateTime = $dateTime;
118  $this->priceCurrency = $priceCurrency;
119  $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
120  parent::__construct($context, $connectionName);
121  }
122 
129  protected function _construct()
130  {
131  $this->_init('catalogrule', 'rule_id');
132  }
133 
138  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $rule)
139  {
140  $connection = $this->getConnection();
141  $connection->delete(
142  $this->getTable('catalogrule_product'),
143  ['rule_id=?' => $rule->getId()]
144  );
145  $connection->delete(
146  $this->getTable('catalogrule_group_website'),
147  ['rule_id=?' => $rule->getId()]
148  );
149  return parent::_afterDelete($rule);
150  }
151 
162  public function getRulePrice($date, $wId, $gId, $pId)
163  {
164  $data = $this->getRulePrices($date, $wId, $gId, [$pId]);
165  if (isset($data[$pId])) {
166  return $data[$pId];
167  }
168 
169  return false;
170  }
171 
182  public function getRulePrices(\DateTimeInterface $date, $websiteId, $customerGroupId, $productIds)
183  {
184  $connection = $this->getConnection();
185  $select = $connection->select()
186  ->from($this->getTable('catalogrule_product_price'), ['product_id', 'rule_price'])
187  ->where('rule_date = ?', $date->format('Y-m-d'))
188  ->where('website_id = ?', $websiteId)
189  ->where('customer_group_id = ?', $customerGroupId)
190  ->where('product_id IN(?)', $productIds);
191 
192  return $connection->fetchPairs($select);
193  }
194 
204  public function getRulesFromProduct($date, $websiteId, $customerGroupId, $productId)
205  {
206  $connection = $this->getConnection();
207  if (is_string($date)) {
208  $date = strtotime($date);
209  }
210  $select = $connection->select()
211  ->from($this->getTable('catalogrule_product'))
212  ->where('website_id = ?', $websiteId)
213  ->where('customer_group_id = ?', $customerGroupId)
214  ->where('product_id = ?', $productId)
215  ->where('from_time = 0 or from_time < ?', $date)
216  ->where('to_time = 0 or to_time > ?', $date);
217 
218  return $connection->fetchAll($select);
219  }
220 
228  public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
229  {
230  $this->getEntityManager()->load($object, $value);
231  return $this;
232  }
233 
239  public function save(\Magento\Framework\Model\AbstractModel $object)
240  {
241  $this->getEntityManager()->save($object);
242  return $this;
243  }
244 
252  public function delete(AbstractModel $object)
253  {
254  $this->getEntityManager()->delete($object);
255  return $this;
256  }
257 
262  private function getAssociatedEntitiesMap()
263  {
264  if (!$this->_associatedEntitiesMap) {
265  $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
266  ->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
267  ->getData();
268  }
270  }
271 
276  private function getEntityManager()
277  {
278  if (null === $this->entityManager) {
280  ->get(\Magento\Framework\EntityManager\EntityManager::class);
281  }
282  return $this->entityManager;
283  }
284 }
_afterDelete(\Magento\Framework\Model\AbstractModel $rule)
Definition: Rule.php:138
load(\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
Definition: Rule.php:228
$storeManager
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product\ConditionFactory $conditionFactory, \Magento\Framework\Stdlib\DateTime\DateTime $coreDate, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\CatalogRule\Helper\Data $catalogRuleData, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Stdlib\DateTime $dateTime, PriceCurrencyInterface $priceCurrency, $connectionName=null)
Definition: Rule.php:97
$logger
$value
Definition: gender.phtml:16
getRulesFromProduct($date, $websiteId, $customerGroupId, $productId)
Definition: Rule.php:204
save(\Magento\Framework\Model\AbstractModel $object)
Definition: Rule.php:239
getRulePrices(\DateTimeInterface $date, $websiteId, $customerGroupId, $productIds)
Definition: Rule.php:182
getRulePrice($date, $wId, $gId, $pId)
Definition: Rule.php:162
$connection
Definition: bulk.php:13