示例#1
0
文件: Products.php 项目: swissup/core
 /**
  * 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();
             }
         }
     }
 }
示例#2
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testGetProductCollection()
 {
     $storeId = 1;
     $categoryChildren = 'children';
     $visibleInCatalogIds = 1;
     $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->will($this->returnValue($visibleInCatalogIds));
     $products = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection', ['setStoreId', 'addAttributeToSort', 'setVisibility', 'setCurPage', 'setPageSize', 'addCountToCategories'], [], '', false);
     $resourceCollection = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Collection\\AbstractCollection', ['addAttributeToSelect', 'addAttributeToFilter', 'addIdFilter', 'load'], [], '', false);
     $resourceCollection->expects($this->exactly(3))->method('addAttributeToSelect')->will($this->returnSelf());
     $resourceCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf());
     $resourceCollection->expects($this->once())->method('addIdFilter')->with($categoryChildren)->will($this->returnSelf());
     $resourceCollection->expects($this->once())->method('load')->will($this->returnSelf());
     $products->expects($this->once())->method('addCountToCategories')->with($resourceCollection);
     $products->expects($this->once())->method('addAttributeToSort')->with('updated_at', 'desc')->will($this->returnSelf());
     $products->expects($this->once())->method('setVisibility')->with($visibleInCatalogIds)->will($this->returnSelf());
     $products->expects($this->once())->method('setCurPage')->with(1)->will($this->returnSelf());
     $products->expects($this->once())->method('setPageSize')->with(50)->will($this->returnSelf());
     $products->expects($this->once())->method('setStoreId')->with($storeId);
     $this->collectionFactory->expects($this->once())->method('create')->will($this->returnValue($products));
     $category = $this->getMock('Magento\\Catalog\\Model\\Category', ['getResourceCollection', 'getChildren', 'getProductCollection', '__wakeup'], [], '', false);
     $category->expects($this->once())->method('getResourceCollection')->will($this->returnValue($resourceCollection));
     $category->expects($this->once())->method('getChildren')->will($this->returnValue($categoryChildren));
     $category->expects($this->once())->method('getProductCollection')->will($this->returnValue($products));
     $layer = $this->getMock('Magento\\Catalog\\Model\\Layer', ['setCurrentCategory', 'prepareProductCollection', 'getProductCollection', '__wakeup'], [], '', false);
     $layer->expects($this->once())->method('setCurrentCategory')->with($category)->will($this->returnSelf());
     $layer->expects($this->once())->method('getProductCollection')->will($this->returnValue($products));
     /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Layer\Resolver $layerResolver */
     $layerResolver = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Layer\\Resolver')->disableOriginalConstructor()->setMethods(['get', 'create'])->getMock();
     $layerResolver->expects($this->any())->method($this->anything())->will($this->returnValue($layer));
     $this->categoryLayer->expects($this->once())->method('setStore')->with($storeId)->will($this->returnValue($layer));
     $this->assertEquals($products, $this->model->getProductCollection($category, $storeId));
 }
示例#3
0
 /**
  * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
  */
 protected function _getProductCollection()
 {
     if (is_null($this->productCollection)) {
         $collection = $this->getAuthor()->getSelectedProductsCollection()->setStore($this->_storeManager->getStore())->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addStoreFilter()->addUrlRewrite()->setVisibility($this->productVisibility->getVisibleInCatalogIds());
         $collection->getSelect()->order('position');
         $this->productCollection = $collection;
     }
     return $this->productCollection;
 }
 /**
  * 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;
 }
 /**
  * Return options.
  *
  * @return mixed
  */
 public function toOptionArray()
 {
     $visibilities = $this->productVisibility->getAllOptions();
     $options[] = ['label' => __('---- Default Option ----'), 'value' => '0'];
     foreach ($visibilities as $visibility) {
         $options[] = ['label' => $visibility['label'], 'value' => $visibility['value']];
     }
     return $options;
 }
 /**
  * Append bundles in upsell list for current product
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /* @var $product \Magento\Catalog\Model\Product */
     $product = $observer->getEvent()->getProduct();
     /**
      * Check is current product type is allowed for bundle selection product type
      */
     if (!in_array($product->getTypeId(), $this->bundleData->getAllowedSelectionTypes())) {
         return $this;
     }
     /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */
     $collection = $observer->getEvent()->getCollection();
     $limit = $observer->getEvent()->getLimit();
     if (is_array($limit)) {
         if (isset($limit['upsell'])) {
             $limit = $limit['upsell'];
         } else {
             $limit = 0;
         }
     }
     /* @var $resource \Magento\Bundle\Model\ResourceModel\Selection */
     $resource = $this->bundleSelection;
     $productIds = array_keys($collection->getItems());
     if ($limit !== null && $limit <= count($productIds)) {
         return $this;
     }
     // retrieve bundle product ids
     $bundleIds = $resource->getParentIdsByChild($product->getId());
     // exclude up-sell product ids
     $bundleIds = array_diff($bundleIds, $productIds);
     if (!$bundleIds) {
         return $this;
     }
     /* @var $bundleCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
     $bundleCollection = $product->getCollection()->addAttributeToSelect($this->config->getProductAttributes())->addStoreFilter()->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setVisibility($this->productVisibility->getVisibleInCatalogIds());
     if ($limit !== null) {
         $bundleCollection->setPageSize($limit);
     }
     $bundleCollection->addFieldToFilter('entity_id', ['in' => $bundleIds])->setFlag('do_not_use_category_id', true);
     if ($collection instanceof \Magento\Framework\Data\Collection) {
         foreach ($bundleCollection as $item) {
             $collection->addItem($item);
         }
     } elseif ($collection instanceof \Magento\Framework\DataObject) {
         $items = $collection->getItems();
         foreach ($bundleCollection as $item) {
             $items[$item->getEntityId()] = $item;
         }
         $collection->setItems($items);
     }
     return $this;
 }
示例#7
0
 /**
  * @param int $storeId
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 public function getProductsCollection($storeId)
 {
     /** @var $product \Magento\Catalog\Model\Product */
     $product = $this->productFactory->create();
     $todayStartOfDayDate = $this->localeDate->date()->setTime(0, 0)->format('Y-m-d H:i:s');
     $todayEndOfDayDate = $this->localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s');
     /** @var $products \Magento\Catalog\Model\ResourceModel\Product\Collection */
     $products = $product->getResourceCollection();
     $products->setStoreId($storeId);
     $products->addStoreFilter()->addAttributeToFilter('news_from_date', ['or' => [0 => ['date' => true, 'to' => $todayEndOfDayDate], 1 => ['is' => new \Zend_Db_Expr('null')]]], 'left')->addAttributeToFilter('news_to_date', ['or' => [0 => ['date' => true, 'from' => $todayStartOfDayDate], 1 => ['is' => new \Zend_Db_Expr('null')]]], 'left')->addAttributeToFilter([['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')], ['attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null')]])->addAttributeToSort('news_from_date', 'desc')->addAttributeToSelect(['name', 'short_description', 'description'], 'inner')->addAttributeToSelect(['price', 'special_price', 'special_from_date', 'special_to_date', 'msrp_display_actual_price_type', 'msrp', 'thumbnail'], 'left')->applyFrontendPriceLimitations();
     $products->setVisibility($this->visibility->getVisibleInCatalogIds());
     return $products;
 }
示例#8
0
 /**
  * @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;
 }
示例#9
0
 /**
  * @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;
 }
 /**
  * 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;
 }
示例#11
0
 /**
  * Test public `createCollection` method and protected `getPageSize` method via `createCollection`
  *
  * @param bool $pagerEnable
  * @param int $productsCount
  * @param int $productsPerPage
  * @param int $expectedPageSize
  * @dataProvider createCollectionDataProvider
  */
 public function testCreateCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
 {
     $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->willReturn([Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH]);
     $collection = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection')->setMethods(['setVisibility', 'addMinimalPrice', 'addFinalPrice', 'addTaxPercents', 'addAttributeToSelect', 'addUrlRewrite', 'addStoreFilter', 'setPageSize', 'setCurPage'])->disableOriginalConstructor()->getMock();
     $collection->expects($this->once())->method('setVisibility')->with([Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH])->willReturnSelf();
     $collection->expects($this->once())->method('addMinimalPrice')->willReturnSelf();
     $collection->expects($this->once())->method('addFinalPrice')->willReturnSelf();
     $collection->expects($this->once())->method('addTaxPercents')->willReturnSelf();
     $collection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf();
     $collection->expects($this->once())->method('addUrlRewrite')->willReturnSelf();
     $collection->expects($this->once())->method('addStoreFilter')->willReturnSelf();
     $collection->expects($this->once())->method('setPageSize')->with($expectedPageSize)->willReturnSelf();
     $collection->expects($this->once())->method('setCurPage')->willReturnSelf();
     $this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
     $this->productsList->setData('conditions_encoded', 'some_serialized_conditions');
     $conditions = $this->getMockBuilder('\\Magento\\Rule\\Model\\Condition\\Combine')->setMethods(['collectValidatedAttributes'])->disableOriginalConstructor()->getMock();
     $conditions->expects($this->once())->method('collectValidatedAttributes')->with($collection)->willReturnSelf();
     $this->builder->expects($this->once())->method('attachConditionToCollection')->with($collection, $conditions)->willReturnSelf();
     $this->rule->expects($this->once())->method('loadPost')->willReturnSelf();
     $this->rule->expects($this->once())->method('getConditions')->willReturn($conditions);
     if ($productsPerPage) {
         $this->productsList->setData('products_per_page', $productsPerPage);
     } else {
         $this->productsList->unsetData('products_per_page');
     }
     $this->productsList->setData('show_pager', $pagerEnable);
     $this->productsList->setData('products_count', $productsCount);
     $this->assertSame($collection, $this->productsList->createCollection());
 }
示例#12
0
 /**
  * @return $this
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _prepareColumns()
 {
     $this->addColumn('entity_id', ['header' => __('ID'), 'type' => 'number', 'index' => 'entity_id', 'header_css_class' => 'col-id', 'column_css_class' => 'col-id']);
     $this->addColumn('name', ['header' => __('Name'), 'index' => 'name', 'class' => 'xxx']);
     $store = $this->_getStore();
     if ($store->getId()) {
         $this->addColumn('custom_name', ['header' => __('Name in %1', $store->getName()), 'index' => 'custom_name', 'header_css_class' => 'col-name', 'column_css_class' => 'col-name']);
     }
     $this->addColumn('type', ['header' => __('Type'), 'index' => 'type_id', 'type' => 'options', 'options' => $this->_type->getOptionArray()]);
     $sets = $this->_setsFactory->create()->setEntityTypeFilter($this->_productFactory->create()->getResource()->getTypeId())->load()->toOptionHash();
     $this->addColumn('set_name', ['header' => __('Product Template'), 'index' => 'attribute_set_id', 'type' => 'options', 'options' => $sets, 'header_css_class' => 'col-attr-name', 'column_css_class' => 'col-attr-name']);
     $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
     $store = $this->_getStore();
     $this->addColumn('price', ['header' => __('Price'), 'type' => 'price', 'currency_code' => $store->getBaseCurrency()->getCode(), 'index' => 'price', 'header_css_class' => 'col-price', 'column_css_class' => 'col-price']);
     if ($this->moduleManager->isEnabled('Magento_CatalogInventory')) {
         $this->addColumn('qty', ['header' => __('Quantity'), 'type' => 'number', 'index' => 'qty']);
     }
     $this->addColumn('visibility', ['header' => __('Visibility'), 'index' => 'visibility', 'type' => 'options', 'options' => $this->_visibility->getOptionArray(), 'header_css_class' => 'col-visibility', 'column_css_class' => 'col-visibility']);
     $this->addColumn('status', ['header' => __('Status'), 'index' => 'status', 'type' => 'options', 'options' => $this->_status->getOptionArray()]);
     if (!$this->_storeManager->isSingleStoreMode()) {
         $this->addColumn('websites', ['header' => __('Websites'), 'sortable' => false, 'index' => 'websites', 'type' => 'options', 'options' => $this->_websiteFactory->create()->getCollection()->toOptionHash(), 'header_css_class' => 'col-websites', 'column_css_class' => 'col-websites']);
     }
     $this->addColumn('edit', ['header' => __('Edit'), 'type' => 'action', 'getter' => 'getId', 'actions' => [['caption' => __('Edit'), 'url' => ['base' => '*/*/edit', 'params' => ['store' => $this->getRequest()->getParam('store')]], 'field' => 'id']], 'filter' => false, 'sortable' => false, 'index' => 'stores', 'header_css_class' => 'col-action', 'column_css_class' => 'col-action']);
     $block = $this->getLayout()->getBlock('grid.bottom.links');
     if ($block) {
         $this->setChild('grid.bottom.links', $block);
     }
     return parent::_prepareColumns();
 }
示例#13
0
 /**
  * Get category collection array
  *
  * @param null|string|bool|int|\Magento\Store\Model\Store $storeId
  * @return array|bool
  */
 public function getCollection($storeId)
 {
     $products = array();
     /* @var $store \Magento\Store\Model\Store */
     $store = $this->_storeManager->getStore($storeId);
     if (!$store) {
         return false;
     }
     $urConditions = array('e.entity_id = ur.product_id', 'ur.category_id IS NULL', $this->_getWriteAdapter()->quoteInto('ur.store_id = ?', $store->getId()), $this->_getWriteAdapter()->quoteInto('ur.is_system = ?', 1));
     $this->_select = $this->_getWriteAdapter()->select()->from(array('e' => $this->getMainTable()), array($this->getIdFieldName(), 'updated_at'))->joinInner(array('w' => $this->getTable('catalog_product_website')), 'e.entity_id = w.product_id', array())->joinLeft(array('ur' => $this->getTable('core_url_rewrite')), join(' AND ', $urConditions), array('url' => 'request_path'))->where('w.website_id = ?', $store->getWebsiteId());
     $this->_addFilter($store->getId(), 'visibility', $this->_productVisibility->getVisibleInSiteIds(), 'in');
     $this->_addFilter($store->getId(), 'status', $this->_productStatus->getVisibleStatusIds(), 'in');
     // Join product images required attributes
     $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId());
     if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) {
         $this->_joinAttribute($store->getId(), 'name');
         $this->_select->columns(array('name' => $this->getReadConnection()->getIfNullSql('t2_name.value', 't1_name.value')));
         if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) {
             $this->_joinAttribute($store->getId(), 'thumbnail');
             $this->_select->columns(array('thumbnail' => $this->getReadConnection()->getIfNullSql('t2_thumbnail.value', 't1_thumbnail.value')));
         } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) {
             $this->_joinAttribute($store->getId(), 'image');
             $this->_select->columns(array('image' => $this->getReadConnection()->getIfNullSql('t2_image.value', 't1_image.value')));
         }
     }
     $query = $this->_getWriteAdapter()->query($this->_select);
     while ($row = $query->fetch()) {
         $product = $this->_prepareProduct($row, $store->getId());
         $products[$product->getId()] = $product;
     }
     return $products;
 }
示例#14
0
 /**
  * Get crosssell products collection
  *
  * @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
  */
 protected function _getCollection()
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection $collection */
     $collection = $this->_productLinkFactory->create()->useCrossSellLinks()->getProductCollection()->setStoreId($this->_storeManager->getStore()->getId())->addStoreFilter()->setPageSize($this->_maxItemCount)->setVisibility($this->_productVisibility->getVisibleInCatalogIds());
     $this->_addProductAttributesAndPrices($collection);
     return $collection;
 }
示例#15
0
 /**
  * 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;
 }
示例#16
0
 /**
  * @return string
  */
 protected function _toHtml()
 {
     $storeId = $this->_getStoreId();
     $storeModel = $this->_storeManager->getStore($storeId);
     $newUrl = $this->_urlBuilder->getUrl('rss/catalog/new/store_id/' . $storeId);
     $title = __('New Products from %1', $storeModel->getFrontendName());
     $lang = $this->_scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeModel);
     /** @var $rssObj \Magento\Rss\Model\Rss */
     $rssObj = $this->_rssFactory->create();
     $rssObj->_addHeader(array('title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8', 'language' => $lang));
     /** @var $product \Magento\Catalog\Model\Product */
     $product = $this->_productFactory->create();
     $todayStartOfDayDate = $this->_localeDate->date()->setTime('00:00:00')->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
     $todayEndOfDayDate = $this->_localeDate->date()->setTime('23:59:59')->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
     /** @var $products \Magento\Catalog\Model\Resource\Product\Collection */
     $products = $product->getCollection();
     $products->setStoreId($storeId);
     $products->addStoreFilter()->addAttributeToFilter('news_from_date', array('or' => array(0 => array('date' => true, 'to' => $todayEndOfDayDate), 1 => array('is' => new \Zend_Db_Expr('null')))), 'left')->addAttributeToFilter('news_to_date', array('or' => array(0 => array('date' => true, 'from' => $todayStartOfDayDate), 1 => array('is' => new \Zend_Db_Expr('null')))), 'left')->addAttributeToFilter(array(array('attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')), array('attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null'))))->addAttributeToSort('news_from_date', 'desc')->addAttributeToSelect(array('name', 'short_description', 'description'), 'inner')->addAttributeToSelect(array('price', 'special_price', 'special_from_date', 'special_to_date', 'msrp_enabled', 'msrp_display_actual_price_type', 'msrp', 'thumbnail'), 'left')->applyFrontendPriceLimitations();
     $products->setVisibility($this->_visibility->getVisibleInCatalogIds());
     /*
     using resource iterator to load the data one by one
     instead of loading all at the same time. loading all data at the same time can cause the big memory allocation.
     */
     $this->_resourceIterator->walk($products->getSelect(), array(array($this, 'addNewItemXmlCallback')), array('rssObj' => $rssObj, 'product' => $product));
     return $rssObj->createRssXml();
 }
示例#17
0
文件: Grid.php 项目: aiesh/magento2
 /**
  * @return $this
  */
 protected function _prepareColumns()
 {
     $this->addColumn('entity_id', array('header' => __('ID'), 'type' => 'number', 'index' => 'entity_id', 'header_css_class' => 'col-id', 'column_css_class' => 'col-id'));
     $this->addColumn('name', array('header' => __('Name'), 'index' => 'name', 'class' => 'xxx'));
     $store = $this->_getStore();
     if ($store->getId()) {
         $this->addColumn('custom_name', array('header' => __('Name in %1', $store->getName()), 'index' => 'custom_name', 'header_css_class' => 'col-name', 'column_css_class' => 'col-name'));
     }
     $this->addColumn('type', array('header' => __('Type'), 'index' => 'type_id', 'type' => 'options', 'options' => $this->_type->getOptionArray()));
     $sets = $this->_setsFactory->create()->setEntityTypeFilter($this->_productFactory->create()->getResource()->getTypeId())->load()->toOptionHash();
     $this->addColumn('set_name', array('header' => __('Attribute Set'), 'index' => 'attribute_set_id', 'type' => 'options', 'options' => $sets, 'header_css_class' => 'col-attr-name', 'column_css_class' => 'col-attr-name'));
     $this->addColumn('sku', array('header' => __('SKU'), 'index' => 'sku'));
     $store = $this->_getStore();
     $this->addColumn('price', array('header' => __('Price'), 'type' => 'price', 'currency_code' => $store->getBaseCurrency()->getCode(), 'index' => 'price', 'header_css_class' => 'col-price', 'column_css_class' => 'col-price'));
     if ($this->_catalogData->isModuleEnabled('Magento_CatalogInventory')) {
         $this->addColumn('qty', array('header' => __('Quantity'), 'type' => 'number', 'index' => 'qty'));
     }
     $this->addColumn('visibility', array('header' => __('Visibility'), 'index' => 'visibility', 'type' => 'options', 'options' => $this->_visibility->getOptionArray(), 'header_css_class' => 'col-visibility', 'column_css_class' => 'col-visibility'));
     $this->addColumn('status', array('header' => __('Status'), 'index' => 'status', 'type' => 'options', 'options' => $this->_status->getOptionArray()));
     if (!$this->_storeManager->isSingleStoreMode()) {
         $this->addColumn('websites', array('header' => __('Websites'), 'sortable' => false, 'index' => 'websites', 'type' => 'options', 'options' => $this->_websiteFactory->create()->getCollection()->toOptionHash(), 'header_css_class' => 'col-websites', 'column_css_class' => 'col-websites'));
     }
     $this->addColumn('edit', array('header' => __('Edit'), 'type' => 'action', 'getter' => 'getId', 'actions' => array(array('caption' => __('Edit'), 'url' => array('base' => '*/*/edit', 'params' => array('store' => $this->getRequest()->getParam('store'))), 'field' => 'id')), 'filter' => false, 'sortable' => false, 'index' => 'stores', 'header_css_class' => 'col-action', 'column_css_class' => 'col-action'));
     if ($this->_catalogData->isModuleEnabled('Magento_Rss')) {
         $this->addRssList('rss/catalog/notifystock', __('Notify Low Stock RSS'));
     }
     return parent::_prepareColumns();
 }
示例#18
0
 /**
  * Get category collection array
  *
  * @param null|string|bool|int|\Magento\Store\Model\Store $storeId
  * @return array|bool
  */
 public function getCollection($storeId)
 {
     $products = [];
     /* @var $store \Magento\Store\Model\Store */
     $store = $this->_storeManager->getStore($storeId);
     if (!$store) {
         return false;
     }
     $adapter = $this->_getWriteAdapter();
     $this->_select = $adapter->select()->from(['e' => $this->getMainTable()], [$this->getIdFieldName(), 'updated_at'])->joinInner(['w' => $this->getTable('catalog_product_website')], 'e.entity_id = w.product_id', [])->joinLeft(['url_rewrite' => $this->getTable('url_rewrite')], 'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1' . $adapter->quoteInto(' AND url_rewrite.store_id = ?', $store->getId()) . $adapter->quoteInto(' AND url_rewrite.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE), ['url' => 'request_path'])->where('w.website_id = ?', $store->getWebsiteId());
     $this->_addFilter($store->getId(), 'visibility', $this->_productVisibility->getVisibleInSiteIds(), 'in');
     $this->_addFilter($store->getId(), 'status', $this->_productStatus->getVisibleStatusIds(), 'in');
     // Join product images required attributes
     $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId());
     if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) {
         $this->_joinAttribute($store->getId(), 'name');
         $this->_select->columns(['name' => $this->getReadConnection()->getIfNullSql('t2_name.value', 't1_name.value')]);
         if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) {
             $this->_joinAttribute($store->getId(), 'thumbnail');
             $this->_select->columns(['thumbnail' => $this->getReadConnection()->getIfNullSql('t2_thumbnail.value', 't1_thumbnail.value')]);
         } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) {
             $this->_joinAttribute($store->getId(), 'image');
             $this->_select->columns(['image' => $this->getReadConnection()->getIfNullSql('t2_image.value', 't1_image.value')]);
         }
     }
     $query = $adapter->query($this->_select);
     while ($row = $query->fetch()) {
         $product = $this->_prepareProduct($row, $store->getId());
         $products[$product->getId()] = $product;
     }
     return $products;
 }
示例#19
0
 /**
  * Calculate count of product index items cache
  *
  * @return $this
  */
 public function calculate()
 {
     $collection = $this->getCollection()->setCustomerId($this->getCustomerId())->addIndexFilter()->setVisibility($this->_productVisibility->getVisibleInSiteIds());
     $count = $collection->getSize();
     $this->_getSession()->setData($this->_countCacheKey, $count);
     return $this;
 }
示例#20
0
 public function testGetFlatColumns()
 {
     $abstractAttributeMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', ['getAttributeCode', '__wakeup'], [], '', false);
     $abstractAttributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('code'));
     $this->_model->setAttribute($abstractAttributeMock);
     $flatColumns = $this->_model->getFlatColumns();
     $this->assertTrue(is_array($flatColumns), 'FlatColumns must be an array value');
     $this->assertTrue(!empty($flatColumns), 'FlatColumns must be not empty');
     foreach ($flatColumns as $result) {
         $this->assertArrayHasKey('unsigned', $result, 'FlatColumns must have "unsigned" column');
         $this->assertArrayHasKey('default', $result, 'FlatColumns must have "default" column');
         $this->assertArrayHasKey('extra', $result, 'FlatColumns must have "extra" column');
         $this->assertArrayHasKey('type', $result, 'FlatColumns must have "type" column');
         $this->assertArrayHasKey('nullable', $result, 'FlatColumns must have "nullable" column');
         $this->assertArrayHasKey('comment', $result, 'FlatColumns must have "comment" column');
     }
 }
示例#21
0
 /**
  * Prepare and return product collection
  *
  * @return \Magento\Catalog\Model\Resource\Product\Collection|Object|\Magento\Framework\Data\Collection
  */
 protected function _getProductCollection()
 {
     $todayStartOfDayDate = $this->_localeDate->date()->setTime('00:00:00')->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
     $todayEndOfDayDate = $this->_localeDate->date()->setTime('23:59:59')->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
     /** @var $collection \Magento\Catalog\Model\Resource\Product\Collection */
     $collection = $this->_productCollectionFactory->create();
     $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
     $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter()->addAttributeToFilter('news_from_date', array('or' => array(0 => array('date' => true, 'to' => $todayEndOfDayDate), 1 => array('is' => new \Zend_Db_Expr('null')))), 'left')->addAttributeToFilter('news_to_date', array('or' => array(0 => array('date' => true, 'from' => $todayStartOfDayDate), 1 => array('is' => new \Zend_Db_Expr('null')))), 'left')->addAttributeToFilter(array(array('attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')), array('attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null'))))->addAttributeToSort('news_from_date', 'desc')->setPageSize($this->getProductsCount())->setCurPage(1);
     return $collection;
 }
示例#22
0
 /**
  * Prepare and return product collection
  *
  * @return \Magento\Catalog\Model\Resource\Product\Collection|Object|\Magento\Framework\Data\Collection
  */
 protected function _getProductCollection()
 {
     $todayStartOfDayDate = $this->_localeDate->date()->setTime(0, 0, 0)->format('Y-m-d H:i:s');
     $todayEndOfDayDate = $this->_localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s');
     /** @var $collection \Magento\Catalog\Model\Resource\Product\Collection */
     $collection = $this->_productCollectionFactory->create();
     $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
     $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter()->addAttributeToFilter('news_from_date', ['or' => [0 => ['date' => true, 'to' => $todayEndOfDayDate], 1 => ['is' => new \Zend_Db_Expr('null')]]], 'left')->addAttributeToFilter('news_to_date', ['or' => [0 => ['date' => true, 'from' => $todayStartOfDayDate], 1 => ['is' => new \Zend_Db_Expr('null')]]], 'left')->addAttributeToFilter([['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')], ['attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null')]])->addAttributeToSort('news_from_date', 'desc')->setPageSize($this->getProductsCount())->setCurPage(1);
     return $collection;
 }
示例#23
0
 /**
  * {@inheritdoc}
  */
 protected function _prepareColumns()
 {
     $this->addColumn('in_products', ['type' => 'checkbox', 'name' => 'in_products', 'values' => $this->getSelectedProducts(), 'align' => 'center', 'index' => 'entity_id', 'header_css_class' => 'col-select', 'column_css_class' => 'col-select']);
     $this->addColumn('position', ['header' => __('Position'), 'name' => 'position', 'type' => 'number', 'validate_class' => 'validate-number', 'index' => 'position', 'editable' => true, 'is_system' => 1, 'header_css_class' => 'col-hidden', 'column_css_class' => 'col-hidden']);
     $this->addColumn('id', ['header' => __('ID'), 'type' => 'number', 'index' => 'entity_id']);
     $this->addColumn('name', ['header' => __('Name'), 'index' => 'name']);
     $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
     $this->addColumn('status', ['header' => __('Status'), 'index' => 'status', 'type' => 'options', 'options' => $this->status->getOptionArray()]);
     $this->addColumn('visibility', ['header' => __('Visibility'), 'index' => 'visibility', 'type' => 'options', 'options' => $this->visibility->getOptionArray()]);
 }
示例#24
0
文件: Upsell.php 项目: nja78/magento2
 /**
  * @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;
 }
示例#25
0
 /**
  * Prepare and return product collection
  *
  * @return \Magento\Catalog\Model\Resource\Product\Collection
  */
 public function createCollection()
 {
     /** @var $collection \Magento\Catalog\Model\Resource\Product\Collection */
     $collection = $this->productCollectionFactory->create();
     $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
     $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter()->setPageSize($this->getProductsPerPage())->setCurPage($this->getRequest()->getParam(self::PAGE_VAR_NAME, 1));
     $conditions = $this->getConditions();
     $conditions->collectValidatedAttributes($collection);
     $this->sqlBuilder->attachConditionToCollection($collection, $conditions);
     return $collection;
 }
示例#26
0
 public function getProductCollection()
 {
     $brand = $this->getBrand();
     $collection = $this->_productCollectionFactory->create();
     $collection->addAttributeToSelect('*');
     $collection->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
     //     	var_dump(get_class_methods($collection));
     //     	die;
     $collection->addAttributeToSelect('name');
     $collection->addStoreFilter()->addAttributeToFilter('manufacturer', $brand->getAttributeId());
     //     	var_dump(count($collection));
     return $collection;
 }
 public function testGetProductsCollection()
 {
     /** @var \DateTime|\PHPUnit_Framework_MockObject_MockObject $dateObject */
     $dateObject = $this->getMock('DateTime');
     $dateObject->expects($this->any())->method('setTime')->will($this->returnSelf());
     $dateObject->expects($this->any())->method('format')->will($this->returnValue(date(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)));
     $this->timezone->expects($this->exactly(2))->method('date')->will($this->returnValue($dateObject));
     /** @var \Magento\Catalog\Model\Resource\Product\Collection $productCollection */
     $productCollection = $this->getMock('Magento\\Catalog\\Model\\Resource\\Product\\Collection', [], [], '', false);
     $this->product->expects($this->once())->method('getResourceCollection')->will($this->returnValue($productCollection));
     $storeId = 1;
     $productCollection->expects($this->once())->method('setStoreId')->with($storeId);
     $productCollection->expects($this->once())->method('addStoreFilter')->will($this->returnSelf());
     $productCollection->expects($this->any())->method('addAttributeToFilter')->will($this->returnSelf());
     $productCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf());
     $productCollection->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf());
     $productCollection->expects($this->once())->method('applyFrontendPriceLimitations')->will($this->returnSelf());
     $visibleIds = [1, 3];
     $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->will($this->returnValue($visibleIds));
     $productCollection->expects($this->once())->method('setVisibility')->with($visibleIds)->will($this->returnSelf());
     $products = $this->newProducts->getProductsCollection($storeId);
     $this->assertEquals($productCollection, $products);
 }
 /**
  * Add columns to grid
  *
  * @return $this
  */
 protected function _prepareColumns()
 {
     if (!$this->isReadonly()) {
         $this->addColumn('in_products', ['type' => 'checkbox', 'name' => 'in_products', 'values' => $this->_getSelectedProducts(), 'align' => 'center', 'index' => 'entity_id', 'header_css_class' => 'col-select', 'column_css_class' => 'col-select']);
     }
     $this->addColumn('entity_id', ['header' => __('ID'), 'sortable' => true, 'index' => 'entity_id', 'header_css_class' => 'col-id', 'column_css_class' => 'col-id']);
     $this->addColumn('name', ['header' => __('Name'), 'index' => 'name', 'header_css_class' => 'col-name', 'column_css_class' => 'col-name']);
     $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku', 'header_css_class' => 'col-sku', 'column_css_class' => 'col-sku']);
     $this->addColumn('visibility', ['header' => __('Visibility'), 'index' => 'visibility', 'type' => 'options', 'options' => $this->_visibility->getOptionArray(), 'header_css_class' => 'col-visibility', 'column_css_class' => 'col-visibility']);
     if (!$this->_storeManager->isSingleStoreMode()) {
         $this->addColumn('websites', ['header' => __('Websites'), 'sortable' => false, 'index' => 'websites', 'type' => 'options', 'options' => $this->_websiteFactory->create()->getCollection()->toOptionHash(), 'header_css_class' => 'col-websites', 'column_css_class' => 'col-websites']);
     }
     $this->addColumn('status', ['header' => __('Status'), 'index' => 'status', 'type' => 'options', 'options' => $this->_status->getOptionArray(), 'header_css_class' => 'col-status', 'column_css_class' => 'col-status', 'frame_callback' => array($this->getLayout()->createBlock('Magefan\\Blog\\Block\\Adminhtml\\Grid\\Column\\Statuses'), 'decorateStatus')]);
     return parent::_prepareColumns();
 }
示例#29
0
 /**
  * Retrieve Product Compare items collection
  *
  * @return \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection
  */
 public function getItems()
 {
     if ($this->_items === null) {
         $this->_compareProduct->setAllowUsedFlat(false);
         $this->_items = $this->_itemCollectionFactory->create();
         $this->_items->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId());
         if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
             $this->_items->setCustomerId($this->currentCustomer->getCustomerId());
         } elseif ($this->_customerId) {
             $this->_items->setCustomerId($this->_customerId);
         } else {
             $this->_items->setVisitorId($this->_customerVisitor->getId());
         }
         $this->_items->addAttributeToSelect($this->_catalogConfig->getProductAttributes())->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
     }
     return $this->_items;
 }
示例#30
0
文件: Upsell.php 项目: aiesh/magento2
 /**
  * Add columns to grid
  *
  * @return $this
  */
 protected function _prepareColumns()
 {
     if (!$this->getProduct()->getUpsellReadonly()) {
         $this->addColumn('in_products', array('type' => 'checkbox', 'name' => 'in_products', 'values' => $this->_getSelectedProducts(), 'align' => 'center', 'index' => 'entity_id', 'header_css_class' => 'col-select', 'column_css_class' => 'col-select'));
     }
     $this->addColumn('entity_id', array('header' => __('ID'), 'sortable' => true, 'index' => 'entity_id', 'header_css_class' => 'col-id', 'column_css_class' => 'col-id'));
     $this->addColumn('name', array('header' => __('Name'), 'index' => 'name', 'header_css_class' => 'col-name', 'column_css_class' => 'col-name'));
     $this->addColumn('type', array('header' => __('Type'), 'index' => 'type_id', 'type' => 'options', 'options' => $this->_type->getOptionArray(), 'header_css_class' => 'col-type', 'column_css_class' => 'col-type'));
     $sets = $this->_setsFactory->create()->setEntityTypeFilter($this->_productFactory->create()->getResource()->getTypeId())->load()->toOptionHash();
     $this->addColumn('set_name', array('header' => __('Attribute Set'), 'index' => 'attribute_set_id', 'type' => 'options', 'options' => $sets, 'header_css_class' => 'col-attr-name', 'column_css_class' => 'col-attr-name'));
     $this->addColumn('status', array('header' => __('Status'), 'index' => 'status', 'type' => 'options', 'options' => $this->_status->getOptionArray(), 'header_css_class' => 'col-status', 'column_css_class' => 'col-status'));
     $this->addColumn('visibility', array('header' => __('Visibility'), 'index' => 'visibility', 'type' => 'options', 'options' => $this->_visibility->getOptionArray(), 'header_css_class' => 'col-visibility', 'column_css_class' => 'col-visibility'));
     $this->addColumn('sku', array('header' => __('SKU'), 'index' => 'sku', 'header_css_class' => 'col-sku', 'column_css_class' => 'col-sku'));
     $this->addColumn('price', array('header' => __('Price'), 'type' => 'currency', 'currency_code' => (string) $this->_scopeConfig->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE), 'index' => 'price', 'header_css_class' => 'col-price', 'column_css_class' => 'col-price'));
     $this->addColumn('position', array('header' => __('Position'), 'name' => 'position', 'type' => 'number', 'validate_class' => 'validate-number', 'index' => 'position', 'editable' => !$this->getProduct()->getUpsellReadonly(), 'edit_only' => !$this->getProduct()->getId(), 'header_css_class' => 'col-position', 'column_css_class' => 'col-position', 'filter_condition_callback' => array($this, 'filterProductPosition')));
     return parent::_prepareColumns();
 }