本文整理汇总了PHP中Magento\Catalog\Model\ResourceModel\Product\Collection类的典型用法代码示例。如果您正苦于以下问题:PHP Collection类的具体用法?PHP Collection怎么用?PHP Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: install
/**
* {@inheritdoc}
*/
public function install(array $fixtures)
{
$this->configWriter->save('sales/msrp/enabled', 1);
foreach ($fixtures as $fileName) {
$fileName = $this->fixtureManager->getFixture($fileName);
if (!file_exists($fileName)) {
continue;
}
$rows = $this->csvReader->getData($fileName);
$header = array_shift($rows);
foreach ($rows as $row) {
$data = [];
foreach ($row as $key => $value) {
$data[$header[$key]] = $value;
}
$row = $data;
$productId = $this->getProductIdBySku($row['sku']);
if (!$productId) {
continue;
}
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productCollection->getItemById($productId);
$product->setMsrpDisplayActualPriceType(Type::TYPE_ON_GESTURE);
if (!empty($row['msrp'])) {
$price = $row['msrp'];
} else {
$price = $product->getPrice() * 1.1;
}
$product->setMsrp($price);
$product->save();
}
}
}
示例2: testGetCollection
public function testGetCollection()
{
$this->collectionMock->expects($this->once())->method('addAttributeToFilter');
$this->productLinkRepositoryMock->expects($this->once())->method('getList')->willReturn([]);
$this->requestMock->expects($this->exactly(2))->method('getParam')->willReturn(1);
$this->assertInstanceOf(Collection::class, $this->getModel()->getCollection());
}
示例3: setUp
/**
* Set up
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->rowCustomizerMock = $this->objectManagerHelper->getObject('\\Magento\\BundleImportExport\\Model\\Export\\RowCustomizer');
$this->productResourceCollection = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection', ['addAttributeToFilter', 'getIterator'], [], '', false);
$this->product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'getPriceType', 'getSkuType', 'getPriceView', 'getWeightType', 'getTypeInstance', 'getOptionsCollection', 'getSelectionsCollection'], [], '', false);
$this->product->expects($this->any())->method('getId')->willReturn(1);
$this->product->expects($this->any())->method('getPriceType')->willReturn(1);
$this->product->expects($this->any())->method('getSkuType')->willReturn(1);
$this->product->expects($this->any())->method('getPriceView')->willReturn(1);
$this->product->expects($this->any())->method('getWeightType')->willReturn(1);
$this->product->expects($this->any())->method('getTypeInstance')->willReturnSelf();
$this->optionsCollection = $this->getMock('\\Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection', ['setOrder', 'getIterator'], [], '', false);
$this->product->expects($this->any())->method('getOptionsCollection')->willReturn($this->optionsCollection);
$this->optionsCollection->expects($this->any())->method('setOrder')->willReturnSelf();
$this->option = $this->getMock('\\Magento\\Bundle\\Model\\Option', ['getId', 'getTitle', 'getType', 'getRequired'], [], '', false);
$this->option->expects($this->any())->method('getId')->willReturn(1);
$this->option->expects($this->any())->method('getTitle')->willReturn('title');
$this->option->expects($this->any())->method('getType')->willReturn(1);
$this->option->expects($this->any())->method('getRequired')->willReturn(1);
$this->optionsCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->option])));
$this->selection = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getSku', 'getSelectionPriceValue', 'getIsDefault', 'getSelectionQty', 'getSelectionPriceType'], [], '', false);
$this->selection->expects($this->any())->method('getSku')->willReturn(1);
$this->selection->expects($this->any())->method('getSelectionPriceValue')->willReturn(1);
$this->selection->expects($this->any())->method('getSelectionQty')->willReturn(1);
$this->selection->expects($this->any())->method('getSelectionPriceType')->willReturn(1);
$this->selectionsCollection = $this->getMock('\\Magento\\Bundle\\Model\\ResourceModel\\Selection\\Collection', ['getIterator', 'addAttributeToSort'], [], '', false);
$this->selectionsCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->selection])));
$this->selectionsCollection->expects($this->any())->method('addAttributeToSort')->willReturnSelf();
$this->product->expects($this->any())->method('getSelectionsCollection')->willReturn($this->selectionsCollection);
$this->productResourceCollection->expects($this->any())->method('addAttributeToFilter')->willReturnSelf();
$this->productResourceCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->product])));
}
示例4: addExcludeProductFilter
/**
* Make collection not to load products that are in specified quote
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @param int $quoteId
* @return $this
*/
public function addExcludeProductFilter($collection, $quoteId)
{
$connection = $this->getConnection();
$exclusionSelect = $connection->select()->from($this->getTable('quote_item'), ['product_id'])->where('quote_id = ?', $quoteId);
$condition = $connection->prepareSqlCondition('e.entity_id', ['nin' => $exclusionSelect]);
$collection->getSelect()->where($condition);
return $this;
}
示例5: collectValidatedAttributes
/**
* Collect validated attributes for Product Collection
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
* @return $this
*/
public function collectValidatedAttributes($productCollection)
{
$alias = array_keys($productCollection->getSelect()->getPart('from'))[0];
foreach ($this->getConditions() as $condition) {
$condition->setData('attribute', $alias . '.' . $condition->getData('attribute'));
$condition->addToCollection($productCollection);
}
return $this;
}
示例6: addFilterByAttributes
/**
* {@inheritDoc}
*/
protected function addFilterByAttributes(ProductCollection $productCollection, array $attributes)
{
foreach ($attributes as $code => $option) {
if (!is_array($option)) {
$option = [$option];
}
$productCollection->addAttributeToFilter($code, ['in' => $option]);
}
}
示例7: 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);
}
示例8: aroundAddFilter
/**
* Replace WHERE-filtering by HAVING-filtering.
*
* @param \Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFilterToCollection $subject
* @param \Closure $proceed
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @param $field
* @param null $condition
*/
public function aroundAddFilter(\Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFilterToCollection $subject, \Closure $proceed, \Magento\Catalog\Model\ResourceModel\Product\Collection $collection, $field, $condition = null)
{
/* skip identical conditions () */
$regKey = print_r($condition, true);
if (!isset($this->_regCond[$regKey])) {
$conn = $collection->getConnection();
$select = $collection->getSelect();
$equation = $this->_repoModifierProductGFrid->getEquationQty();
$prepared = $conn->prepareSqlCondition($equation, $condition);
$select->having($prepared);
$this->_regCond[$regKey] = true;
}
return;
}
示例9: afterAddStockDataToCollection
/**
* Replace default stock id in the where clause by stock id corresponded with store id.
*
* @param \Magento\CatalogInventory\Model\ResourceModel\Stock\Status $subject
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $result
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function afterAddStockDataToCollection(\Magento\CatalogInventory\Model\ResourceModel\Stock\Status $subject, \Magento\Catalog\Model\ResourceModel\Product\Collection $result)
{
/** @var \Magento\Framework\Db\Select $select */
$select = $result->getSelect();
$from = $select->getPart('from');
$join = $from['stock_status_index'];
$cond = $join['joinCondition'];
$stockId = $this->_manStock->getCurrentStockId();
$fixed = str_replace('.stock_id = 1', '.stock_id = ' . $stockId, $cond);
$join['joinCondition'] = $fixed;
$from['stock_status_index'] = $join;
$select->setPart('from', $from);
return $result;
}
示例10: _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;
}
示例11: testFindAttributeIdsByProductIds
public function testFindAttributeIdsByProductIds()
{
$productIds = [1, 2, 3];
$attributeSetIds = [3, 4, 6];
$select = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)->willReturnSelf();
$select->expects($this->once())->method('columns')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
$select->expects($this->once())->method('where')->with('entity_id IN (?)', $productIds)->willReturnSelf();
$select->expects($this->once())->method('group')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
$connection = $this->getMock(AdapterInterface::class);
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn($attributeSetIds);
$this->productCollection->expects($this->once())->method('getSelect')->willReturn($select);
$this->productCollection->expects($this->once())->method('getConnection')->willReturn($connection);
$this->assertEquals($attributeSetIds, $this->attributeSetFinder->findAttributeSetIdsByProductIds($productIds));
}
示例12: _prepareCollection
/**
* Premare block data
* @return $this
*/
protected function _prepareCollection()
{
$post = $this->_coreRegistry->registry('current_blog_post');
$this->_itemCollection = $this->_productCollectionFactory->create()->addAttributeToSelect('required_options')->addStoreFilter()->addAttributeToFilter('entity_id', array('in' => $post->getRelatedProductIds() ?: array(0)));
if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->setPageSize((int) $this->_scopeConfig->getValue('mfblog/post_view/related_products/number_of_products', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例13: _prepareCollection
/**
* Premare block data
* @return $this
*/
protected function _prepareCollection()
{
$post = $this->getPost();
$this->_itemCollection = $post->getRelatedProducts()->addAttributeToSelect('required_options');
if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->setPageSize((int) $this->_scopeConfig->getValue('mfblog/post_view/related_products/number_of_products', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$this->_itemCollection->getSelect()->order('rl.position', 'ASC');
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例14: addAttributeToSort
/**
* Add attribute to sort
*
* @param string $attribute
* @param string $dir
* @return $this|\Magento\Catalog\Model\ResourceModel\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);
}
示例15: addfilterByParent
protected function addfilterByParent()
{
$this->productCollectionMock->method('getTable')->with('catalog_product_relation')->willReturn('catalog_product_relation');
$zendDbSelectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false);
$this->productCollectionMock->method('getSelect')->willReturn($zendDbSelectMock);
$zendDbSelectMock->method('join')->willReturn($zendDbSelectMock);
$zendDbSelectMock->method('where')->willReturn($zendDbSelectMock);
}