Category: magento

Get List of brand attribute options and swatch images + magento 2

$attributeCode = ‘brand’;
$entityType = ‘catalog_product’;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
->loadByCode($entityType, $attributeCode);
$attributeId = $attributeInfo->getAttributeId();
$attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
->setPositionOrder(‘asc’)
->setAttributeFilter($attributeId)
->setStoreFilter()
->load();
$optionsAll = $attributeOptionAll->getData();
foreach($optionsAll as $_option){
$optionId = $_option[‘option_id’];

$resource = $objectManager->get(‘Magento\Framework\App\ResourceConnection’);
$connection = $resource->getConnection();
$tableName = $resource->getTableName(‘eav_attribute_option_swatch’); //gives table name with prefix
$sql = “Select * FROM “.$tableName.” where option_id = “.$optionId;
$result = $connection->fetchAll($sql);
$swatchId = $result[0][‘swatch_id’];

$swatch = $objectManager->create(‘Magento\Swatches\Model\Swatch’)->load($swatchId);
$imageName=$swatch->getData(‘value’);
if(!empty($imageName)){
$imagePath=$objectManager->create(‘Magento\Swatches\Helper\Media’)->getSwatchMediaUrl().$imageName;
?>

” />

<?php
}

}

Get Products from category Id + Magento2

 

$productCollectionFactory = $objectManager->get(‘\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory’);
$categoryCollectionFactory = $objectManager->get(‘\Magento\Catalog\Model\CategoryFactory’);
$category = $categoryCollectionFactory->create()->load($categoryId);
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect(‘*’);
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter(‘visibility’, \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter(‘status’,\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

foreach ($collection as $product) {
echo “<pre>”;
print_r($product->getData());
echo “</pre>”;
}

 

 

reference: https://www.emiprotechnologies.com/technical_notes/magento-technical-notes-60/post/different-ways-to-get-product-collection-in-magento2-673

Get Current Category Id + Magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get(‘Magento\Framework\Registry’)->registry(‘current_category’);//get current category
echo $category->getId();
echo $category->getName();