0

I have some product ids . Currently I can display products in category using {{block . But now I am developing a custom block in

app/design/frontend/Alothemes/bencher6/Magento_Theme/templates/myphp/myphpcode.phtml 

then in myphpcode.phtml i write the following query

$productId = 672;
   $_product = Mage::getModel('catalog/product')->load(672);

But I got Uncaught Error: Class 'Mage' not found magento .

So how can I list selected product block on a page .

When i try the following code

$product=$this->getLoadProduct(672);
echo $product->getName();

Uncaught Error: Call to a member function getName() on null

I am calling this in cms page using following code

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::myphp/myphpcode.phtml"}}
0

please try with below code : ( you are using M1 code )

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  • Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 1608519680 bytes) in /var/www/html/app/design/frontend/Alothemes/bencher6/Magento_Theme/templates/myphp/myphpcode.phtml – John Aug 20 at 11:20
  • increase memory limit 1024M in htaccess or php.ini – Anas Mansuri Aug 20 at 11:23
0

Please check with the following code

$productId = "10"; //Product Id

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

echo $product->getName(); //Get Product Name

I hope it will help you.

  • Yes this is working . But i want the entire product block , not the name only – John Aug 20 at 11:31
  • You can replace echo $product->getName(); to echo $product->getData(); and check please. – Monark Bhawani Aug 20 at 11:38
  • We're sorry, an error has occurred while generating this content. – John Aug 20 at 11:39
  • Can you describe what you need exactly?. – Monark Bhawani Aug 20 at 11:42
  • i want to show the entire product block with name, image , price etc . For example if we want to display product in a cateogry , in cms block we write {{widget type="Magento\CatalogWidget\Block\Product\ProductsList" ....etc ... category_ids,operator:==,value:1^]^]"}} But if we want display products with ids 32, 33, 34 what we will do ? – John Aug 20 at 11:47
0
<?php 
$objectManager = Magento\Framework\App\ObjectManager::getInstance();
$products = $objectManager->get('Magento\Catalog\Model\Product')
            ->getCollection()
            ->addAttributeToFilter('entity_id',array('product_id1','product_id2'));
echo "<pre>";
print_r($products->getData());
?>
  • How can i call this in cms page . Before i am using following code {{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::myphp/myphpcode.phtml"}} – John Aug 20 at 11:28
  • i want the entire product block , not the name only – John Aug 20 at 11:32
  • John, Please check my updated code. – Flying Finner Aug 20 at 12:04
0

Try Below Code:-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();
foreach($collection as $product):
    if($product->getProductsShowAttribute() == 1):
        echo $product->getEntityId();
        echo $product->getName();
     endif;
endforeach;
  • but where i can write the id – John Aug 20 at 11:53
  • you can create a new attribute and this attribute set to product after that you get this product only you have to set this attibute – Devidas Aug 20 at 11:59
  • i have update my code please check it – Devidas Aug 20 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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