public function testGetSelectionsCollection() { $optionIds = [1, 2, 3]; $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'getStoreId', 'getData', 'hasData', 'setData', 'getId'])->getMock(); $selectionCollection = $this->getMockBuilder('\\Magento\\Bundle\\Model\\ResourceModel\\Selection\\Collection')->disableOriginalConstructor()->setMethods(['addAttributeToSelect', 'setFlag', 'setPositionOrder', 'addStoreFilter', 'setStoreId', 'addFilterByRequiredOptions', 'setOptionIdsFilter', 'joinPrices'])->getMock(); $store = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getWebsiteId'])->getMock(); $product->expects($this->once())->method('hasData')->with('_cache_instance_selections_collection1_2_3')->willReturn(false); $product->expects($this->once())->method('getStoreId')->willReturn('store_id'); $product->expects($this->at(2))->method('getData')->with('_cache_instance_store_filter')->willReturn($selectionCollection); $this->bundleCollection->expects($this->once())->method('create')->willReturn($selectionCollection); $selectionCollection->expects($this->any())->method('addAttributeToSelect')->willReturnSelf(); $selectionCollection->expects($this->any())->method('setFlag')->willReturnSelf(); $selectionCollection->expects($this->any())->method('setPositionOrder')->willReturnSelf(); $selectionCollection->expects($this->any())->method('addStoreFilter')->willReturnSelf(); $selectionCollection->expects($this->any())->method('setStoreId')->willReturnSelf(); $selectionCollection->expects($this->any())->method('addFilterByRequiredOptions')->willReturnSelf(); $selectionCollection->expects($this->any())->method('setOptionIdsFilter')->willReturnSelf(); $this->storeManager->expects($this->once())->method('getStore')->willReturn($store); $store->expects($this->once())->method('getWebsiteId')->willReturn('website_id'); $selectionCollection->expects($this->any())->method('joinPrices')->with('website_id')->willReturnSelf(); $product->expects($this->once())->method('setData')->with('_cache_instance_selections_collection1_2_3', $selectionCollection)->willReturnSelf(); $product->expects($this->at(4))->method('getData')->with('_cache_instance_selections_collection1_2_3')->willReturn($selectionCollection); $this->assertEquals($selectionCollection, $this->model->getSelectionsCollection($optionIds, $product)); }
/** * Retrieve bundle selections collection based on ids * * @param array $selectionIds * @param \Magento\Catalog\Model\Product $product * @return \Magento\Bundle\Model\ResourceModel\Selection\Collection */ public function getSelectionsByIds($selectionIds, $product) { sort($selectionIds); $usedSelections = $product->getData($this->_keyUsedSelections); $usedSelectionsIds = $product->getData($this->_keyUsedSelectionsIds); if (!$usedSelections || $usedSelectionsIds !== $selectionIds) { $storeId = $product->getStoreId(); $usedSelections = $this->_bundleCollection->create()->addAttributeToSelect('*')->setFlag('require_stock_items', true)->setFlag('product_children', true)->addStoreFilter($this->getStoreFilter($product))->setStoreId($storeId)->setPositionOrder()->addFilterByRequiredOptions()->setSelectionIdsFilter($selectionIds); if (!$this->_catalogData->isPriceGlobal() && $storeId) { $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); $usedSelections->joinPrices($websiteId); } $product->setData($this->_keyUsedSelections, $usedSelections); $product->setData($this->_keyUsedSelectionsIds, $selectionIds); } return $usedSelections; }
/** * Retrieve bundle selections collection based on ids * * @param array $selectionIds * @param \Magento\Catalog\Model\Product $product * @return \Magento\Bundle\Model\ResourceModel\Selection\Collection */ public function getSelectionsByIds($selectionIds, $product) { sort($selectionIds); $usedSelections = $product->getData($this->_keyUsedSelections); $usedSelectionsIds = $product->getData($this->_keyUsedSelectionsIds); if (!$usedSelections || $usedSelectionsIds !== $selectionIds) { $storeId = $product->getStoreId(); $usedSelections = $this->_bundleCollection->create()->addAttributeToSelect('*')->setFlag('require_stock_items', true)->setFlag('product_children', true)->addStoreFilter($this->getStoreFilter($product))->setStoreId($storeId)->setPositionOrder()->addFilterByRequiredOptions()->setSelectionIdsFilter($selectionIds); if (count($usedSelections->getItems()) !== count($selectionIds)) { throw new \Magento\Framework\Exception\LocalizedException(__('The options you selected are not available.')); } if (!$this->_catalogData->isPriceGlobal() && $storeId) { $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); $usedSelections->joinPrices($websiteId); } $product->setData($this->_keyUsedSelections, $usedSelections); $product->setData($this->_keyUsedSelectionsIds, $selectionIds); } return $usedSelections; }