0

I want to create a phtml file and create a dynamic block with that one to put on CMS pages - Take it as product information blocks where i want to display the product name, the price, the URL - nothing more.

Is it possible to make that with something like the standard magento 2 block variable and link that to a phtml file?

I got something like this in mind:

Magento 2 Block

{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/product-info.phtml" product-id="30"}}

And then get the information within the phtml file via the used product-id on the block variable but I also got no clue how to approach that.

1

you can do by the following code

{{block class="Magento\Framework\View\Element\Template" name="blockname" template="Vendor_Module::test.phtml" product_id="30"}}

in your phtml file

Now you can get the data by calling:

$myVar = $block->getData('product_id'); or $this->getData('product_id');

OR

$myVar = $block->getProductId();

  • Works! Thank you :) Do you also know how i can get the "incl. xx% VAT" text via this method ? like $product->getProductTax(); ? – Marcel H. Oct 18 '18 at 8:51
  • I think you can't get product tax directly in phtml file from cms block/page, but you can get it via phtml file to call function in block check this link – Hitesh Oct 18 '18 at 9:18

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.