PrestaShop allows developers to override core functionality without modifying the original files. This ensures that updates do not erase your customizations. In this guide, we will explain the right way to override PrestaShop core functionality safely and effectively.
Why Override Core Functionality?
Overriding is useful when you need to:
- Modify default behaviors without changing core files.
- Extend or customize existing functionalities.
- Ensure changes are update-safe and maintainable.
Understanding PrestaShop’s Override System
PrestaShop uses an override mechanism that lets you replace core class methods without altering the original files. Overrides are stored in the /override directory.
Step 1: Locating the Core File to Override
Identify the core file you want to modify. PrestaShop’s core classes are found in:
- Controllers:
/controllers/front/or/controllers/admin/ - Core Classes:
/classes/ - Module Overrides:
/modules/
For example, to override Cart.php, locate it in classes/Cart.php.
Step 2: Creating the Override File
PrestaShop expects override files in override/ with the same namespace structure as the core files.
Example: Overriding Cart.php
To override the getOrderTotal() method in Cart.php, create a new file:
Path: /override/classes/Cart.php
<?php
class Cart extends CartCore
{
public function getOrderTotal($withTaxes = true, $type = Cart::BOTH)
{
// Custom logic before calling the parent method
$total = parent::getOrderTotal($withTaxes, $type);
// Example modification: Apply an additional discount
if ($total > 100) {
$total -= 5; // Apply a discount of $5 for orders above $100
}
return $total;
}
}Step 3: Clearing Cache and Validating the Override
After adding the override, you must clear PrestaShop’s cache:
- Navigate to Advanced Parameters > Performance.
- Click Clear Cache.
- Disable and re-enable cache if necessary.
You can also manually delete cached files from /var/cache/prod/.
Step 4: Testing the Override
Perform tests to ensure your override works:
- Add products to the cart and verify the order total.
- Check for unexpected errors in logs (
/var/logs/).
Best Practices for Overriding in PrestaShop
- Avoid unnecessary overrides – use module hooks if possible.
- Document your changes to keep track of modifications.
- Ensure compatibility with future PrestaShop updates.
- Test overrides on a staging environment before deploying to live sites.
Conclusion
Overriding PrestaShop core functionality allows you to modify behaviors safely without altering core files. By following these steps, you ensure your changes are update-safe and maintainable. Always prefer hooks when possible and keep your overrides well-documented for future reference.
Now, try applying an override in your PrestaShop store and enhance its functionality the right way!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!



Leave a Reply