Price.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Model\ResourceModel;
  7. /**
  8. * Product alert for changed price resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Price extends \Magento\ProductAlert\Model\ResourceModel\AbstractResource
  14. {
  15. /**
  16. * @var \Magento\Framework\Stdlib\DateTime\DateTimeFactory
  17. */
  18. protected $_dateFactory;
  19. /**
  20. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  21. * @param \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory
  22. * @param string $connectionName
  23. */
  24. public function __construct(
  25. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  26. \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory,
  27. $connectionName = null
  28. ) {
  29. $this->_dateFactory = $dateFactory;
  30. parent::__construct($context, $connectionName);
  31. }
  32. /**
  33. * Initialize connection
  34. *
  35. * @return void
  36. */
  37. protected function _construct()
  38. {
  39. $this->_init('product_alert_price', 'alert_price_id');
  40. }
  41. /**
  42. * Before save process, check exists the same alert
  43. *
  44. * @param \Magento\Framework\Model\AbstractModel $object
  45. * @return $this
  46. */
  47. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  48. {
  49. if ($object->getId() === null && $object->getCustomerId() && $object->getProductId() && $object->getWebsiteId()
  50. ) {
  51. if ($row = $this->_getAlertRow($object)) {
  52. $price = $object->getPrice();
  53. $object->addData($row);
  54. if ($price) {
  55. $object->setPrice($price);
  56. }
  57. $object->setStatus(0);
  58. }
  59. }
  60. if ($object->getAddDate() === null) {
  61. $object->setAddDate($this->_dateFactory->create()->gmtDate());
  62. }
  63. return parent::_beforeSave($object);
  64. }
  65. }