php Magento-Catalog-Model-Product-Visibility类(方法)实例源码

下面列出了php Magento-Catalog-Model-Product-Visibility 类(方法)源码代码实例,从而了解它的用法。

作者: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();
             }
         }
     }
 }

作者:pradeep-wagent    项目:magento   
/**
  * @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));
 }

作者:sz-bil    项目:Magento2.   
/**
  * @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;
 }

作者: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;
 }

作者:dotmaile    项目:dotmailer-magento2-extensio   
/**
  * 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;
 }

作者:pradeep-wagent    项目:magento   
/**
  * 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;
 }

作者:pradeep-wagent    项目:magento   
/**
  * @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;
 }

作者:pradeep-wagent    项目:magento   
/**
  * @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;
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * @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;
 }

作者: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;
 }

作者:nja7    项目:magento   
/**
  * 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());
 }

作者:whoopl    项目:magento2-testin   
/**
  * @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();
 }

作者:pavelnovitsk    项目:magento   
/**
  * 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;
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * 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;
 }

作者:magefa    项目:module-blo   
/**
  * 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;
 }

作者:aies    项目:magento   
/**
  * @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();
 }

作者:aies    项目:magento   
/**
  * @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();
 }

作者:shabbirvividad    项目:magento   
/**
  * 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;
 }

作者:nja7    项目:magento   
/**
  * 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;
 }

作者:Atli    项目:docker-magento   
/**
  * 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;
 }


问题


面经


文章

微信
公众号

扫码关注公众号