1

How to get the product by price range through object manager in phtml file. Like following

Get product where price is greater than 1 and less than 25

3 Answers 3

2

Try it.

<?php

//$productId = 100;
$sku = "testsku";
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

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

$collection = $prodCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('price', array('in' => range(1, 25)));
foreach ($collection as $product){
     echo 'Name =  '.$product->getName().'<br>';
     echo 'Id  =  '.$product->getId().'<br>';
     echo 'Sku  =  '.$product->getSku().'<br>';
     echo 'Image' =  '.$product->getImage().'<br>';
}  

?>
4
  • Hero Bro Thanks. Thank you so much please tell how to get image also. Commented Mar 1, 2018 at 10:51
  • check update code @Bunny Xudot Commented Mar 1, 2018 at 11:19
  • now code work for you please accept and upvot my answer.@Bunny Xudot Commented Mar 1, 2018 at 11:22
  • @Ravindrasinh Zala, Bunny Xudot haven't enough reputation point for upvote. behalf of him i'll do that... Commented Mar 1, 2018 at 11:31
0

create a object of abstractProduct Block then call the getProductPrice method and pass product object as parameter. see below.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToSort('created_at', 'DESC')
            ->setPageSize(8)
            ->load();
?>
<div>
    <?php foreach ($collection as $product) :?>
        <div class="item-box">
            <a href="<?php echo $product->getProductUrl(); ?>" >
                <span class="imgbx">
                    <img src="<?php echo $abstractProductBlock->getImage($product, 'latest_collection_list')->getImageUrl(); ?>" alt="<?php echo $product->getName(); ?>" />
                </span>
                <h3><?php echo $product->getName(); ?></h3>
                <span class="hm-price"><?php echo $abstractProductBlock->getProductPrice($product) ?></span>
            </a>
        </div>
    <?php endforeach;  ?>
</div>
?>
0

Try this.

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$products = $objectManager->create("Magento\Catalog\Model\Product")->getCollection()
            ->addAttributeToFilter('price', array('in' => range(1, 25)));

foreach ($products as $product) {
    echo "<pre>";
    print_r($product->getData());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.