Customizing Price Display in Magento: Admin Control for Special/Regular Prices
As an e-commerce developer, you might find yourself in situations where you need to give your Magento admin the power to control whether regular or special prices are displayed on the frontend. In this post, I’ll guide you through creating a custom module that allows admin users to toggle between showing and hiding these price values.
The Problem: Managing Price Visibility
By default, Magento displays both the regular and special prices on product pages when there’s an active sale price. However, sometimes you may want your admin to have more control over this behavior.
For instance, you might want to:
- Hide the regular price entirely during promotional periods.
- Show only the discounted price for certain customer groups.
- Display a strikethrough effect for the original price instead of showing both values side by side.
The Solution: A Custom Admin Module
To achieve this level of control, we’ll need to create our own module that extends Magento’s core functionality. This approach allows us to keep changes separate from the base code, ensuring easy updates and better maintainability.
Here’s a high-level overview of the steps involved:
-
Create the module structure: Follow the standard Magento module creation process by creating directories under
app/code/VendorName/ModuleName(replace “VendorName” with your actual vendor name or namespace). -
Add configuration options: In the
etc/adminhtml/system.xmlfile, define new configuration fields for enabling/disabling regular price display and any additional settings you need.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="pricedisplay" show_in_default="1" show_in_website="1" translate="label, comments">
<label>Price Display Settings</label>
<tab>General</tab>
<sort_order>2000</sort_order>
<resource>VendorName_ModuleName::price_display_settings</resource>
<group id="pricedisplay_general">
<field id="enable_regular_price" sort_order="10" show_in_default="1" show_in_website="1">
<label>Enable Regular Price Display</label>
<frontend_type>select</frontend_type>
<source_model>\Magento\Config\Model\Config\Source\Yesno::optionsArray</source_model>
</field>
</group>
</section>
</system>
</config>
-
Override the price block: Create a new phtml file in your module’s template directory (e.g.,
app/code/VendorName/ModuleName/view/frontend/templates/catalog/product/price.phtml) and copy the relevant code from Magento’s core price.phtml to serve as a starting point. -
Fetch admin configuration values: In your override price.phtml, use Magento’s config helper (
Magento\Framework\App\Config\ScopeConfigInterface) to fetch the admin settings defined previously:
$regularPriceEnabled = $block->getConfig()->getValue('VendorName_ModuleName/pricedisplay_general/enable_regular_price', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
-
Conditionally display prices: Based on the
$regularPriceEnabledvalue, conditionally render or hide the regular price in your phtml file. -
Clear cache and refresh: After implementing these changes, don’t forget to clear Magento’s cache and flush the configuration for the updates to take effect.
Additional Best Practices
- Always follow the principle of least privilege when creating admin configurations.
- Use meaningful resource identifiers and sort orders for better organization in the admin panel.
- Document your customizations clearly in your module’s
README.mdfile. - Test your changes thoroughly across different scenarios, especially with regard to caching and performance.
Conclusion
Creating a custom Magento module to manage price visibility is an effective way to extend the platform’s built-in functionality without modifying core files. By following this approach, you can provide admin users with the flexibility they need while keeping your store’s codebase clean and maintainable.
Remember that proper documentation, unit testing, and adherence to Magento coding standards will help ensure a successful implementation. If you’re looking for more resources on Magento development, be sure to check out our blog at https://laravelcompany.com.