Magento Have terms and conditions if customer buys certain products

Magento Have terms and conditions if customer buys certain products

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Have terms and conditions if customer buys certain products

Displaying Terms and Conditions for Specific Magento Products

In many e-commerce scenarios, it is necessary to display terms and conditions (T&C) only when certain products are added to the customer’s cart. This is particularly common in industries that require a prescription or have specific legal requirements associated with their products.

Understanding the Requirement

The client wants to display the T&C file (agreements.phtml) if the product has an attribute prescription_required set to ‘yes’. The T&C should only appear when these specific items are in the shopping cart.

Proposed Solution Approach

To achieve this, we’ll follow a two-step approach:

  1. Identify Prescription-Required Products
  2. Display Terms and Conditions

Step 1: Identify Prescription-Required Products

First, we need to identify which products require a prescription. You’ve already created the prescription_required attribute in the Magento backend.

Now, let’s get the shopping cart items and filter them based on this attribute:

<?php
$cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();

foreach ($cartItems as $item) {
    if ($item->getData('prescription_required') == 1) { // Assuming 'yes' is represented by 1
        $prescriptionRequiredFlag = true;
        break; // Stop the loop once we find a prescription-required item
    }
}
?>

In this code, we retrieve all visible items from the shopping cart and loop through them. If any item has prescription_required set to 1 (or ‘yes’), we set $prescriptionRequiredFlag to true and break the loop.

Step 2: Display Terms and Conditions

If a prescription-required product is found, proceed with displaying the terms and conditions:

<?php if isset($prescriptionRequiredFlag) && $prescriptionRequiredFlag === true): ?>
    <?php echo Mage::getSingleton('core/translate')->htmlEscape($this->getLayout()->createBlock('cms/block')
        ->setBlockId('agreements')
        ->toHtml()); ?>
<?php endif; ?>

Here, we check if the $prescriptionRequiredFlag is set and equals true. If it does, we use Magento’s CMS block rendering functionality to fetch the content of the ‘agreements’ block (which presumably contains your T&C) and display it.

Best Practices

  1. Use Block IDs Carefully: In this example, we’re assuming that the T&C are stored in a CMS block with ID agreements. Ensure you use unique and meaningful block IDs to avoid conflicts.

  2. Attribute Values: If your backend allows multiple values for attributes (e.g., ‘yes’, ‘no’), ensure you handle these variations appropriately in your code.

  3. Performance Optimization: If your cart contains many items, consider optimizing the loop by using Magento’s built-in functions or database queries to directly filter prescription-required products.

At LaravelCompany, we often work with complex e-commerce requirements like this one. Our expert team can help you implement custom solutions that meet your business needs while ensuring optimal performance and security.

Conclusion

Displaying terms and conditions based on the contents of a shopping cart requires careful management of product attributes and conditional rendering in Magento’s template files.

By following the approach outlined above, you should be able to display the required T&C file only when specific prescription-required products are present in the customer’s cart. If you encounter any issues or need further assistance, feel free to reach out to our team at LaravelCompany.

Checkout Catalog Themes Admin

Our network