Magento: Syncing Custom Credit Card Payment Attributes to Both sales_flat_order_payment and sales_flat_quote_payment Tables
Introduction
In Magento, by default, the normal credit card information saves to two different tables when an order is placed:
sales_flat_quote_payment- This table stores payment data associated with a quote (shopping cart).sales_flat_order_payment- This table contains payment details related to actual orders.
If you’ve created a custom credit card attribute and want it to be saved in both tables, this guide will walk you through the process step-by-step.
The Problem: Data Only Saves to sales_flat_quote_payment
When a customer places an order with your custom credit card attribute set, the data only gets saved into the sales_flat_quote_payment table. This is because Magento doesn’t automatically sync this information to the sales_flat_order_payment table during the order creation process.
The Solution: Extending the Sales Model
To make sure your custom payment data ends up in both tables, you need to extend the core Magento sales model and override the save() method. This will ensure that when an order is created or updated, your custom credit card attribute gets saved into the correct tables.
Here’s a high-level overview of what we’ll do:
- Create a new module.
- Create a new sales payment model extending
\Magento\Sales\Model\Order\Payment. - Override the
save()method to save additional data to both quote and order payment tables.
Step-by-Step Guide
Let’s dive into the implementation details:
Step 1: Create a New Module
First, create a new module folder following Magento’s structure:
/app/code/Vendor/YourModule/
Inside your module, create a registration.php file to register your module with Magento:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_YourModule',
__DIR__
);
Step 2: Create the New Payment Model
Create a new payment model extending \Magento\Sales\Model\Order\Payment:
namespace Vendor\YourModule\Model;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order\Payment;
class YourCustomPayment extends Payment
{
// Add any necessary constructor code here
}
Step 3: Override the save() Method
Open your new payment model and override the save() method:
public function save()
{
$this->beforeSave();
if ($this->getId()) {
$this->isObjectNew(false);
}
try {
$this->_getResource()->save($this);
// Add this line to save data to sales_flat_order_payment
$this->_getResource()->copyToOrderPayment($this);
$this->afterSave();
return $this;
} catch (LocalizedException $e) {
throw $e;
} catch (\Exception $e) {
$this->_getResource()->rollBack();
throw new LocalizedException(__('Cannot save the payment data.'));
}
}
The copyToOrderPayment() method is a custom method you’ll need to implement in your model’s resource:
namespace Vendor\YourModule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class YourCustomPayment extends AbstractDb
{
protected function _construct()
{
$this->_init('sales/flat_quote_payment', 'entity_id');
}
public function copyToOrderPayment($payment)
{
if ($payment->getId()) {
// Here, you can use the $payment object to fetch your custom credit card attribute value
// Then save it into sales_flat_order_payment using Magento's resource model methods
// Example: $customValue = $payment->getAdditionalInformation('your_custom_attribute_code');
}
}
}
Conclusion
Extending the core Magento sales payment model and overriding the save() method is a powerful technique to ensure your custom credit card attributes get saved into both sales_flat_order_payment and sales_flat_quote_payment tables.
This solution provides a clean, robust way to modify Magento’s core behavior without directly modifying its codebase, which is a best practice for maintaining upgrade compatibility.
To learn more about Magento development best practices and how to build scalable e-commerce solutions, visit https://laravelcompany.com.