0

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.

Mohit Kumar Arora
10.2k7 gold badges29 silver badges57 bronze badges
asked Jun 14, 2018 at 11:21
2
  • Which php code is not working and on which file? Commented 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 fine Commented Jun 14, 2018 at 11:26

5 Answers 5

1

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.

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Jun 14, 2018 at 11:58
2
  • How may i get the data from this block to my static block Commented Jun 14, 2018 at 12:08
  • @CompuScie, sorry I could understand your comment? Commented Jun 14, 2018 at 12:46
2

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"}}
answered Dec 14, 2018 at 10:41
0

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"}}
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Jun 14, 2018 at 12:32
1
  • ohh you can also check the accepted answer above... its also correct Commented Jun 14, 2018 at 12:33
0

Please try the below instead of your code:

<div title="<?= $_product->getAttributeText("manufacturer"); ?>"></div>
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Jun 14, 2018 at 11:28
6
  • 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 browser Commented Jun 14, 2018 at 11:31
  • Check the updated answer please. title is not for div. Commented Jun 14, 2018 at 11:35
  • i want to get the title for the div, Commented Jun 14, 2018 at 11:42
  • Please update the js code on your question. Commented Jun 14, 2018 at 11:45
  • 1
    That 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 phtml extension. Commented Jun 14, 2018 at 11:46
0

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>
answered Dec 14, 2018 at 10:44

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.