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"}}
asked Aug 20, 2019 at 11:04
3
  • Check this url magento.stackexchange.com/questions/94851/… Commented Aug 20, 2019 at 11:08
  • If you ever found see Mage then its M1 code not M2 ;) Commented Aug 20, 2019 at 11:17
  • it is m2 . I write the code . i dont know the m2 code . thats why i write m1 code Commented Aug 20, 2019 at 11:20

4 Answers 4

1

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);
answered Aug 20, 2019 at 11:17
2
  • 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 Commented Aug 20, 2019 at 11:20
  • increase memory limit 1024M in htaccess or php.ini Commented Aug 20, 2019 at 11:23
1

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;
answered Aug 20, 2019 at 11:51
4
  • but where i can write the id Commented Aug 20, 2019 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 Commented Aug 20, 2019 at 11:59
  • i have update my code please check it Commented Aug 20, 2019 at 12:06
  • could you please check this magento.stackexchange.com/questions/329168/… Commented Jan 11, 2021 at 13:07
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.

answered Aug 20, 2019 at 11:29
6
  • Yes this is working . But i want the entire product block , not the name only Commented Aug 20, 2019 at 11:31
  • You can replace echo $product->getName(); to echo $product->getData(); and check please. Commented Aug 20, 2019 at 11:38
  • We're sorry, an error has occurred while generating this content. Commented Aug 20, 2019 at 11:39
  • Can you describe what you need exactly?. Commented Aug 20, 2019 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 ? Commented Aug 20, 2019 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());
?>
answered Aug 20, 2019 at 11:21
3
  • 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"}} Commented Aug 20, 2019 at 11:28
  • i want the entire product block , not the name only Commented Aug 20, 2019 at 11:32
  • John, Please check my updated code. Commented Aug 20, 2019 at 12:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.