AppendUpsellProductsObserver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. class AppendUpsellProductsObserver implements ObserverInterface
  9. {
  10. /**
  11. * Bundle data
  12. *
  13. * @var \Magento\Bundle\Helper\Data
  14. */
  15. protected $bundleData;
  16. /**
  17. * @var \Magento\Bundle\Model\ResourceModel\Selection
  18. */
  19. protected $bundleSelection;
  20. /**
  21. * @var \Magento\Catalog\Model\Config
  22. */
  23. protected $config;
  24. /**
  25. * @var \Magento\Catalog\Model\Product\Visibility
  26. */
  27. protected $productVisibility;
  28. /**
  29. * @param \Magento\Bundle\Helper\Data $bundleData
  30. * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
  31. * @param \Magento\Catalog\Model\Config $config
  32. * @param \Magento\Bundle\Model\ResourceModel\Selection $bundleSelection
  33. */
  34. public function __construct(
  35. \Magento\Bundle\Helper\Data $bundleData,
  36. \Magento\Catalog\Model\Product\Visibility $productVisibility,
  37. \Magento\Catalog\Model\Config $config,
  38. \Magento\Bundle\Model\ResourceModel\Selection $bundleSelection
  39. ) {
  40. $this->bundleData = $bundleData;
  41. $this->productVisibility = $productVisibility;
  42. $this->config = $config;
  43. $this->bundleSelection = $bundleSelection;
  44. }
  45. /**
  46. * Append bundles in upsell list for current product
  47. *
  48. * @param \Magento\Framework\Event\Observer $observer
  49. * @return $this
  50. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  51. * @SuppressWarnings(PHPMD.NPathComplexity)
  52. */
  53. public function execute(\Magento\Framework\Event\Observer $observer)
  54. {
  55. /* @var $product \Magento\Catalog\Model\Product */
  56. $product = $observer->getEvent()->getProduct();
  57. /**
  58. * Check is current product type is allowed for bundle selection product type
  59. */
  60. if (!in_array($product->getTypeId(), $this->bundleData->getAllowedSelectionTypes())) {
  61. return $this;
  62. }
  63. /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */
  64. $collection = $observer->getEvent()->getCollection();
  65. $limit = $observer->getEvent()->getLimit();
  66. if (is_array($limit)) {
  67. if (isset($limit['upsell'])) {
  68. $limit = $limit['upsell'];
  69. } else {
  70. $limit = 0;
  71. }
  72. }
  73. /* @var $resource \Magento\Bundle\Model\ResourceModel\Selection */
  74. $resource = $this->bundleSelection;
  75. $productIds = array_keys($collection->getItems());
  76. if ($limit !== null && $limit <= count($productIds)) {
  77. return $this;
  78. }
  79. // retrieve bundle product ids
  80. $bundleIds = $resource->getParentIdsByChild($product->getId());
  81. // exclude up-sell product ids
  82. $bundleIds = array_diff($bundleIds, $productIds);
  83. if (!$bundleIds) {
  84. return $this;
  85. }
  86. /* @var $bundleCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
  87. $bundleCollection = $product->getCollection()->addAttributeToSelect(
  88. $this->config->getProductAttributes()
  89. )->addStoreFilter()->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setVisibility(
  90. $this->productVisibility->getVisibleInCatalogIds()
  91. );
  92. if ($limit !== null) {
  93. $bundleCollection->setPageSize($limit);
  94. }
  95. $bundleCollection->addFieldToFilter(
  96. 'entity_id',
  97. ['in' => $bundleIds]
  98. )->setFlag(
  99. 'do_not_use_category_id',
  100. true
  101. );
  102. if ($collection instanceof \Magento\Framework\Data\Collection) {
  103. foreach ($bundleCollection as $item) {
  104. $collection->addItem($item);
  105. }
  106. } elseif ($collection instanceof \Magento\Framework\DataObject) {
  107. $items = $collection->getItems();
  108. foreach ($bundleCollection as $item) {
  109. $items[$item->getEntityId()] = $item;
  110. }
  111. $collection->setItems($items);
  112. }
  113. return $this;
  114. }
  115. }