作者:swissu
项目:cor
/**
* 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();
}
}
}
}
作者:kidaa3
项目:magento2-platforms
/**
* Construct
*
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
* @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
* @param array $meta
* @param array $data
*/
public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, array $addFieldStrategies = [], array $addFilterStrategies = [], array $meta = [], array $data = [])
{
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collectionFactory->create();
$this->addFieldStrategies = $addFieldStrategies;
$this->addFilterStrategies = $addFilterStrategies;
}
作者:kidaa3
项目:magento2-platforms
/**
* {@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>");
}
作者:kidaa3
项目:magento2-platforms
/**
* @param SampleDataContext $sampleDataContext
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
*/
public function __construct(SampleDataContext $sampleDataContext, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Framework\App\Config\Storage\WriterInterface $configWriter)
{
$this->fixtureManager = $sampleDataContext->getFixtureManager();
$this->csvReader = $sampleDataContext->getCsvReader();
$this->productCollection = $productCollectionFactory->create()->addAttributeToSelect('sku');
$this->configWriter = $configWriter;
}
作者:Doabilit
项目:magento2de
/**
* {@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>");
}
作者:Doabilit
项目:magento2de
protected function setUp()
{
$this->productCollection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$this->productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock();
$this->productCollectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
$this->attributeSetFinder = (new ObjectManager($this))->getObject(AttributeSetFinder::class, ['productCollectionFactory' => $this->productCollectionFactory]);
}
作者:kidaa3
项目:magento2-platforms
/**
* @param \Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory
* @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
*/
public function __construct(\Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory)
{
$this->categoryTree = $categoryTreeFactory->create(['categoryTree' => $categoryResourceTreeFactory->create(), 'categoryCollection' => $categoryCollectionFactory->create()]);
$this->eavConfig = $eavConfig;
$this->attributeCollectionFactory = $attributeCollectionFactory;
$this->attrOptionCollectionFactory = $attrOptionCollectionFactory;
$this->productCollection = $productCollectionFactory->create();
}
作者:Doabilit
项目:magento2de
/**
* {@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;
}
作者:BlackIkeEagl
项目:magento2-continuousph
/**
* @return void
*/
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
$this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->setMethods(['toArray', 'isLoaded', 'addAttributeToFilter', 'load', 'getSize', 'addFilterByRequiredOptions', 'addStoreFilter'])->getMock();
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->collectionFactoryMock->expects($this->any())->method('create')->willReturn($this->collectionMock);
$this->dataHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->setMethods(['getAllowedSelectionTypes'])->getMock();
}
作者:emizentec
项目:magento2-featured-product
/**
* 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;
}
作者:BlackIkeEagl
项目:magento2-continuousph
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
$this->poolMock = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock();
$this->modifierMockOne = $this->getMockBuilder(ModifierInterface::class)->setMethods(['getData', 'getMeta'])->getMockForAbstractClass();
$this->model = $this->objectManager->getObject(ProductDataProvider::class, ['name' => 'testName', 'primaryFieldName' => 'testPrimaryFieldName', 'requestFieldName' => 'testRequestFieldName', 'collectionFactory' => $this->collectionFactoryMock, 'pool' => $this->poolMock]);
}
作者:Doabilit
项目:magento2de
protected function setUp()
{
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
$this->collectionMock = $this->getMockBuilder(AbstractCollection::class)->disableOriginalConstructor()->setMethods(['load', 'getSelect', 'getTable', 'getIterator', 'isLoaded', 'toArray', 'getSize'])->getMockForAbstractClass();
$this->dbSelectMock = $this->getMockBuilder(DbSelect::class)->disableOriginalConstructor()->getMock();
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->dataProvider = $this->objectManagerHelper->getObject(ProductCustomOptionsDataProvider::class, ['collectionFactory' => $this->collectionFactoryMock, 'request' => $this->requestMock]);
}
作者:pradeep-wagent
项目:magento
/**
* @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;
}
作者:BlackIkeEagl
项目:magento2-continuousph
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)->getMockForAbstractClass();
$this->productLinkRepositoryMock = $this->getMockBuilder(ProductLinkRepositoryInterface::class)->getMockForAbstractClass();
$this->productMock = $this->getMockBuilder(ProductInterface::class)->getMockForAbstractClass();
$this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock();
$this->productRepositoryMock->expects($this->any())->method('getById')->willReturn($this->productMock);
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
}
作者:pradeep-wagent
项目:magento
/**
* @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;
}
作者:samitrima
项目:Blog-Extension-for-Magento-
/**
* 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;
}
作者:mirasvi
项目:module-blo
/**
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getRelatedProducts()
{
$ids = $this->getRelatedProductIds();
$ids[] = 0;
$collection = $this->productCollectionFactory->create()->addAttributeToSelect('*')->addFieldToFilter('entity_id', $ids);
return $collection;
}
作者:samitrima
项目:Blog-Extension-for-Magento-
/**
* Prepare collection
*
* @return Extended
*/
protected function _prepareCollection()
{
$post = $this->getPost();
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->joinField('websites', 'catalog_product_website', 'website_id', 'product_id=entity_id', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
作者:kidaa3
项目:magento2-platforms
/**
* @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;
}
作者:salelso
项目:magento2-extension-change-attribute-se
/**
* 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]);
}