/** * @param AbstractCollection $collection * * @return $this */ public function applyToCollection($collection) { if ($this->getTable() && $this->getPkFieldName()) { $collection->joinTable($this->getTable(), $this->getPkFieldName() . '=entity_id', ['affected_product_id' => $this->getPkFieldName()]); } return $this; }
/** * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection * @param \Magento\Catalog\Block\Product\ListProduct $subject * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection */ public function afterGetLoadedProductCollection(\Magento\Catalog\Block\Product\ListProduct $subject, $resultCollection) { if ($resultCollection->count() == 1) { /** @var \Magento\Catalog\Model\Product $product */ $product = $resultCollection->getFirstItem(); $this->response->setRedirect($product->getProductUrl()); } return $resultCollection; }
public function testAddValueSortToCollectionNotGlobal() { $this->attributeModel->expects($this->any())->method('isScopeGlobal')->will($this->returnValue(false)); $this->collection->expects($this->once())->method('order')->with('check_sql asc')->will($this->returnSelf()); $this->collection->expects($this->once())->method('getStoreId')->will($this->returnValue(1)); $this->collection->expects($this->any())->method('getConnection')->will($this->returnSelf()); $this->collection->expects($this->any())->method('getCheckSql')->will($this->returnValue('check_sql')); $this->status->setAttribute($this->attributeModel); $this->status->addValueSortToCollection($this->collection); }
/** * Add Join with option value for collection select * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @param \Magento\Eav\Model\Entity\Attribute $attribute * @param \Zend_Db_Expr $valueExpr * @return $this */ public function addOptionValueToCollection($collection, $attribute, $valueExpr) { $adapter = $this->_getReadAdapter(); $attributeCode = $attribute->getAttributeCode(); $optionTable1 = $attributeCode . '_option_value_t1'; $optionTable2 = $attributeCode . '_option_value_t2'; $tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr} AND {$optionTable1}.store_id=0"; $tableJoinCond2 = $adapter->quoteInto("{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?", $collection->getStoreId()); $valueExpr = $adapter->getCheckSql("{$optionTable2}.value_id IS NULL", "{$optionTable1}.value", "{$optionTable2}.value"); $collection->getSelect()->joinLeft([$optionTable1 => $this->getTable('eav_attribute_option_value')], $tableJoinCond1, [])->joinLeft([$optionTable2 => $this->getTable('eav_attribute_option_value')], $tableJoinCond2, [$attributeCode => $valueExpr]); return $this; }
/** * @param AbstractCollection $collection * @return \Magento\Backend\Model\View\Result\Redirect */ protected function massAction(AbstractCollection $collection) { $customersDeleted = 0; foreach ($collection->getAllIds() as $customerId) { $this->customerRepository->deleteById($customerId); $customersDeleted++; } if ($customersDeleted) { $this->messageManager->addSuccess(__('A total of %1 record(s) were deleted.', $customersDeleted)); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setPath($this->getComponentRefererUrl()); return $resultRedirect; }
public function testExportCurPageEqualToLastBreakInternalCalls() { $curPage = $lastPage = $page = 1; $itemsPerPage = 10; $this->product->expects($this->once())->method('getWriter')->willReturn($this->writer); $this->product->expects($this->exactly(1))->method('_getEntityCollection')->willReturn($this->abstractCollection); $this->product->expects($this->once())->method('_prepareEntityCollection')->with($this->abstractCollection); $this->product->expects($this->once())->method('getItemsPerPage')->willReturn($itemsPerPage); $this->product->expects($this->once())->method('paginateCollection')->with($page, $itemsPerPage); $this->abstractCollection->expects($this->once())->method('setOrder')->with('has_options', 'asc'); $this->abstractCollection->expects($this->once())->method('setStoreId')->with(Store::DEFAULT_STORE_ID); $this->abstractCollection->expects($this->once())->method('count')->willReturn(1); $this->abstractCollection->expects($this->once())->method('getCurPage')->willReturn($curPage); $this->abstractCollection->expects($this->once())->method('getLastPageNumber')->willReturn($lastPage); $headers = ['headers']; $this->product->expects($this->once())->method('_getHeaderColumns')->willReturn($headers); $this->writer->expects($this->once())->method('setHeaderCols')->with($headers); $row = 'value'; $data = [$row]; $this->product->expects($this->once())->method('getExportData')->willReturn($data); $customFieldsMappingResult = ['result']; $this->product->expects($this->once())->method('_customFieldsMapping')->with($row)->willReturn($customFieldsMappingResult); $this->writer->expects($this->once())->method('writeRow')->with($customFieldsMappingResult); $this->writer->expects($this->once())->method('getContents'); $this->product->export(); }
/** * Test export for current page */ public function testExportCurrentPageCalls() { $curPage = $lastPage = $page = 1; $itemsPerPage = 10; $this->advancedPricing->expects($this->once())->method('getWriter')->willReturn($this->writer); $this->advancedPricing->expects($this->exactly(1))->method('_getEntityCollection')->willReturn($this->abstractCollection); $this->advancedPricing->expects($this->once())->method('_prepareEntityCollection')->with($this->abstractCollection); $this->advancedPricing->expects($this->once())->method('getItemsPerPage')->willReturn($itemsPerPage); $this->advancedPricing->expects($this->once())->method('paginateCollection')->with($page, $itemsPerPage); $this->abstractCollection->expects($this->once())->method('setOrder')->with('has_options', 'asc'); $this->abstractCollection->expects($this->once())->method('setStoreId')->with(Store::DEFAULT_STORE_ID); $this->abstractCollection->expects($this->once())->method('count')->willReturn(1); $this->abstractCollection->expects($this->once())->method('getCurPage')->willReturn($curPage); $this->abstractCollection->expects($this->once())->method('getLastPageNumber')->willReturn($lastPage); $headers = ['headers']; $this->advancedPricing->expects($this->any())->method('_getHeaderColumns')->willReturn($headers); $this->writer->expects($this->any())->method('setHeaderCols')->with($headers); $webSite = 'All Websites [USD]'; $userGroup = 'General'; $this->advancedPricing->expects($this->any())->method('_getWebsiteCode')->willReturn($webSite); $this->advancedPricing->expects($this->any())->method('_getCustomerGroupById')->willReturn($userGroup); $data = [['sku' => 'simpletest', 'group_price_website' => $webSite, 'group_price_customer_group' => $userGroup, 'group_price' => '100', 'tier_price_website' => $webSite, 'tier_price_customer_group' => $userGroup, 'tier_price_qty' => '2', 'tier_price' => '23']]; $this->advancedPricing->expects($this->once())->method('getExportData')->willReturn($data); $exportData = ['sku' => 'simpletest', 'group_price_website' => $webSite, 'group_price_customer_group' => $userGroup, 'group_price' => '100', 'tier_price_website' => $webSite, 'tier_price_customer_group' => $userGroup, 'tier_price_qty' => '2', 'tier_price' => '23']; $this->advancedPricing->expects($this->any())->method('correctExportData')->willReturn($exportData); $this->writer->expects($this->once())->method('writeRow')->with($exportData); $this->writer->expects($this->once())->method('getContents'); $this->advancedPricing->export(); }
/** * @inheritdoc */ public function fetchItem() { $item = parent::fetchItem(); if ($item) { $this->entitySnapshot->registerSnapshot($item); } return $item; }
/** * Add Value Sort To Collection Select * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @param string $dir direction * @return \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource */ public function addValueSortToCollection($collection, $dir = 'asc') { $attributeCode = $this->getAttribute()->getAttributeCode(); $attributeId = $this->getAttribute()->getId(); $attributeTable = $this->getAttribute()->getBackend()->getTable(); if ($this->getAttribute()->isScopeGlobal()) { $tableName = $attributeCode . '_t'; $collection->getSelect()->joinLeft([$tableName => $attributeTable], "e.entity_id={$tableName}.entity_id" . " AND {$tableName}.attribute_id='{$attributeId}'" . " AND {$tableName}.store_id='0'", []); $valueExpr = $tableName . '.value'; } else { $valueTable1 = $attributeCode . '_t1'; $valueTable2 = $attributeCode . '_t2'; $collection->getSelect()->joinLeft([$valueTable1 => $attributeTable], "e.entity_id={$valueTable1}.entity_id" . " AND {$valueTable1}.attribute_id='{$attributeId}'" . " AND {$valueTable1}.store_id='0'", [])->joinLeft([$valueTable2 => $attributeTable], "e.entity_id={$valueTable2}.entity_id" . " AND {$valueTable2}.attribute_id='{$attributeId}'" . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'", []); $valueExpr = $collection->getConnection()->getCheckSql($valueTable2 . '.value_id > 0', $valueTable2 . '.value', $valueTable1 . '.value'); } $collection->getSelect()->order($valueExpr . ' ' . $dir); return $this; }
/** * @return void */ protected function initAttributes() { if (empty($this->attributes)) { foreach ($this->config['fields'] as $field) { if (isset($field['source']) && $field['source'] == 'eav') { $attribute = $this->dataSet->getEntity()->getAttribute($field['name']); if ($attribute) { $this->attributes[$field['name']] = $attribute->getData(); $options = []; if ($attribute->usesSource()) { $options = $attribute->getSource()->getAllOptions(); } $this->attributes[$field['name']]['options'] = $options; $this->attributes[$field['name']]['is_required'] = $attribute->getIsRequired(); } } } } }
/** * Apply filter to collection and add not skipped attributes to select. * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection) { if (!isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP]) || !is_array($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) { $exportFilter = []; } else { $exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP]; } $exportAttrCodes = $this->_getExportAttrCodes(); foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) { $attrCode = $attribute->getAttributeCode(); // filter applying if (isset($exportFilter[$attrCode])) { $attrFilterType = \Magento\ImportExport\Model\Export::getAttributeFilterType($attribute); if (\Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT == $attrFilterType) { if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) { $collection->addAttributeToFilter($attrCode, ['eq' => $exportFilter[$attrCode]]); } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT == $attrFilterType) { if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) { $collection->addAttributeToFilter($attrCode, ['like' => "%{$exportFilter[$attrCode]}%"]); } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_DATE == $attrFilterType) { if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) { $from = array_shift($exportFilter[$attrCode]); $to = array_shift($exportFilter[$attrCode]); if (is_scalar($from) && !empty($from)) { $date = (new \DateTime($from))->format('m/d/Y'); $collection->addAttributeToFilter($attrCode, ['from' => $date, 'date' => true]); } if (is_scalar($to) && !empty($to)) { $date = (new \DateTime($to))->format('m/d/Y'); $collection->addAttributeToFilter($attrCode, ['to' => $date, 'date' => true]); } } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_NUMBER == $attrFilterType) { if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) { $from = array_shift($exportFilter[$attrCode]); $to = array_shift($exportFilter[$attrCode]); if (is_numeric($from)) { $collection->addAttributeToFilter($attrCode, ['from' => $from]); } if (is_numeric($to)) { $collection->addAttributeToFilter($attrCode, ['to' => $to]); } } } } if (in_array($attrCode, $exportAttrCodes)) { $collection->addAttributeToSelect($attrCode); } } return $collection; }
/** * Add not skipped attributes to select * * @param AbstractCollection $collection * @return AbstractCollection */ protected function _addAttributesToCollection(AbstractCollection $collection) { $attributeCodes = $this->_getExportAttributeCodes(); $collection->addAttributeToSelect($attributeCodes); return $collection; }
/** * 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; }
/** * Add Value Sort To Collection Select * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @param string $dir * * @return $this */ public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC) { $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1'; $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2'; $collection->getSelect()->joinLeft([$valueTable1 => $this->getAttribute()->getBackend()->getTable()], "e.entity_id={$valueTable1}.entity_id" . " AND {$valueTable1}.attribute_id='{$this->getAttribute()->getId()}'" . " AND {$valueTable1}.store_id=0", [])->joinLeft([$valueTable2 => $this->getAttribute()->getBackend()->getTable()], "e.entity_id={$valueTable2}.entity_id" . " AND {$valueTable2}.attribute_id='{$this->getAttribute()->getId()}'" . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'", []); $valueExpr = $collection->getSelect()->getConnection()->getCheckSql("{$valueTable2}.value_id > 0", "{$valueTable2}.value", "{$valueTable1}.value"); $this->_attrOptionFactory->create()->addOptionValueToCollection($collection, $this->getAttribute(), $valueExpr); $collection->getSelect()->order("{$this->getAttribute()->getAttributeCode()} {$dir}"); return $this; }
/** * Adding join statement to collection select instance * * @param string $method * @param object $attribute * @param string $tableAlias * @param array $condition * @param string $fieldCode * @param string $fieldAlias * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection */ protected function _joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias) { if (isset($this->_joinAttributes[$fieldCode]['store_id'])) { $storeId = $this->_joinAttributes[$fieldCode]['store_id']; } else { $storeId = $this->getStoreId(); } $connection = $this->getConnection(); if ($storeId != $this->getDefaultStoreId() && !$attribute->isScopeGlobal()) { /** * Add joining default value for not default store * if value for store is null - we use default value */ $defCondition = '(' . implode(') AND (', $condition) . ')'; $defAlias = $tableAlias . '_default'; $defAlias = $this->getConnection()->getTableName($defAlias); $defFieldAlias = str_replace($tableAlias, $defAlias, $fieldAlias); $tableAlias = $this->getConnection()->getTableName($tableAlias); $defCondition = str_replace($tableAlias, $defAlias, $defCondition); $defCondition .= $connection->quoteInto(" AND " . $connection->quoteColumnAs("{$defAlias}.store_id", null) . " = ?", $this->getDefaultStoreId()); $this->getSelect()->{$method}([$defAlias => $attribute->getBackend()->getTable()], $defCondition, []); $method = 'joinLeft'; $fieldAlias = $this->getConnection()->getCheckSql("{$tableAlias}.value_id > 0", $fieldAlias, $defFieldAlias); $this->_joinAttributes[$fieldCode]['condition_alias'] = $fieldAlias; $this->_joinAttributes[$fieldCode]['attribute'] = $attribute; } else { $storeId = $this->getDefaultStoreId(); } $condition[] = $connection->quoteInto($connection->quoteColumnAs("{$tableAlias}.store_id", null) . ' = ?', $storeId); return parent::_joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias); }
/** * Reset left join * * @param int $limit * @param int $offset * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection */ protected function _getAllIdsSelect($limit = null, $offset = null) { $idsSelect = parent::_getAllIdsSelect($limit, $offset); $idsSelect->resetJoinLeft(); return $idsSelect; }
/** * Attach conditions filter to collection * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @param Combine $combine * * @return void */ public function attachConditionToCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection, Combine $combine) { $this->_connection = $collection->getResource()->getReadConnection(); $this->_joinTablesToCollection($collection, $combine); $whereExpression = (string) $this->_getMappedSqlCombination($combine); if (!empty($whereExpression)) { // Select ::where method adds braces even on empty expression $collection->getSelect()->where($whereExpression); } }
/** * @param array $items * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection * @throws \Magento\Framework\Exception\LocalizedException */ protected function convertValueArrayToCollectionItems($items, $collection) { /** @var \Magento\Rma\Model\ResourceModel\Item\Collection $collection */ foreach ($items as $item) { $collection->addFilter('entity_id', $item['entity_id'], 'or'); } $collection->load(); return $collection; }