本文整理汇总了PHP中Magento\Catalog\Model\Resource\Product\Collection类的典型用法代码示例。如果您正苦于以下问题:PHP Collection类的具体用法?PHP Collection怎么用?PHP Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* {@inheritdoc}
*/
public function run()
{
$this->logger->log('Installing MAP:');
$this->configWriter->save('sales/msrp/enabled', 1);
$fixtureFile = 'Msrp/products_msrp.csv';
$fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile);
/** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */
$csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']);
foreach ($csvReader as $row) {
$productId = $this->getProductIdBySku($row['sku']);
if (!$productId) {
continue;
}
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productCollection->getItemById($productId);
$product->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE);
if (!empty($row['msrp'])) {
$price = $row['msrp'];
} else {
$price = $product->getPrice() * 1.1;
}
$product->setMsrp($price);
$product->save();
$this->logger->logInline('.');
}
}
示例2: prepareData
/**
* Prepare configurable data for export
*
* @param \Magento\Catalog\Model\Resource\Product\Collection $collection
* @param int $productIds
* @return void
*/
public function prepareData($collection, $productIds)
{
$collection->addAttributeToFilter('entity_id', ['in' => $productIds])->addAttributeToFilter('type_id', ['eq' => \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE]);
while ($product = $collection->fetchItem()) {
$productAttributesOptions = $product->getTypeInstance()->getConfigurableOptions($product);
foreach ($productAttributesOptions as $productAttributeOption) {
$this->configurableData[$product->getId()] = [];
$variations = [];
$variationsPrices = [];
$variationsLabels = [];
foreach ($productAttributeOption as $optValues) {
$variations[$optValues['sku']][] = $optValues['attribute_code'] . '=' . $optValues['option_title'];
$priceType = $optValues['pricing_is_percent'] ? 'percent' : 'fixed';
$variationsPrices[] = 'name=' . $optValues['attribute_code'] . ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR . 'value=' . $optValues['option_title'] . ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR . 'price=' . $optValues['pricing_value'] . ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR . 'price_type=' . $priceType;
if (!empty($optValues['super_attribute_label'])) {
$variationsLabels[$optValues['attribute_code']] = $optValues['attribute_code'] . '=' . $optValues['super_attribute_label'];
}
}
foreach ($variations as $sku => $values) {
$variations[$sku] = 'sku=' . $sku . ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR . implode(ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $values);
}
$variations = implode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $variations);
$variationsPrices = array_unique($variationsPrices);
$variationsPrices = implode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $variationsPrices);
$variationsLabels = implode(ImportProduct::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $variationsLabels);
$this->configurableData[$product->getId()] = ['configurable_variations' => $variations, 'configurable_variation_prices' => $variationsPrices, 'configurable_variation_labels' => $variationsLabels];
}
}
}
示例3: addExcludeProductFilter
/**
* Make collection not to load products that are in specified quote
*
* @param \Magento\Catalog\Model\Resource\Product\Collection $collection
* @param int $quoteId
* @return $this
*/
public function addExcludeProductFilter($collection, $quoteId)
{
$adapter = $this->_getReadAdapter();
$exclusionSelect = $adapter->select()->from($this->getTable('sales_flat_quote_item'), array('product_id'))->where('quote_id = ?', $quoteId);
$condition = $adapter->prepareSqlCondition('e.entity_id', array('nin' => $exclusionSelect));
$collection->getSelect()->where($condition);
return $this;
}
示例4: __construct
/**
* @param \Magento\Catalog\Api\CategoryManagementInterface $categoryReadService
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $attributeCollectionFactory
* @param \Magento\Eav\Model\Resource\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Catalog\Model\Resource\Product\Collection $productCollection
*/
public function __construct(\Magento\Catalog\Api\CategoryManagementInterface $categoryReadService, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Eav\Model\Resource\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Catalog\Model\Resource\Product\Collection $productCollection)
{
$this->categoryReadService = $categoryReadService;
$this->eavConfig = $eavConfig;
$this->attributeCollectionFactory = $attributeCollectionFactory;
$this->attrOptionCollectionFactory = $attrOptionCollectionFactory;
$this->productCollection = $productCollection->removeAllItems()->clear()->addAttributeToSelect('sku');
}
示例5: tearDown
/**
* Execute per test cleanup
*/
public function tearDown()
{
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\Registry');
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
$this->productCollection->addFieldToFilter('entity_id', ['in' => [10, 11, 12]])->delete();
unset($this->productCollection);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
}
示例6: prepareData
/**
* Prepare configurable data for export
*
* @param \Magento\Catalog\Model\Resource\Product\Collection $collection
* @param int $productIds
* @return void
*/
public function prepareData($collection, $productIds)
{
$collection->addAttributeToFilter('entity_id', array('in' => $productIds))->addAttributeToFilter('type_id', array('eq' => \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE));
while ($product = $collection->fetchItem()) {
$productAttributesOptions = $product->getTypeInstance()->getConfigurableOptions($product);
foreach ($productAttributesOptions as $productAttributeOption) {
$this->configurableData[$product->getId()] = array();
foreach ($productAttributeOption as $optionValues) {
$priceType = $optionValues['pricing_is_percent'] ? '%' : '';
$this->configurableData[$product->getId()][] = array('_super_products_sku' => $optionValues['sku'], '_super_attribute_code' => $optionValues['attribute_code'], '_super_attribute_option' => $optionValues['option_title'], '_super_attribute_price_corr' => $optionValues['pricing_value'] . $priceType);
}
}
}
}
示例7: getProducts
/**
* Get products by IDs
* @param array $productIds
* @param null|int $chunkSize
* @param bool $filterVisibility
* @return \Magento\Catalog\Model\Resource\Product\Collection
*/
public function getProducts(array $productIds = [], $chunkSize = null, $filterVisibility = true)
{
$this->_productCollection->addAttributeToSelect('sku')->addAttributeToSelect($this->_config->getProductAttributes())->addAttributeToSelect('status')->addAttributeToSelect('visibility');
if ($filterVisibility) {
$this->_productCollection->addAttributeToFilter('visibility', ['in' => [Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH]])->addAttributeToFilter('status', Status::STATUS_ENABLED);
}
if (!empty($productIds)) {
$this->_productCollection->addIdFilter($productIds);
}
if ($chunkSize !== null) {
$this->_productCollection->setPageSize($chunkSize);
}
return $this->_productCollection;
}
示例8: _prepareData
/**
* @return $this
*/
protected function _prepareData()
{
$product = $this->_coreRegistry->registry('product');
/* @var $product \Magento\Catalog\Model\Product */
$this->_itemCollection = $product->getRelatedProductCollection()->addAttributeToSelect('required_options')->setPositionOrder()->addStoreFilter();
if ($this->moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例9: addAttributeToSort
/**
* Add attribute to sort
*
* @param string $attribute
* @param string $dir
* @return $this|\Magento\Catalog\Model\Resource\Product\Collection
*/
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
if (in_array($attribute, ['review_cnt', 'last_created', 'avg_rating', 'avg_rating_approved'])) {
$this->getSelect()->order($attribute . ' ' . $dir);
return $this;
}
return parent::addAttributeToSort($attribute, $dir);
}
示例10: _prepareData
/**
* @return $this
*/
protected function _prepareData()
{
$product = $this->_coreRegistry->registry('product');
/* @var $product \Magento\Catalog\Model\Product */
$this->_itemCollection = $product->getUpSellProductCollection()->setPositionOrder()->addStoreFilter();
if ($this->moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->load();
/**
* Updating collection with desired items
*/
$this->_eventManager->dispatch('catalog_product_upsell', ['product' => $product, 'collection' => $this->_itemCollection, 'limit' => null]);
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例11: setUp
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function setUp()
{
$this->setCollectionFactory = $this->getMock('Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\CollectionFactory', ['create'], [], '', false);
$this->setCollection = $this->getMock('Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\Collection', ['setEntityTypeFilter'], [], '', false);
$this->setCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($this->setCollection));
$item = new \Magento\Framework\Object(['id' => 1, 'attribute_set_name' => 'Default', '_attribute_set' => 'Default']);
$this->setCollection->expects($this->any())->method('setEntityTypeFilter')->will($this->returnValue([$item]));
$this->attrCollectionFactory = $this->getMock('Magento\\Catalog\\Model\\Resource\\Product\\Attribute\\CollectionFactory', ['create'], [], '', false);
$this->attrCollection = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Product\\Attribute\\Collection', ['setAttributeSetFilter'], [], '', false);
$superAttributes = [];
foreach ($this->_getSuperAttributes() as $superAttribute) {
$item = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', ['isStatic'], $superAttribute, '', false);
$item->setData($superAttribute);
$item->method('isStatic')->will($this->returnValue(false));
$superAttributes[] = $item;
}
$this->attrCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($this->attrCollection));
$this->attrCollection->expects($this->any())->method('setAttributeSetFilter')->will($this->returnValue($superAttributes));
$this->_entityModel = $this->getMock('Magento\\CatalogImportExport\\Model\\Import\\Product', ['getNewSku', 'getOldSku', 'getNextBunch', 'isRowAllowedToImport', 'getConnection', 'getAttrSetIdToName', 'getAttributeOptions'], [], '', false);
$this->params = [0 => $this->_entityModel, 1 => 'configurable'];
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->_connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', ['select', 'fetchAll', 'fetchPairs', 'joinLeft', 'insertOnDuplicate', 'delete', 'quoteInto'], [], '', false);
$this->select = $this->getMock('Magento\\Framework\\DB\\Select', ['from', 'where', 'joinLeft', 'getAdapter'], [], '', false);
$this->select->expects($this->any())->method('from')->will($this->returnSelf());
$this->select->expects($this->any())->method('where')->will($this->returnSelf());
$this->select->expects($this->any())->method('joinLeft')->will($this->returnSelf());
$this->_connection->expects($this->any())->method('select')->will($this->returnValue($this->select));
$adapter = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', [], [], '', false);
$adapter->expects($this->any())->method('quoteInto')->will($this->returnValue('query'));
$this->select->expects($this->any())->method('getAdapter')->willReturn($adapter);
$this->_connection->expects($this->any())->method('insertOnDuplicate')->willReturnSelf();
$this->_connection->expects($this->any())->method('delete')->willReturnSelf();
$this->_connection->expects($this->any())->method('quoteInto')->willReturn('');
$this->_connection->expects($this->any())->method('fetchPairs')->will($this->returnValue([]));
$this->resource = $this->getMock('\\Magento\\Framework\\App\\Resource', ['getConnection', 'getTableName'], [], '', false);
$this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->_connection));
$this->resource->expects($this->any())->method('getTableName')->will($this->returnValue('tableName'));
$this->_entityModel->expects($this->any())->method('getConnection')->will($this->returnValue($this->_connection));
$this->productCollectionFactory = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Product\\CollectionFactory', ['create'], [], '', false);
$this->productCollection = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection', ['addFieldToFilter', 'addAttributeToSelect'], [], '', false);
$products = [];
$testProducts = [['id' => 1, 'attribute_set_id' => 4, 'testattr2' => 1, 'testattr3' => 1], ['id' => 2, 'attribute_set_id' => 4, 'testattr2' => 1, 'testattr3' => 1], ['id' => 20, 'attribute_set_id' => 4, 'testattr2' => 1, 'testattr3' => 1]];
foreach ($testProducts as $product) {
$item = $this->getMock('\\Magento\\Framework\\Object', ['getAttributeSetId'], [], '', false);
$item->setData($product);
$item->expects($this->any())->method('getAttributeSetId')->willReturn(4);
$products[] = $item;
}
$this->productCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($this->productCollection));
$this->productCollection->expects($this->any())->method('addFieldToFilter')->will($this->returnValue($this->productCollection));
$this->productCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnValue($products));
$this->_entityModel->expects($this->any())->method('getAttributeOptions')->will($this->returnValue(['attr2val1' => '1', 'attr2val2' => '2', 'attr2val3' => '3', 'testattr3v1' => '4', 'testattr30v1' => '4', 'testattr3v2' => '5', 'testattr3v3' => '6']));
$this->configurable = $this->objectManagerHelper->getObject('Magento\\ConfigurableImportExport\\Model\\Import\\Product\\Type\\Configurable', ['attrSetColFac' => $this->setCollectionFactory, 'prodAttrColFac' => $this->attrCollectionFactory, 'params' => $this->params, 'resource' => $this->resource, 'productColFac' => $this->productCollectionFactory]);
}
示例12: __construct
/**
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Framework\App\Resource $resource
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
* @param \Magento\Catalog\Model\Resource\Helper $resourceHelper
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Module\Manager $moduleManager
* @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
* @param \Magento\Catalog\Model\Resource\Url $catalogUrl
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
* @param \Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $attributeCollectionFactory
* @param \Zend_Db_Adapter_Abstract $connection
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\Resource $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Catalog\Model\Resource\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager, \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, \Magento\Catalog\Model\Resource\Url $catalogUrl, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Catalog\Model\Resource\Product\Attribute\CollectionFactory $attributeCollectionFactory, $connection = null)
{
$this->_attributeCollectionFactory = $attributeCollectionFactory;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $resource, $eavEntityFactory, $resourceHelper, $universalFactory, $storeManager, $moduleManager, $catalogProductFlatState, $scopeConfig, $productOptionFactory, $catalogUrl, $localeDate, $customerSession, $dateTime, $groupManagement, $connection);
}
示例13: addFilterGroupToCollection
/**
* Helper function that adds a FilterGroup to the collection.
*
* @param FilterGroup $filterGroup
* @param Collection $collection
* @return void
* @throws \Magento\Framework\Exception\InputException
*/
protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
{
$fields = [];
foreach ($filterGroup->getFilters() as $filter) {
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$field = $this->translateField($filter->getField());
$fields[] = array('attribute' => $field, $condition => $filter->getValue());
}
if ($fields) {
$collection->addFieldToFilter($fields);
}
}
示例14: __construct
/**
* @param \Magento\Core\Model\EntityFactory $entityFactory
* @param \Magento\Framework\Logger $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Framework\App\Resource $resource
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
* @param \Magento\Catalog\Model\Resource\Helper $resourceHelper
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
* @param \Magento\Framework\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Helper\Data $catalogData
* @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
* @param \Magento\Catalog\Model\Resource\Url $catalogUrl
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Customer\Model\Visitor $customerVisitor
* @param mixed $connection
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\Logger $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\Resource $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Catalog\Model\Resource\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Data $catalogData, \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, \Magento\Catalog\Model\Resource\Url $catalogUrl, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Model\Visitor $customerVisitor, $connection = null)
{
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $resource, $eavEntityFactory, $resourceHelper, $universalFactory, $storeManager, $catalogData, $catalogProductFlatState, $scopeConfig, $productOptionFactory, $catalogUrl, $localeDate, $customerSession, $dateTime, $connection);
$this->_customerVisitor = $customerVisitor;
}
示例15: testStoreDependentAttributeValue
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
* @magentoConfigFixture current_store customer/account_share/scope 0
*/
public function testStoreDependentAttributeValue()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Store\Model\Store $store */
$store = $objectManager->create('Magento\\Store\\Model\\Store');
$store->load('fixturestore', 'code');
$product = $this->_collection->addAttributeToSelect('name')->load()->getFirstItem();
$this->assertEquals('Simple Product One', $product->getName());
$product = $this->_collection->clear()->addAttributeToSelect('name')->addStoreFilter($store)->load()->getFirstItem();
$this->assertEquals("StoreTitle", $product->getName());
}