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"}}
-
Check this url magento.stackexchange.com/questions/94851/…Tsita– Tsita2019年08月20日 11:08:02 +00:00Commented Aug 20, 2019 at 11:08
-
If you ever found see Mage then its M1 code not M2 ;)anonymous– anonymous2019年08月20日 11:17:24 +00:00Commented Aug 20, 2019 at 11:17
-
it is m2 . I write the code . i dont know the m2 code . thats why i write m1 codeJohn– John2019年08月20日 11:20:52 +00:00Commented Aug 20, 2019 at 11:20
4 Answers 4
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.phtmlJohn– John2019年08月20日 11:20:10 +00:00Commented Aug 20, 2019 at 11:20
-
increase memory limit 1024M in htaccess or php.iniAnas Mansuri– Anas Mansuri2019年08月20日 11:23:01 +00:00Commented Aug 20, 2019 at 11:23
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 idJohn– John2019年08月20日 11:53:21 +00:00Commented 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 attibuteDevidas– Devidas2019年08月20日 11:59:54 +00:00Commented Aug 20, 2019 at 11:59
-
i have update my code please check itDevidas– Devidas2019年08月20日 12:06:38 +00:00Commented Aug 20, 2019 at 12:06
-
could you please check this magento.stackexchange.com/questions/329168/…John– John2021年01月11日 13:07:30 +00:00Commented Jan 11, 2021 at 13:07
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 onlyJohn– John2019年08月20日 11:31:26 +00:00Commented Aug 20, 2019 at 11:31
-
You can replace
echo $product->getName();toecho $product->getData();and check please.Monark Bhawani– Monark Bhawani2019年08月20日 11:38:18 +00:00Commented Aug 20, 2019 at 11:38 -
We're sorry, an error has occurred while generating this content.John– John2019年08月20日 11:39:50 +00:00Commented Aug 20, 2019 at 11:39
-
Can you describe what you need exactly?.Monark Bhawani– Monark Bhawani2019年08月20日 11:42:57 +00:00Commented 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 ?John– John2019年08月20日 11:47:20 +00:00Commented Aug 20, 2019 at 11:47
<?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– John2019年08月20日 11:28:45 +00:00Commented Aug 20, 2019 at 11:28
-
i want the entire product block , not the name onlyJohn– John2019年08月20日 11:32:19 +00:00Commented Aug 20, 2019 at 11:32
-
John, Please check my updated code.Flying Finner– Flying Finner2019年08月20日 12:04:52 +00:00Commented Aug 20, 2019 at 12:04
Explore related questions
See similar questions with these tags.