/** * Populate data object using data in array format. * * @param mixed $dataObject * @param array $data * @param string $interfaceName * @return $this */ public function populateWithArray($dataObject, array $data, $interfaceName) { if ($dataObject instanceof ExtensibleDataInterface) { $data = $this->joinProcessor->extractExtensionAttributes(get_class($dataObject), $data); } $this->_setDataValues($dataObject, $data, $interfaceName); return $this; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria) { $quoteCollection = $this->quoteCollectionFactory->create(); $this->extensionAttributesJoinProcessor->process($quoteCollection); $searchData = $this->searchResultsDataFactory->create(); $searchData->setSearchCriteria($searchCriteria); $searchData->setItems($quoteCollection->getItems()); return $searchData; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria) { $this->quoteCollection = $this->getQuoteCollection(); /** @var \Magento\Quote\Api\Data\CartSearchResultsInterface $searchData */ $searchData = $this->searchResultsDataFactory->create(); $searchData->setSearchCriteria($searchCriteria); foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $this->quoteCollection); } $searchData->setTotalCount($this->quoteCollection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { /** @var SortOrder $sortOrder */ foreach ($sortOrders as $sortOrder) { $this->quoteCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $this->quoteCollection->setCurPage($searchCriteria->getCurrentPage()); $this->quoteCollection->setPageSize($searchCriteria->getPageSize()); $this->extensionAttributesJoinProcessor->process($this->quoteCollection); foreach ($this->quoteCollection->getItems() as $quote) { /** @var CartInterface $quote */ $this->getLoadHandler()->load($quote); } $searchData->setItems($this->quoteCollection->getItems()); return $searchData; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return \Magento\Bundle\Api\Data\OptionInterface[] */ public function getItems(\Magento\Catalog\Api\Data\ProductInterface $product) { $optionCollection = $this->type->getOptionsCollection($product); $this->extensionAttributesJoinProcessor->process($optionCollection); $optionList = []; /** @var \Magento\Bundle\Model\Option $option */ foreach ($optionCollection as $option) { $productLinks = $this->linkList->getItems($product, $option->getOptionId()); /** @var \Magento\Bundle\Api\Data\OptionInterface $optionDataObject */ $optionDataObject = $this->optionFactory->create(); $this->dataObjectHelper->populateWithArray($optionDataObject, $option->getData(), '\\Magento\\Bundle\\Api\\Data\\OptionInterface'); $optionDataObject->setOptionId($option->getOptionId())->setTitle($option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle())->setSku($product->getSku())->setProductLinks($productLinks); $optionList[] = $optionDataObject; } return $optionList; }
/** * Retrieve customers addresses matching the specified criteria. * * @param SearchCriteriaInterface $searchCriteria * @return \Magento\Customer\Api\Data\AddressSearchResultsInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function getList(SearchCriteriaInterface $searchCriteria) { $searchResults = $this->addressSearchResultsFactory->create(); /** @var Collection $collection */ $collection = $this->addressCollectionFactory->create(); $this->extensionAttributesJoinProcessor->process($collection, 'Magento\\Customer\\Api\\Data\\AddressInterface'); // Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $searchResults->setTotalCount($collection->getSize()); /** @var SortOrder $sortOrder */ foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) { $field = $sortOrder->getField(); $collection->addOrder($field, $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = []; /** @var \Magento\Customer\Model\Address $address */ foreach ($collection->getItems() as $address) { $addresses[] = $this->getById($address->getId()); } $searchResults->setItems($addresses); $searchResults->setSearchCriteria($searchCriteria); return $searchResults; }
/** * @param int $direction * @param string $expectedDirection * @dataProvider getListSuccessDataProvider */ public function testGetListSuccess($direction, $expectedDirection) { $searchResult = $this->getMock('\\Magento\\Quote\\Api\\Data\\CartSearchResultsInterface', [], [], '', false); $searchCriteriaMock = $this->getMock('\\Magento\\Framework\\Api\\SearchCriteria', [], [], '', false); $cartMock = $this->getMock('Magento\\Payment\\Model\\Cart', [], [], '', false); $filterMock = $this->getMock('\\Magento\\Framework\\Api\\Filter', [], [], '', false); $pageSize = 10; $this->searchResultsDataFactory->expects($this->once())->method('create')->will($this->returnValue($searchResult)); $searchResult->expects($this->once())->method('setSearchCriteria'); $filterGroupMock = $this->getMock('\\Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $searchCriteriaMock->expects($this->any())->method('getFilterGroups')->will($this->returnValue([$filterGroupMock])); //addFilterGroupToCollection() checks $filterGroupMock->expects($this->any())->method('getFilters')->will($this->returnValue([$filterMock])); $filterMock->expects($this->once())->method('getField')->will($this->returnValue('store_id')); $filterMock->expects($this->any())->method('getConditionType')->will($this->returnValue('eq')); $filterMock->expects($this->once())->method('getValue')->will($this->returnValue('filter_value')); //back in getList() $this->quoteCollectionMock->expects($this->once())->method('getSize')->willReturn($pageSize); $searchResult->expects($this->once())->method('setTotalCount')->with($pageSize); $sortOrderMock = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->setMethods(['getField', 'getDirection'])->disableOriginalConstructor()->getMock(); //foreach cycle $searchCriteriaMock->expects($this->once())->method('getSortOrders')->will($this->returnValue([$sortOrderMock])); $sortOrderMock->expects($this->once())->method('getField')->will($this->returnValue('id')); $sortOrderMock->expects($this->once())->method('getDirection')->will($this->returnValue($direction)); $this->quoteCollectionMock->expects($this->once())->method('addOrder')->with('id', $expectedDirection); $searchCriteriaMock->expects($this->once())->method('getCurrentPage')->will($this->returnValue(1)); $searchCriteriaMock->expects($this->once())->method('getPageSize')->will($this->returnValue(10)); $this->quoteCollectionMock->expects($this->once())->method('setCurPage')->with(1); $this->quoteCollectionMock->expects($this->once())->method('setPageSize')->with(10); $this->extensionAttributesJoinProcessorMock->expects($this->once())->method('process')->with($this->isInstanceOf('\\Magento\\Quote\\Model\\ResourceModel\\Quote\\Collection')); $this->quoteCollectionMock->expects($this->once())->method('getItems')->willReturn([$cartMock]); $searchResult->expects($this->once())->method('setItems')->with([$cartMock]); $this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock)); }
/** * Retrieve coupon. * * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria * @return \Magento\SalesRule\Api\Data\CouponSearchResultInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $collection */ $collection = $this->collectionFactory->create(); $couponInterfaceName = 'Magento\\SalesRule\\Api\\Data\\CouponInterface'; $this->extensionAttributesJoinProcessor->process($collection, $couponInterfaceName); //Add filters from root filter group to the collection /** @var FilterGroup $group */ foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders === null) { $sortOrders = []; } /** @var \Magento\Framework\Api\SortOrder $sortOrder */ foreach ($sortOrders as $sortOrder) { $field = $sortOrder->getField(); $collection->addOrder($field, $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $coupons = []; /** @var \Magento\SalesRule\Model\Coupon $couponModel */ foreach ($collection->getItems() as $couponModel) { $coupons[] = $couponModel->getData(); } $searchResults = $this->searchResultFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($coupons); $searchResults->setTotalCount($collection->getSize()); return $searchResults; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria) { $searchResults = $this->taxRuleSearchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $fields = []; $collection = $this->collectionFactory->create(); $this->joinProcessor->process($collection); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); foreach ($group->getFilters() as $filter) { $fields[] = $this->translateField($filter->getField()); } } if ($fields) { if (in_array('cd.customer_tax_class_id', $fields) || in_array('cd.product_tax_class_id', $fields)) { $collection->joinCalculationData('cd'); } } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($sortOrders as $sortOrder) { $collection->addOrder($this->translateField($sortOrder->getField()), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $searchResults->setItems($collection->getItems()); return $searchResults; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\Tax\Model\ResourceModel\Calculation\Rate\Collection $collection */ $collection = $this->rateFactory->create()->getCollection(); $this->joinProcessor->process($collection); $collection->joinRegionTable(); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($sortOrders as $sortOrder) { $collection->addOrder($this->translateField($sortOrder->getField()), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $taxRate = []; /** @var \Magento\Tax\Model\Calculation\Rate $taxRateModel */ foreach ($collection as $taxRateModel) { $taxRate[] = $taxRateModel; } return $this->taxRateSearchResultsFactory->create()->setItems($taxRate)->setTotalCount($collection->getSize())->setSearchCriteria($searchCriteria); }
/** * Get downloadable product samples * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Downloadable\Model\Resource\Sample\Collection */ public function getSamples($product) { if ($product->getDownloadableSamples() === null) { $sampleCollection = $this->_samplesFactory->create()->addProductToFilter($product->getId())->addTitleToResult($product->getStoreId()); $this->extensionAttributesJoinProcessor->process($sampleCollection); $product->setDownloadableSamples($sampleCollection); } return $product->getDownloadableSamples(); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $collection */ $collection = $this->collectionFactory->create(); $this->joinProcessor->process($collection); /** The only possible/meaningful search criteria for attribute set is entity type code */ $entityTypeCode = $this->getEntityTypeCode($searchCriteria); if ($entityTypeCode !== null) { $collection->setEntityTypeFilter($this->eavConfig->getEntityType($entityTypeCode)->getId()); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($collection->getItems()); $searchResults->setTotalCount($collection->getSize()); return $searchResults; }
/** * Fetch collection data * * @param Select $select * @return array */ protected function _fetchAll(Select $select) { $data = $this->_fetchStrategy->fetchAll($select, $this->_bindParams); if ($this->extensionAttributesJoinProcessor) { foreach ($data as $key => $dataItem) { $data[$key] = $this->extensionAttributesJoinProcessor->extractExtensionAttributes($this->_itemObjectClass, $dataItem); } } return $data; }
/** * @param int $productId * @param int $storeId * @param bool $requiredOnly * @return \Magento\Catalog\Api\Data\ProductCustomOptionInterface[] */ public function getProductOptions($productId, $storeId, $requiredOnly = false) { $collection = $this->addFieldToFilter('cpe.entity_id', $productId)->addTitleToResult($storeId)->addPriceToResult($storeId)->setOrder('sort_order', 'asc')->setOrder('title', 'asc'); if ($requiredOnly) { $collection->addRequiredFilter(); } $collection->addValuesToResult($storeId); $this->joinProcessor->process($collection); return $collection->getItems(); }
/** * {@inheritdoc} */ public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { if (!$entityTypeCode) { throw InputException::requiredField('entity_type_code'); } /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection */ $attributeCollection = $this->attributeCollectionFactory->create(); $this->joinProcessor->process($attributeCollection); $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]); $attributeCollection->join(['entity_type' => $attributeCollection->getTable('eav_entity_type')], 'main_table.entity_type_id = entity_type.entity_type_id', []); $attributeCollection->joinLeft(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []); $entityType = $this->eavConfig->getEntityType($entityTypeCode); $additionalTable = $entityType->getAdditionalAttributeTable(); if ($additionalTable) { $attributeCollection->join(['additional_table' => $attributeCollection->getTable($additionalTable)], 'main_table.attribute_id = additional_table.attribute_id', []); } //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $attributeCollection); } /** @var SortOrder $sortOrder */ foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) { $attributeCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } $totalCount = $attributeCollection->getSize(); // Group attributes by id to prevent duplicates with different attribute sets $attributeCollection->addAttributeGrouping(); $attributeCollection->setCurPage($searchCriteria->getCurrentPage()); $attributeCollection->setPageSize($searchCriteria->getPageSize()); $attributes = []; /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */ foreach ($attributeCollection as $attribute) { $attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode()); } $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($attributes); $searchResults->setTotalCount($totalCount); return $searchResults; }
/** * @param ProductInterface $product * @return OptionInterface[] */ public function load(ProductInterface $product) { $options = []; /** @var Configurable $typeInstance */ $typeInstance = $product->getTypeInstance(); $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product); $this->extensionAttributesJoinProcessor->process($attributeCollection); foreach ($attributeCollection as $attribute) { $values = []; $attributeOptions = $attribute->getOptions(); if (is_array($attributeOptions)) { foreach ($attributeOptions as $option) { /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */ $value = $this->optionValueFactory->create(); $value->setValueIndex($option['value_index']); $values[] = $value; } } $attribute->setValues($values); $options[] = $attribute; } return $options; }
/** * Find entities by criteria * * @param \Magento\Framework\Api\SearchCriteria $searchCriteria * @return \Magento\Sales\Api\Data\ShipmentItemInterface[] */ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria) { $collection = $this->shipmentItemInterfaceSearchResultFactory->create(); $this->extensionAttributesJoinProcessor->process($collection); foreach ($searchCriteria->getFilterGroups() as $filterGroup) { foreach ($filterGroup->getFilters() as $filter) { $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); return $collection; }
/** * {@inheritdoc} * * @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[] Array of checkout agreement data objects. */ public function getList() { if (!$this->scopeConfig->isSetFlag('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE)) { return []; } $storeId = $this->storeManager->getStore()->getId(); /** @var $agreementCollection AgreementCollection */ $agreementCollection = $this->collectionFactory->create(); $this->extensionAttributesJoinProcessor->process($agreementCollection); $agreementCollection->addStoreFilter($storeId); $agreementCollection->addFieldToFilter('is_active', 1); $agreementDataObjects = []; foreach ($agreementCollection as $agreement) { $agreementDataObjects[] = $agreement; } return $agreementDataObjects; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetList() { $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false); $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false); $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection', [], [], '', false); $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false); $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false); $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['getId', 'setId', 'setStoreId', 'getStoreId', 'getAttributeSetId', 'setAttributeSetId', 'setRpToken', 'setRpTokenCreatedAt', 'getDataModel', 'setPasswordHash', 'getCollection'], [], 'customerModel', false); $metadata = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], '', false); $this->searchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults); $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel); $customerModel->expects($this->once())->method('getCollection')->willReturn($collection); $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([$metadata]); $metadata->expects($this->once())->method('getAttributeCode')->willReturn('attribute-code'); $collection->expects($this->once())->method('addAttributeToSelect')->with('attribute-code'); $collection->expects($this->once())->method('addNameToSelect'); $collection->expects($this->at(2))->method('joinAttribute')->with('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(3))->method('joinAttribute')->with('billing_city', 'customer_address/city', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(4))->method('joinAttribute')->with('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(5))->method('joinAttribute')->with('billing_region', 'customer_address/region', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(6))->method('joinAttribute')->with('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(7))->method('joinAttribute')->with('company', 'customer_address/company', 'default_billing', null, 'left')->willReturnSelf(); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], []); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $filter->expects($this->once())->method('getConditionType')->willReturn(false); $filter->expects($this->once())->method('getField')->willReturn('Field'); $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value'); $collection->expects($this->once())->method('addOrder')->with('Field', 'ASC'); $searchCriteria->expects($this->atLeastOnce())->method('getSortOrders')->willReturn([$sortOrder]); $sortOrder->expects($this->once())->method('getField')->willReturn('Field'); $sortOrder->expects($this->once())->method('getDirection')->willReturn(\Magento\Framework\Api\SortOrder::SORT_ASC); $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1); $collection->expects($this->once())->method('setCurPage')->with(1); $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10); $collection->expects($this->once())->method('setPageSize')->with(10); $collection->expects($this->once())->method('getSize')->willReturn(23); $searchResults->expects($this->once())->method('setTotalCount')->with(23); $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$customerModel])); $customerModel->expects($this->atLeastOnce())->method('getDataModel')->willReturn($this->customer); $searchResults->expects($this->once())->method('setItems')->with([$this->customer]); $this->assertSame($searchResults, $this->model->getList($searchCriteria)); }
/** * {@inheritdoc} */ public function getList(SearchCriteriaInterface $searchCriteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); /** @var \Magento\Customer\Model\Resource\Group\Collection $collection */ $collection = $this->groupFactory->create()->getCollection(); $groupInterfaceName = 'Magento\\Customer\\Api\\Data\\GroupInterface'; $this->extensionAttributesJoinProcessor->process($collection, $groupInterfaceName); $collection->addTaxClass(); //Add filters from root filter group to the collection /** @var FilterGroup $group */ foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $sortOrders = $searchCriteria->getSortOrders(); /** @var \Magento\Framework\Api\SortOrder $sortOrder */ if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $sortOrder) { $field = $this->translateField($sortOrder->getField()); $collection->addOrder($field, $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC'); } } else { // set a default sorting order since this method is used constantly in many // different blocks $field = $this->translateField('id'); $collection->addOrder($field, 'ASC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); /** @var \Magento\Customer\Api\Data\GroupInterface[] $groups */ $groups = []; /** @var \Magento\Customer\Model\Group $group */ foreach ($collection as $group) { /** @var \Magento\Customer\Api\Data\GroupInterface $groupDataObject */ $groupDataObject = $this->groupDataFactory->create()->setId($group->getId())->setCode($group->getCode())->setTaxClassId($group->getTaxClassId())->setTaxClassName($group->getTaxClassName()); $data = $group->getData(); $data = $this->extensionAttributesJoinProcessor->extractExtensionAttributes($groupInterfaceName, $data); if (isset($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]) && $data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY] instanceof GroupExtensionInterface) { $groupDataObject->setExtensionAttributes($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]); } $groups[] = $groupDataObject; } $searchResults->setTotalCount($collection->getSize()); return $searchResults->setItems($groups); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetListWithoutSortOrder() { $groupId = 86; $groupExtension = $this->getMock('Magento\\Customer\\Api\\Data\\GroupExtensionInterface', [], [], '', false); $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false); $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Group\\Collection', [], [], '', false); $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false); $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false); $this->searchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults); $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); $this->groupFactory->expects($this->once())->method('create')->willReturn($this->groupModel); $this->groupModel->expects($this->once())->method('getCollection')->willReturn($collection); $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\GroupInterface'); $collection->expects($this->once())->method('addTaxClass'); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $filter->expects($this->once())->method('getConditionType')->willReturn(false); $filter->expects($this->once())->method('getField')->willReturn('Field'); $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value'); $collection->expects($this->once())->method('addFieldToFilter')->with(['Field'], [['eq' => 'Value']]); $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1); $collection->expects($this->once())->method('setCurPage')->with(1); $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10); $collection->expects($this->once())->method('setPageSize')->with(10); $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupModel])); $this->groupDataFactory->expects($this->once())->method('create')->willReturn($this->group); $this->group->expects($this->once())->method('setId')->with($groupId)->willReturnSelf(); $this->group->expects($this->once())->method('setCode')->with('Code')->willReturnSelf(); $this->group->expects($this->once())->method('setTaxClassId')->with(234)->willReturnSelf(); $this->group->expects($this->once())->method('setTaxClassName')->with('Tax class name')->willReturnSelf(); $this->groupModel->expects($this->atLeastOnce())->method('getId')->willReturn($groupId); $this->groupModel->expects($this->atLeastOnce())->method('getCode')->willReturn('Code'); $this->groupModel->expects($this->atLeastOnce())->method('getTaxClassId')->willReturn(234); $this->groupModel->expects($this->atLeastOnce())->method('getTaxClassName')->willReturn('Tax class name'); $this->groupModel->expects($this->once())->method('getData')->willReturn([]); $this->extensionAttributesJoinProcessor->expects($this->once())->method('extractExtensionAttributes')->with('Magento\\Customer\\Api\\Data\\GroupInterface', [])->willReturn(['extension_attributes' => $groupExtension]); $this->group->expects($this->once())->method('setExtensionAttributes')->with($groupExtension); $collection->expects($this->once())->method('getSize')->willReturn(9); $searchResults->expects($this->once())->method('setTotalCount')->with(9); $searchResults->expects($this->once())->method('setItems')->with([$this->group])->willReturnSelf(); $collection->expects($this->once())->method('addOrder')->with('customer_group_id', 'ASC'); $this->assertSame($searchResults, $this->model->getList($searchCriteria)); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria) { $searchData = $this->searchResultsDataFactory->create(); $searchData->setSearchCriteria($searchCriteria); foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $this->quoteCollection); } $searchData->setTotalCount($this->quoteCollection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { foreach ($sortOrders as $sortOrder) { $this->quoteCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $this->quoteCollection->setCurPage($searchCriteria->getCurrentPage()); $this->quoteCollection->setPageSize($searchCriteria->getPageSize()); $this->extensionAttributesJoinProcessor->process($this->quoteCollection); $searchData->setItems($this->quoteCollection->getItems()); return $searchData; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { $attributeSetId = $this->retrieveAttributeSetIdFromSearchCriteria($searchCriteria); if (!$attributeSetId) { throw InputException::requiredField('attribute_set_id'); } try { $this->setRepository->get($attributeSetId); } catch (\Exception $exception) { throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } $collection = $this->groupListFactory->create(); $this->joinProcessor->process($collection); $collection->setAttributeSetFilter($attributeSetId); $collection->setSortOrder(); $searchResult = $this->searchResultsFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($collection->getItems()); $searchResult->setTotalCount($collection->getSize()); return $searchResult; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); /** @var TaxClassCollection $collection */ $collection = $this->taxClassCollectionFactory->create(); $this->joinProcessor->process($collection); foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $searchResults->setItems($collection->getItems()); return $searchResults; }
/** * {@inheritdoc} */ public function getList(SearchCriteriaInterface $searchCriteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); /** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */ $collection = $this->customerFactory->create()->getCollection(); $this->extensionAttributesJoinProcessor->process($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); // This is needed to make sure all the attributes are properly loaded foreach ($this->customerMetadata->getAllAttributesMetadata() as $metadata) { $collection->addAttributeToSelect($metadata->getAttributeCode()); } // Needed to enable filtering on name as a whole $collection->addNameToSelect(); // Needed to enable filtering based on billing address attributes $collection->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left'); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { /** @var SortOrder $sortOrder */ foreach ($searchCriteria->getSortOrders() as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $customers = []; /** @var \Magento\Customer\Model\Customer $customerModel */ foreach ($collection as $customerModel) { $customers[] = $customerModel->getDataModel(); } $searchResults->setItems($customers); return $searchResults; }
public function testGetList() { $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false); $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Address\\Collection', [], [], '', false); $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false); $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false); $this->addressSearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults); $this->addressCollectionFactory->expects($this->once())->method('create')->willReturn($collection); $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\AddressInterface'); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $filter->expects($this->once())->method('getConditionType')->willReturn(false); $filter->expects($this->once())->method('getField')->willReturn('Field'); $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value'); $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], [['eq' => 'Value']]); $collection->expects($this->once())->method('getSize')->willReturn(23); $searchResults->expects($this->once())->method('setTotalCount')->with(23); $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false); $searchCriteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]); $sortOrder->expects($this->once())->method('getField')->willReturn('Field'); $sortOrder->expects($this->once())->method('getDirection')->willReturn(\Magento\Framework\Api\SortOrder::SORT_ASC); $collection->expects($this->once())->method('addOrder')->with('Field', 'ASC'); $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1); $collection->expects($this->once())->method('setCurPage')->with(1); $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10); $collection->expects($this->once())->method('setPageSize')->with(10); $collection->expects($this->once())->method('getItems')->willReturn([$this->address]); $this->address->expects($this->once())->method('getId')->willReturn(12); $customerAddress = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false); $this->addressRegistry->expects($this->once())->method('retrieve')->with(12)->willReturn($this->address); $this->address->expects($this->once())->method('getDataModel')->willReturn($customerAddress); $searchResults->expects($this->once())->method('setItems')->with([$customerAddress]); $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); $this->assertSame($searchResults, $this->repository->getList($searchCriteria)); }
/** * Retrieve configurable attributes data * * @param \Magento\Catalog\Model\Product $product * @return \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute[] */ public function getConfigurableAttributes($product) { \Magento\Framework\Profiler::start('CONFIGURABLE:' . __METHOD__, ['group' => 'CONFIGURABLE', 'method' => __METHOD__]); if (!$product->hasData($this->_configurableAttributes)) { $configurableAttributes = $this->getConfigurableAttributeCollection($product); $this->extensionAttributesJoinProcessor->process($configurableAttributes); $configurableAttributes->orderByPosition()->load(); $product->setData($this->_configurableAttributes, $configurableAttributes); } \Magento\Framework\Profiler::stop('CONFIGURABLE:' . __METHOD__); return $product->getData($this->_configurableAttributes); }
/** * Retrieve configurable attributes data * * @param \Magento\Catalog\Model\Product $product * @return \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute[] */ public function getConfigurableAttributes($product) { \Magento\Framework\Profiler::start('CONFIGURABLE:' . __METHOD__, ['group' => 'CONFIGURABLE', 'method' => __METHOD__]); if (!$product->hasData($this->_configurableAttributes)) { $cacheId = __CLASS__ . $product->getId(); $configurableAttributes = $this->cache->load($cacheId); if ($configurableAttributes) { $configurableAttributes = unserialize($configurableAttributes); $configurableAttributes->setProductFilter($product); } else { $configurableAttributes = $this->getConfigurableAttributeCollection($product); $this->extensionAttributesJoinProcessor->process($configurableAttributes); $configurableAttributes->orderByPosition()->load(); $this->cache->save(serialize($configurableAttributes), $cacheId, $product->getIdentities()); } $product->setData($this->_configurableAttributes, $configurableAttributes); } \Magento\Framework\Profiler::stop('CONFIGURABLE:' . __METHOD__); return $product->getData($this->_configurableAttributes); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */ $collection = $this->collectionFactory->create(); $this->extensionAttributesJoinProcessor->process($collection); foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) { $collection->addAttributeToSelect($metadata->getAttributeCode()); } $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } /** @var SortOrder $sortOrder */ foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) { $field = $sortOrder->getField(); $collection->addOrder($field, $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $collection->load(); $searchResult = $this->searchResultsFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($collection->getItems()); $searchResult->setTotalCount($collection->getSize()); return $searchResult; }
/** * Get all options of product * * @return \Magento\Catalog\Api\Data\ProductCustomOptionInterface[]|null */ public function getOptions() { if (empty($this->_options) && $this->getHasOptions() && !$this->optionsInitialized) { $collection = $this->getProductOptionsCollection(); $this->joinProcessor->process($collection); foreach ($collection as $option) { $option->setProduct($this); $this->addOption($option); } $this->optionsInitialized = true; } return $this->_options; }
/** * Retrieve quote items collection * * @param bool $useCache * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function getItemsCollection($useCache = true) { if ($this->hasItemsCollection()) { return $this->getData('items_collection'); } if (null === $this->_items) { $this->_items = $this->_quoteItemCollectionFactory->create(); $this->extensionAttributesJoinProcessor->process($this->_items); $this->_items->setQuote($this); } return $this->_items; }