Add prescription verification with layout XML and product attributes
Magento 2 integration involves creating a product attribute (requires_prescription), adding layout XML to load the widget script, and a PHTML template to conditionally render the prescription gate. You can do this via theme overrides or a custom module.
In Magento Admin, go to Stores > Attributes > Product > Add New Attribute:
-- Create the product attribute via MySQL or Admin panel
-- Admin: Stores > Attributes > Product > Add New Attribute
-- Attribute Code: requires_prescription
-- Catalog Input Type: Yes/No
-- Add to Default attribute setCreate or edit the product view layout file in your theme:
<!-- app/design/frontend/VENDOR/THEME/Magento_Catalog/layout/catalog_product_view.xml -->
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="https://rxcompliant.com/widget/rxcompliant.js"
src_type="url" />
</head>
<body>
<referenceContainer name="product.info.form.content">
<block class="Magento\Catalog\Block\Product\View"
name="rxcompliant.gate"
template="Magento_Catalog::product/rxcompliant-gate.phtml"
before="product.info.addtocart" />
</referenceContainer>
</body>
</page>Create the template file that conditionally renders the prescription gate:
<?php
/** app/design/frontend/VENDOR/THEME/Magento_Catalog/templates/product/rxcompliant-gate.phtml */
$product = $block->getProduct();
$requiresRx = $product->getData('requires_prescription');
?>
<?php if ($requiresRx): ?>
<div data-requires-rx
data-product-id="<?= $block->escapeHtmlAttr($product->getId()) ?>"
data-product-title="<?= $block->escapeHtmlAttr($product->getName()) ?>"
data-platform="magento">
</div>
<script>
// Pass API key (store in Magento config or hardcode)
document.currentScript.parentElement
.querySelector('[data-requires-rx]')
.closest('form')
?.insertAdjacentHTML('beforebegin', '');
</script>
<?php endif; ?>Go to Catalog > Products > Edit. Find the "Requires Prescription" attribute and set it to "Yes" for each product that needs a prescription.
# Deploy static content and clear cache
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flushFor a cleaner separation, create a standalone Magento module:
<!-- Optional: Create a custom module -->
<!-- app/code/RxCompliant/PrescriptionGate/etc/module.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="RxCompliant_PrescriptionGate" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config><!-- app/code/RxCompliant/PrescriptionGate/etc/frontend/di.xml -->
<!-- Observer to add script tag to head -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Page\Config">
<plugin name="rxcompliant_add_script"
type="RxCompliant\PrescriptionGate\Plugin\AddScript" />
</type>
</config>Sign up for free and add prescription verification to your Magento store.