本文整理汇总了PHP中Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::create方法的典型用法代码示例。如果您正苦于以下问题:PHP CollectionFactory::create方法的具体用法?PHP CollectionFactory::create怎么用?PHP CollectionFactory::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
的用法示例。
在下文中一共展示了CollectionFactory::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Update product(s) status action
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$productIds = $collection->getAllIds();
$storeId = (int) $this->getRequest()->getParam('store', 0);
$attributeSetId = (int) $this->getRequest()->getParam('changeattributeset');
try {
foreach ($collection->getItems() as $product) {
//echo get_class($product->getTypeInstance());die();
if ($this->validateConfigurable($product, $attributeSetId, $storeId) == false) {
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
break;
}
$product->setAttributeSetId($attributeSetId)->setStoreId($storeId);
}
$collection->save();
//$product->getTypeInstance();
$this->messageManager->addSuccess(__('A total of %1 record(s) have been updated.', count($productIds)));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->_getSession()->addException($e, __('Something went wrong while updating the product(s) atrribute set.'));
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
}
示例2: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appState->setAreaCode('catalog');
/** @var ProductCollection $productCollection */
$productCollection = $this->productCollectionFactory->create();
$productIds = $productCollection->getAllIds();
if (!count($productIds)) {
$output->writeln("<info>No product images to resize</info>");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}
try {
foreach ($productIds as $productId) {
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
continue;
}
/** @var ImageCache $imageCache */
$imageCache = $this->imageCacheFactory->create();
$imageCache->generate($product);
$output->write(".");
}
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
$output->write("\n");
$output->writeln("<info>Product images resized successfully</info>");
}
示例3: execute
/**
* Enable requested attribute for products, if no products are using it
*
* Date attributes is supported for attribute_from values only.
*
* @param array $data Key => Value pairs of attribute_code and count
* @return void
*/
public function execute($data)
{
$visibility = $this->catalogProductVisibility->getVisibleInCatalogIds();
$attributes = $this->attributeCollectionFactory->create()->addFieldToFilter('attribute_code', ['in' => array_keys($data)]);
foreach ($attributes as $attribute) {
$collection = $this->productCollectionFactory->create()->setPageSize(1)->setCurPage(1);
switch ($attribute->getFrontendInput()) {
case 'boolean':
$value = 1;
$collection->addAttributeToFilter($attribute, 1);
break;
case 'date':
$value = $this->localeDate->date()->format('Y-m-d H:i:s');
$collection->addAttributeToFilter($attribute, [['date' => true, 'to' => $value]]);
break;
}
if ($collection->getSize()) {
// customer already has some products with specified attribute
continue;
}
foreach ($this->getStoreIds() as $storeId) {
$visibleProducts = $this->productCollectionFactory->create()->setStoreId($storeId)->setVisibility($visibility)->addStoreFilter($storeId)->setPageSize($data[$attribute->getAttributeCode()])->setCurPage(1);
if (!$visibleProducts->getSize()) {
continue;
}
foreach ($visibleProducts as $product) {
$product->addAttributeUpdate($attribute->getAttributeCode(), (int) in_array(0, $this->getStoreIds()), 0);
$product->setStoreId($storeId)->setData($attribute->getAttributeCode(), $value)->save();
}
}
}
}
示例4: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appState->setAreaCode('catalog');
/** @var ProductCollection $productCollection */
$productCollection = $this->productCollectionFactory->create();
$productIds = $productCollection->getAllIds();
if (!count($productIds)) {
$output->writeln("<info>No product images to resize</info>");
return;
}
try {
foreach ($productIds as $productId) {
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
continue;
}
/** @var ImageCache $imageCache */
$imageCache = $this->imageCacheFactory->create();
$imageCache->generate($product);
$output->write(".");
}
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
return;
}
$output->write("\n");
$output->writeln("<info>Product images resized successfully</info>");
}
示例5: getCollection
/**
* @param \Magento\Catalog\Model\Category $category
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getCollection(\Magento\Catalog\Model\Category $category)
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addCategoryFilter($category);
return $collection;
}
示例6: findAttributeSetIdsByProductIds
/**
* {@inheritdoc}
*/
public function findAttributeSetIdsByProductIds(array $productIds)
{
/** @var $collection Collection */
$collection = $this->productCollectionFactory->create();
$select = $collection->getSelect()->reset(Select::COLUMNS)->columns(ProductInterface::ATTRIBUTE_SET_ID)->where('entity_id IN (?)', $productIds)->group(ProductInterface::ATTRIBUTE_SET_ID);
$result = $collection->getConnection()->fetchCol($select);
return $result;
}
示例7: getFeaturedProduct
/**
* get featured product collection
*/
public function getFeaturedProduct()
{
$limit = $this->getProductLimit();
$collection = $this->_productCollectionFactory->create();
$collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$collection->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setPageSize($limit)->addAttributeToSelect('*');
$collection->addAttributeToFilter('et_featured', '1');
return $collection;
}
示例8: _getProductCollection
/**
* @return Collection
*/
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
/** @var Collection $collection */
$collection = $this->_productCollectionFactory->create();
$this->_catalogLayer->prepareProductCollection($collection);
$collection->addAttributeToFilter('promotion', 1)->addStoreFilter();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
示例9: executeInternal
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function executeInternal()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$this->attributeHelper->setProductIds($collection->getAllIds());
if (!$this->_validateProducts()) {
return $this->resultRedirectFactory->create()->setPath('catalog/product/', ['_current' => true]);
}
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Update Attributes'));
return $resultPage;
}
示例10: execute
/**
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$productDeleted = 0;
foreach ($collection->getItems() as $product) {
$product->delete();
$productDeleted++;
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $productDeleted));
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
}
示例11: getProducts
/**
* {@inheritdoc}
*/
public function getProducts(ProductInterface $product)
{
if (!isset($this->products[$product->getId()])) {
if ($this->requestSafety->isSafeMethod()) {
$productIds = $this->resource->getConnection()->fetchCol('(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')');
$this->products[$product->getId()] = $this->collectionFactory->create()->addAttributeToSelect(['price', 'special_price'])->addIdFilter($productIds);
} else {
$this->products[$product->getId()] = $this->configurable->getUsedProducts($product);
}
}
return $this->products[$product->getId()];
}
示例12: _getProductCollection
/**
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->_productCollectionFactory->create();
$this->_catalogLayer->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
$collection->setPage(1, $numProducts);
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
示例13: getCount
/**
* {@inheritdoc}
*/
public function getCount($status = null)
{
$products = $this->productsFactory->create();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
switch ($status) {
case Status::STATUS_ENABLED:
$products->addAttributeToFilter('status', Status::STATUS_ENABLED);
break;
case Status::STATUS_DISABLED:
$products->addAttributeToFilter('status', Status::STATUS_DISABLED);
break;
}
return $products->getSize();
}
示例14: _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;
}
示例15: getProductCollection
/**
* @param \Magento\Catalog\Model\Category $category
* @param int $storeId
* @return $this
*/
public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId)
{
/** @var $layer \Magento\Catalog\Model\Layer */
$layer = $this->catalogLayer->setStore($storeId);
$collection = $category->getResourceCollection();
$collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load();
/** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$productCollection = $this->collectionFactory->create();
$currentCategory = $layer->setCurrentCategory($category);
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
$category->getProductCollection()->setStoreId($storeId);
$products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50);
return $products;
}