Skip to main content

Get latest products magento 2

 

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager
    ->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(); 
 foreach ($collection as $product){
    echo 'Name = '.$product->getName().'<br/>';
}
 

foreach ($collection as $product)
{
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();

<div class="item">
<a href="<?= $product->getProductUrl(); ?>">
<div class="product-home">
<span class="tag">NEU</span>
<img src="<?php echo $imageUrl; ?>" alt="product1">
</div>
<h4><?php echo $product->getName();?></h4>
<p><span><?= $product->getPrice(); ?> €</span> 11,40 € für Mitglieder</p>
</a>
</div>
}
 
 /** Get product attributes  **/
$attributes = $product->getAttributes();
foreach($attributes as $a)
{
if (null !== $product->getCustomAttribute('featured')) {
echo $product->getCustomAttribute('featured')->getValue();
}
echo '<pre>';
print_r($a->getName());
echo '</pre>';

//echo $a->getName()."\n";
 

Comments

Popular posts from this blog

Magento 2: Category list for custom magento system configuration section ( Backend )

In system.xml file field for multi select of category is like: NOTE: Use Select for Single item and multiselect for multiple in - <field id = "bannerlist" translate = "label" type = " multiselect " <group id = "bannerblock_setting" translate = "label" type = "text" delault = "1" sortOrder = "3" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Setting </label> <field id = "bannerlist" translate = "label" type = "multiselect" sortOrder = "10" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Select Category </label> <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>--> <source_model> Ipragmatech\Bannerblock\Model\Config\Source\C...

Magento 2: Get Products by category ID

<?php $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        // $appState = $objectManager->get('\Magento\Framework\App\State'); // $appState->setAreaCode('frontend'); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category'); $categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository'); $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(); $categoryId = 47; // YOUR CATEGORY ID $category = $categoryFactory->create()->load($categoryId); $categoryProducts = $category->getProductCollection()                              ->addAttributeToSelect('*');       ...