Stock.php 1.9 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 back in stock resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Stock 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_stock', 'alert_stock_id');
  40. }
  41. /**
  42. * Before save action
  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
  50. && $object->getCustomerId()
  51. && $object->getProductId()
  52. && $object->getWebsiteId()
  53. ) {
  54. if ($row = $this->_getAlertRow($object)) {
  55. $object->addData($row);
  56. $object->setStatus(0);
  57. }
  58. }
  59. if ($object->getAddDate() === null) {
  60. $object->setAddDate($this->_dateFactory->create()->gmtDate());
  61. $object->setStatus(0);
  62. }
  63. return parent::_beforeSave($object);
  64. }
  65. }