1

We are running into an issue where we need to run a little php script within a .html file in Magento 2, namely;

/app/design/frontend/CleverSoft/core/Magento_Checkout/web/template/minicart/content.html

When we have tried adding the ability for html files to run php via the .htaccess, it ends up causing other errors within Magento so it just ends up breaking more things. Namely, using things like;

AddType application/x-httpd-php56 .php .htm .html
or
AddType application/x-httpd-php .html .htm

Are there other alternatives to try to run php within this html file, or is it possible to change this file to .phtml like so many of the others? I would just have to find where it is originally called I assume and change that from .html to .phtml?

Thanks for the insight!

asked Oct 15, 2020 at 16:09

1 Answer 1

1
+50

You're asking how to run PHP inside a client-side rendered file which is not feasible. I believe you would need to build this yourself from scratch.

The template is set in vendor/magento/module-checkout/view/frontend/layout/default.xml

<item name="template" xsi:type="string">Magento_Checkout/minicart/content</item>

As you can see there is no file suffix, but even if there was changing this would not help you as UI Component templates are rendered client side so you will not be able to run PHP.

More importantly, why do you need to run PHP in here? It would be much easier to achieve what you need with JS.

answered Oct 19, 2020 at 14:39
6
  • We are trying to have a php call to get the active customers email to which we append to the URL of the link within the content.html, something to where the ending URL would be created as example.com/checkout/<?php $email; ?> .. Have not thought about trying to append this with JS, think that would be a valid option? Possibly have it time out for 1 second after page loads and append it via JS in an active phtml file? Commented Oct 19, 2020 at 16:09
  • Within the JS you could check if the user is logged in and if so send an Ajax request to a PHP controller which gives you their email address, then update a Knockout observable which will render to the template only when the value gets populated. magento.stackexchange.com/questions/263905/… has two solutions that may help. Commented Oct 19, 2020 at 16:23
  • Or if you Google 'Magento 2 get customer email address javascript' there are some helpful answers, just be careful not to rely on answers that are in the checkout as the customerData seems to work differently there. Commented Oct 19, 2020 at 16:24
  • Thanks for this Ben! I tried something simple like; document.getElementById("top-cart-btn-checkout").href="test.html"; however it doesn't appear to actually update the given Id, could it be due to being inside an element that has functions going on within it? The link being; <a href="link-to-change.html" id="top-cart-btn-checkout" class="action primary checkout" data-bind="attr: {title: $t('Checkout')}" title="Checkout">Link</a> Commented Oct 19, 2020 at 16:38
  • Ended up just using jquery to swap out the URLs and it works fine, thanks for the good explanation and suggestion! Commented Oct 19, 2020 at 20:41

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.