本文整理汇总了PHP中Magento\Catalog\Model\Product\Visibility类的典型用法代码示例。如果您正苦于以下问题:PHP Visibility类的具体用法?PHP Visibility怎么用?PHP Visibility使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Visibility类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Enable requested attribute for products, if no products are using it
*
* Date attributes is supported for attribute_from values only.
*
* @param array $data Key => Value pairs of attribute_code and count
* @return void
*/
public function execute($data)
{
$visibility = $this->catalogProductVisibility->getVisibleInCatalogIds();
$attributes = $this->attributeCollectionFactory->create()->addFieldToFilter('attribute_code', ['in' => array_keys($data)]);
foreach ($attributes as $attribute) {
$collection = $this->productCollectionFactory->create()->setPageSize(1)->setCurPage(1);
switch ($attribute->getFrontendInput()) {
case 'boolean':
$value = 1;
$collection->addAttributeToFilter($attribute, 1);
break;
case 'date':
$value = $this->localeDate->date()->format('Y-m-d H:i:s');
$collection->addAttributeToFilter($attribute, [['date' => true, 'to' => $value]]);
break;
}
if ($collection->getSize()) {
// customer already has some products with specified attribute
continue;
}
foreach ($this->getStoreIds() as $storeId) {
$visibleProducts = $this->productCollectionFactory->create()->setStoreId($storeId)->setVisibility($visibility)->addStoreFilter($storeId)->setPageSize($data[$attribute->getAttributeCode()])->setCurPage(1);
if (!$visibleProducts->getSize()) {
continue;
}
foreach ($visibleProducts as $product) {
$product->addAttributeUpdate($attribute->getAttributeCode(), (int) in_array(0, $this->getStoreIds()), 0);
$product->setStoreId($storeId)->setData($attribute->getAttributeCode(), $value)->save();
}
}
}
}
示例2: testGetProductCollection
/**
* @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: _getProductCollection
/**
* @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;
}
示例4: getFeaturedProduct
/**
* get featured product collection
*/
public function getFeaturedProduct()
{
$limit = $this->getProductLimit();
$collection = $this->_productCollectionFactory->create();
$collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$collection->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setPageSize($limit)->addAttributeToSelect('*');
$collection->addAttributeToFilter('et_featured', '1');
return $collection;
}
示例5: toOptionArray
/**
* 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;
}
示例6: execute
/**
* 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: getProductsCollection
/**
* @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: getProductCollection
/**
* @param \Magento\Catalog\Model\Category $category
* @param int $storeId
* @return $this
*/
public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId)
{
/** @var $layer \Magento\Catalog\Model\Layer */
$layer = $this->catalogLayer->setStore($storeId);
$collection = $category->getResourceCollection();
$collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load();
/** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$productCollection = $this->collectionFactory->create();
$currentCategory = $layer->setCurrentCategory($category);
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
$category->getProductCollection()->setStoreId($storeId);
$products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50);
return $products;
}
示例9: _prepareData
/**
* @return $this
*/
protected function _prepareData()
{
$product = $this->_coreRegistry->registry('product');
/* @var $product \Magento\Catalog\Model\Product */
$this->_itemCollection = $product->getRelatedProductCollection()->addAttributeToSelect('required_options')->setPositionOrder()->addStoreFilter();
if ($this->moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例10: _prepareCollection
/**
* Premare block data
* @return $this
*/
protected function _prepareCollection()
{
$post = $this->_coreRegistry->registry('current_blog_post');
$this->_itemCollection = $this->_productCollectionFactory->create()->addAttributeToSelect('required_options')->addStoreFilter()->addAttributeToFilter('entity_id', array('in' => $post->getRelatedProductIds() ?: array(0)));
if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->setPageSize((int) $this->_scopeConfig->getValue('mfblog/post_view/related_products/number_of_products', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
示例11: testCreateCollection
/**
* 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: _prepareColumns
/**
* @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: getCollection
/**
* 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: _getCollection
/**
* 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: _prepareCollection
/**
* Premare block data
* @return $this
*/
protected function _prepareCollection()
{
$post = $this->getPost();
$this->_itemCollection = $post->getRelatedProducts()->addAttributeToSelect('required_options');
if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
$this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$this->_itemCollection->setPageSize((int) $this->_scopeConfig->getValue('mfblog/post_view/related_products/number_of_products', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$this->_itemCollection->getSelect()->order('rl.position', 'ASC');
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}