← All Integrations

Magento 2 Integration

Add prescription verification with layout XML and product attributes

15-minute setupMagento 2.3+Theme or Module

How It Works

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.

Prerequisites

  • 1. Magento 2.3+ (Open Source or Commerce)
  • 2. Access to theme files or ability to create modules
  • 3. An RxCompliant API key (sign up free)

1Create the product attribute

In Magento Admin, go to Stores > Attributes > Product > Add New Attribute:

  • Default Label: Requires Prescription
  • Catalog Input Type: Yes/No
  • Attribute Code: requires_prescription
  • Add to the Default attribute set
Admin Panel
-- 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 set

2Add layout XML to load the script and template

Create or edit the product view layout file in your theme:

catalog_product_view.xml
<!-- 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>

3Create the PHTML template

Create the template file that conditionally renders the prescription gate:

rxcompliant-gate.phtml
<?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; ?>

4Set products as requiring prescription

Go to Catalog > Products > Edit. Find the "Requires Prescription" attribute and set it to "Yes" for each product that needs a prescription.

5Deploy and clear cache

Terminal
# Deploy static content and clear cache
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

Alternative: Custom Module Approach

For a cleaner separation, create a standalone Magento module:

module.xml
<!-- 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>
di.xml
<!-- 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>

Ready to get started?

Sign up for free and add prescription verification to your Magento store.