我试图展示品牌特有的产品。品牌是我的属性之一,这是强制性的,并附在每一个产品。
我为每个品牌在一个网站下创建了不同的商店,也为每个品牌创建了不同的URL。因此,我想展示产品品牌明智的每一个品牌商店。
因此,哪一种最简单的方式来过滤产品的属性即品牌。
我使用的是Magento 2.1.2,MySQL 6,PHP7.0
发布于 2017-04-03 08:44:49
使用以下代码按存储ID筛选产品集合:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*')
$collection->addStoreFilter($storeid)
$collection->addAttributeToFilter('attribute_code');发布于 2019-06-03 12:56:15
使用此代码可以通过存储id和属性代码筛选产品集合。
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
protected $collectionFactory
public function __construct(CollectionFactory $collectionFactory)
{
$this->collectionFactory =$collectionFactory;
}
public function productCollection($storeId ,$attributeCode)
{
$collection = $collectionFactory->create();
$collection->addAttributeToSelect('*')
$collection->addStoreFilter($storeId)
$collection->addAttributeToFilter($attributeCode);
return $collection;
}发布于 2018-04-03 10:57:15
use Magento\Framework\Data\OptionSourceInterface;
class Productlist implements OptionSourceInterface{
/**
* Get products
*
* @return array
*/
public function toOptionArray(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$store=$objectManager->create('\Magento\Store\Model\StoreRepository');
$productCollectionFactory =$objectManager->create('\Magento\Catalog\Model\Product')->getCollection();
$storeId='3';
$productCollectionFactory->addAttributeToSelect('name');
$rootCategoryId = $store->getById($storeId)->getRootCategoryId();
$productCollectionFactory->addAttributeToSelect('*');
$productCollectionFactory->addCategoriesFilter(array('eq' => $rootCategoryId));
$options = [];
foreach ($productCollectionFactory as $product) {
$options[] = ['label' => $product->getName(), 'value' => $product->getId()];
}
return $options;
}https://stackoverflow.com/questions/40792145
复制相似问题