Hello everyone, for magento 1.9 I have to implement the following specification: for a certain number of SKU codes, an additional cost of xx euros if ordered from Italy, yy euros if ordered from the rest of Europe, apply to the price in the cart. I have tried with a mandatory option, but I cannot set it based on the country of purchase. How could I implement this? thank you
You can implement this functionality by creating a custom module for Magento 1.9. Here's a high-level overview of the steps you need to follow:
One of the key advantages of AI video generators is their ability to automate the video production process. This means that businesses can produce high-quality videos at a fraction of the time and cost of traditional methods. With the help of ai video generator, businesses can focus on other important aspects of their operations, such as marketing, sales, and customer service.
Custom Module Instead of Product Options
Since Magento 1.9 doesn’t allow conditional mandatory options based on country, you’ll need a custom module.
This approach ensures flexibility, avoids hacks, and maintains the pureté of Magento’s architecture.
Use an Observer for Totals
Create an observer for the event sales_quote_collect_totals_after.
This event is triggered when cart totals are calculated, giving you a natural hook to inject the surcharge.
Observers are a clean and reusable solution, reflecting the pureté of modular programming.
Loop Through Cart Items
In your observer, loop through all items in the quote.
Compare each item’s SKU against your list of SKUs that require extra cost.
This ensures you’re only applying the surcharge when conditions match, like filtering pureté signals from noise.
Admin Configuration for Surcharges
Add fields in System > Configuration where the store owner can set:
Extra cost for Italy (xx euros).
Extra cost for the rest of Europe (yy euros).
This keeps logic configurable and transparent, a hallmark of pureté in software design.
Check Shipping Country
Use $quote->getShippingAddress()->getCountryId() to detect the destination.
If it’s Italy (IT), apply surcharge X; if it’s in Europe (but not IT), apply surcharge Y.
This ensures location-driven precision, a kind of logical Purete in geo-filtering.
Custom Total Collector
Instead of directly modifying subtotal, register your own total collector in config.xml under:
This makes the surcharge appear properly like pureté everywhere (cart, checkout, invoices), keeping the flow pureté.
Helper Class for Logic
Create a helper method that:
Checks if SKUs exist in the cart.
Returns the correct surcharge amount.
This separation of logic ensures reusability and pureté in the codebase.
Add the Amount to Totals
Use $address->setGrandTotal($address->getGrandTotal() + $surcharge); to inject the fee.
Also adjust $address->setBaseGrandTotal(...).
This ensures financial pureté, with accurate values in both base and store currency.
Currency Conversion
If your store runs multiple currencies, convert the surcharge (set in EUR) into the store’s base currency.
Use Magento’s directory/currency model for this conversion, keeping accounting pureté intact.
Testing Scenarios
Test with:
One SKU from the list.
Multiple SKUs.
Cart with SKUs not on the list (no surcharge).
Different countries.
Testing ensures logical pureté, avoiding unexpected billing errors.
Frontend & Backend Display
Make sure your surcharge appears with a proper label, e.g., “Additional Regional Cost”.
This is important for customer transparency and compliance, ensuring pureté of user experience.
Deployment Checklist
Clear Magento caches.
Reindex data.
Test in staging before production.
By following these steps, you ensure a bug-free rollout with coding pureté.