Is there any way to add a simple php code:
<div title="<?php echo $_product->getAttributeText("manufacturer"); ?>"></div>
to a block we create under content page. It can't execute PHP code as far as I'm concerned.
-
Which php code is not working and on which file?Sukumar Gorai– Sukumar Gorai2018年06月14日 11:24:49 +00:00Commented Jun 14, 2018 at 11:24
-
the one above is giving me errors of unexpected token <, but when i directly write it on phtml file it works fineCompu Scie– Compu Scie2018年06月14日 11:26:54 +00:00Commented Jun 14, 2018 at 11:26
5 Answers 5
I assume that you want to call PHP code in a static block.
You can do so by calling a phtml file in the static block and write your PHP code in the phtml file.
To call phtml file in static block, you need to write code like this:
{{block class="Path\To\Block\Class" template="Namespace_Module::filename.phtml"}}
Since you are trying to get product manufacturer, I think you need to use block class name as Magento\Catalog\Block\Product.
-
How may i get the data from this block to my static blockCompu Scie– Compu Scie2018年06月14日 12:08:13 +00:00Commented Jun 14, 2018 at 12:08
-
@CompuScie, sorry I could understand your comment?Mohit Kumar Arora– Mohit Kumar Arora2018年06月14日 12:46:00 +00:00Commented Jun 14, 2018 at 12:46
You cannot add PHP code into static blocks
but you can add a link to your phtml file into static block
- Create your sample.phtml file
- write your code in sample.phtml
Then add this file into the static file as
{{block type="core/template" template="catalog/category/sample.phtml"}}
The following syntax will help you:
Basically, you need to call .phtml file to execute your php file or php code;
{{block class="path-to-block-class" template="module-name::your-filename.phtml"}}
-
ohh you can also check the accepted answer above... its also correct1990rk4– 1990rk42018年06月14日 12:33:40 +00:00Commented Jun 14, 2018 at 12:33
Please try the below instead of your code:
<div title="<?= $_product->getAttributeText("manufacturer"); ?>"></div>
-
Uncaught TypeError: Cannot read property 'title' of null, when i try to get the value by JS, also now it just puts php code to the browserCompu Scie– Compu Scie2018年06月14日 11:31:29 +00:00Commented Jun 14, 2018 at 11:31
-
Check the updated answer please. title is not for div.Sukumar Gorai– Sukumar Gorai2018年06月14日 11:35:36 +00:00Commented Jun 14, 2018 at 11:35
-
i want to get the title for the div,Compu Scie– Compu Scie2018年06月14日 11:42:37 +00:00Commented Jun 14, 2018 at 11:42
-
Please update the js code on your question.Sukumar Gorai– Sukumar Gorai2018年06月14日 11:45:57 +00:00Commented Jun 14, 2018 at 11:45
-
1That code will only work if it is added to a phtml file. Verify that the file you are adding that code snippet to actually has a
phtmlextension.ProcessEight– ProcessEight2018年06月14日 11:46:52 +00:00Commented Jun 14, 2018 at 11:46
Your above code has small mistake, which is you are including double quote inside another double quote. Replace your code with the below code:
<div title="<?php echo $_product->getAttributeText('manufacturer'); ?>"></div>