Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AppendUpsellProductsObserver.php
Go to the documentation of this file.
1 <?php
7 
9 
11 {
17  protected $bundleData;
18 
22  protected $bundleSelection;
23 
27  protected $config;
28 
32  protected $productVisibility;
33 
40  public function __construct(
41  \Magento\Bundle\Helper\Data $bundleData,
42  \Magento\Catalog\Model\Product\Visibility $productVisibility,
43  \Magento\Catalog\Model\Config $config,
44  \Magento\Bundle\Model\ResourceModel\Selection $bundleSelection
45  ) {
46  $this->bundleData = $bundleData;
47  $this->productVisibility = $productVisibility;
48  $this->config = $config;
49  $this->bundleSelection = $bundleSelection;
50  }
51 
60  public function execute(\Magento\Framework\Event\Observer $observer)
61  {
62  /* @var $product \Magento\Catalog\Model\Product */
63  $product = $observer->getEvent()->getProduct();
64 
68  if (!in_array($product->getTypeId(), $this->bundleData->getAllowedSelectionTypes())) {
69  return $this;
70  }
71 
72  /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */
73  $collection = $observer->getEvent()->getCollection();
74  $limit = $observer->getEvent()->getLimit();
75  if (is_array($limit)) {
76  if (isset($limit['upsell'])) {
77  $limit = $limit['upsell'];
78  } else {
79  $limit = 0;
80  }
81  }
82 
83  /* @var $resource \Magento\Bundle\Model\ResourceModel\Selection */
85 
86  $productIds = array_keys($collection->getItems());
87  if ($limit !== null && $limit <= count($productIds)) {
88  return $this;
89  }
90 
91  // retrieve bundle product ids
92  $bundleIds = $resource->getParentIdsByChild($product->getId());
93  // exclude up-sell product ids
94  $bundleIds = array_diff($bundleIds, $productIds);
95 
96  if (!$bundleIds) {
97  return $this;
98  }
99 
100  /* @var $bundleCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
101  $bundleCollection = $product->getCollection()->addAttributeToSelect(
102  $this->config->getProductAttributes()
103  )->addStoreFilter()->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setVisibility(
104  $this->productVisibility->getVisibleInCatalogIds()
105  );
106 
107  if ($limit !== null) {
108  $bundleCollection->setPageSize($limit);
109  }
110  $bundleCollection->addFieldToFilter(
111  'entity_id',
112  ['in' => $bundleIds]
113  )->setFlag(
114  'do_not_use_category_id',
115  true
116  );
117 
118  if ($collection instanceof \Magento\Framework\Data\Collection) {
119  foreach ($bundleCollection as $item) {
120  $collection->addItem($item);
121  }
122  } elseif ($collection instanceof \Magento\Framework\DataObject) {
123  $items = $collection->getItems();
124  foreach ($bundleCollection as $item) {
125  $items[$item->getEntityId()] = $item;
126  }
127  $collection->setItems($items);
128  }
129 
130  return $this;
131  }
132 }
execute(\Magento\Framework\Event\Observer $observer)
__construct(\Magento\Bundle\Helper\Data $bundleData, \Magento\Catalog\Model\Product\Visibility $productVisibility, \Magento\Catalog\Model\Config $config, \Magento\Bundle\Model\ResourceModel\Selection $bundleSelection)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$resource
Definition: bulk.php:12
$items