Product.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\ConfigurableProduct\Plugin\Model\ResourceModel;
  8. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  9. use Magento\Framework\Indexer\ActionInterface;
  10. class Product
  11. {
  12. /**
  13. * @var Configurable
  14. */
  15. private $configurable;
  16. /**
  17. * @var ActionInterface
  18. */
  19. private $productIndexer;
  20. /**
  21. * Initialize Product dependencies.
  22. *
  23. * @param Configurable $configurable
  24. * @param ActionInterface $productIndexer
  25. */
  26. public function __construct(
  27. Configurable $configurable,
  28. ActionInterface $productIndexer
  29. ) {
  30. $this->configurable = $configurable;
  31. $this->productIndexer = $productIndexer;
  32. }
  33. /**
  34. * We need reset attribute set id to attribute after related simple product was saved
  35. *
  36. * @param \Magento\Catalog\Model\ResourceModel\Product $subject
  37. * @param \Magento\Framework\DataObject $object
  38. * @return void
  39. *
  40. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  41. */
  42. public function beforeSave(
  43. \Magento\Catalog\Model\ResourceModel\Product $subject,
  44. \Magento\Framework\DataObject $object
  45. ) {
  46. /** @var \Magento\Catalog\Model\Product $object */
  47. if ($object->getTypeId() == Configurable::TYPE_CODE) {
  48. $object->getTypeInstance()->getSetAttributes($object);
  49. }
  50. }
  51. /**
  52. * Gather configurable parent ids of product being deleted and reindex after delete is complete.
  53. *
  54. * @param \Magento\Catalog\Model\ResourceModel\Product $subject
  55. * @param \Closure $proceed
  56. * @param \Magento\Catalog\Model\Product $product
  57. * @return \Magento\Catalog\Model\ResourceModel\Product
  58. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  59. */
  60. public function aroundDelete(
  61. \Magento\Catalog\Model\ResourceModel\Product $subject,
  62. \Closure $proceed,
  63. \Magento\Catalog\Model\Product $product
  64. ) {
  65. $configurableProductIds = $this->configurable->getParentIdsByChild($product->getId());
  66. $result = $proceed($product);
  67. $this->productIndexer->executeList($configurableProductIds);
  68. return $result;
  69. }
  70. }