/** * Get data * * @return array */ public function getData() { if (!$this->getCollection()->isLoaded()) { $this->getCollection()->addAttributeToFilter('type_id', $this->dataHelper->getAllowedSelectionTypes()); $this->getCollection()->addFilterByRequiredOptions(); $this->getCollection()->addStoreFilter(\Magento\Store\Model\Store::DEFAULT_STORE_ID); $this->getCollection()->load(); } $items = $this->getCollection()->toArray(); return ['totalRecords' => $this->getCollection()->getSize(), 'items' => array_values($items)]; }
/** * Append bundles in upsell list for current product * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { /* @var $product \Magento\Catalog\Model\Product */ $product = $observer->getEvent()->getProduct(); /** * Check is current product type is allowed for bundle selection product type */ if (!in_array($product->getTypeId(), $this->bundleData->getAllowedSelectionTypes())) { return $this; } /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */ $collection = $observer->getEvent()->getCollection(); $limit = $observer->getEvent()->getLimit(); if (is_array($limit)) { if (isset($limit['upsell'])) { $limit = $limit['upsell']; } else { $limit = 0; } } /* @var $resource \Magento\Bundle\Model\ResourceModel\Selection */ $resource = $this->bundleSelection; $productIds = array_keys($collection->getItems()); if ($limit !== null && $limit <= count($productIds)) { return $this; } // retrieve bundle product ids $bundleIds = $resource->getParentIdsByChild($product->getId()); // exclude up-sell product ids $bundleIds = array_diff($bundleIds, $productIds); if (!$bundleIds) { return $this; } /* @var $bundleCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $bundleCollection = $product->getCollection()->addAttributeToSelect($this->config->getProductAttributes())->addStoreFilter()->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setVisibility($this->productVisibility->getVisibleInCatalogIds()); if ($limit !== null) { $bundleCollection->setPageSize($limit); } $bundleCollection->addFieldToFilter('entity_id', ['in' => $bundleIds])->setFlag('do_not_use_category_id', true); if ($collection instanceof \Magento\Framework\Data\Collection) { foreach ($bundleCollection as $item) { $collection->addItem($item); } } elseif ($collection instanceof \Magento\Framework\DataObject) { $items = $collection->getItems(); foreach ($bundleCollection as $item) { $items[$item->getEntityId()] = $item; } $collection->setItems($items); } return $this; }
public function testGetData() { $items = ['testProduct1', 'testProduct2']; $expectedData = ['totalRecords' => count($items), 'items' => $items]; $this->dataHelperMock->expects($this->once())->method('getAllowedSelectionTypes')->willReturn([self::ALLOWED_TYPE]); $this->collectionMock->expects($this->once())->method('isLoaded')->willReturn(false); $this->collectionMock->expects($this->once())->method('addAttributeToFilter')->with('type_id', [self::ALLOWED_TYPE]); $this->collectionMock->expects($this->once())->method('addFilterByRequiredOptions'); $this->collectionMock->expects($this->once())->method('addStoreFilter')->with(\Magento\Store\Model\Store::DEFAULT_STORE_ID); $this->collectionMock->expects($this->once())->method('toArray')->willReturn($items); $this->collectionMock->expects($this->once())->method('getSize')->willReturn(count($items)); $this->assertEquals($expectedData, $this->getModel()->getData()); }
/** * Retrieve array of allowed product types for bundle selection product * * @return array */ public function getAllowedSelectionTypes() { return $this->_bundleData->getAllowedSelectionTypes(); }
public function testGetAllowedSelectionTypesIfTypesIsNotSet() { $configData = []; $this->config->expects($this->once())->method('getType')->with(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)->will($this->returnValue($configData)); $this->assertEquals([], $this->helper->getAllowedSelectionTypes()); }