Preventing Magento from Saving Pickup Point Address During Checkout
As an e-commerce developer working on a project for a shipping carrier that uses pickup points, you’re facing a unique challenge: preventing the system from saving the pickup point’s address as the customer’s shipping information during checkout.
Understanding the Problem
Currently, when customers choose to register and go through the checkout process, Magento saves both their billing and shipping addresses in the customer database. However, if they select a pickup point (which is essentially an alternative shipping location), you want to ensure that only the pickup point address is saved with the order details, not as part of the customer’s permanent shipping information.
The primary issue lies in distinguishing between a regular checkout where customers save their addresses and one involving a pickup point. You’ve already attempted several approaches:
-
Using an Observer on “customer_address_save_before”: This event allows you to prevent the address from being saved, but detecting whether it was triggered during the checkout process proves challenging.
-
Overriding the pickup point saving method: Setting
save_in_address_bookto false seems ineffective because the system is simulating a new registration during checkout. -
Simulating a guest checkout: While this could potentially solve the issue, implementing such a simulation remains unclear.
The Proposed Solution
To address this, we suggest a two-step approach:
Step 1: Detecting Pickup Point Selection
Create an observer for the event checkout_controller_onepage_save_shipping_method. In this observer, check whether the selected shipping method corresponds to a pickup point. If it does, set a flag or use Magento’s shared data storage (e.g., session) to indicate that a pickup point is being used.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$method = $observer->getMethodInstance();
if ($method->getCarrierCode() === 'pickup_point') {
Maggie::getSession()->setPickupPointUsed(true);
}
}
Step 2: Preventing Address Saving
Next, use the customer_address_save_before event. Here, check whether the pickup point flag is set. If it is, and if Magento is attempting to save an address with a specific type (which you should define as your pickup point type), prevent the saving process.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$address = $observer->getAddress();
$typeId = $address->getTypeId();
if (Maggie::getSession()->getPickupPointUsed() && $typeId === 'pickup_point') {
// Prevent the address from being saved in the customer's address book
Maggie::getSession()->unsPickupPointUsed();
return false;
}
}
Implementing the Solution
To implement this approach:
-
Create the observers: Use Magento’s event observer implementation to create these two observers.
-
Configure the events: Configure your
app/etc/config.phpfile or use a module configuration XML file to map these observers to their respective events. -
Test thoroughly: Ensure that you test this solution across different scenarios, including registered customers and guest checkouts involving pickup points.
Conclusion
Preventing Magento from saving pickup point addresses as permanent shipping information requires careful event observation and conditional logic. By detecting the selection of a pickup point during checkout and preventing its address from being saved when it doesn’t match certain criteria, you can ensure that only the necessary order-related address data is stored in your database.
Remember to thoroughly test this solution to ensure it handles edge cases correctly and maintains Magento’s standard functionality for regular shipping addresses. If you’re looking for further assistance or have questions about other Magento development challenges, feel free to visit Laravel Company for comprehensive support.
Happy coding!