Changing Order Status for Credit Card Payments in Magento
When using Magento’s built-in payment system and processing orders via credit card, you might notice that completed transactions are marked as ‘pending’. This status doesn’t immediately reflect the final payment status. To provide more clarity to your admin team, let’s explore how to change this default behavior.
Understanding the Default Magento Behavior
By default, when a customer completes an order using a credit card payment method like Authorize.net or PayPal, Magento sets the order status to ‘pending’. This is because the platform doesn’t immediately capture the payment; it merely authorizes it. The actual transaction happens when you process and capture the payment manually in the admin panel.
Creating a New Payment Status
To make the order status more descriptive, we can create a new status specifically for credit card payments that have been processed successfully. Here’s how to do this:
- Log in to your Magento admin panel.
- Navigate to Stores > Settings > Configuration > Sales > Sales > Order Status.
- In the ‘Order Statuses’ section, click on Add New Status.
- Enter a name for your new status, such as “Credit Card Payment Processed” or simply “Paid by Credit Card”.
- Set the status label and label in store views accordingly.
- Click on Save Order Status.
Assigning the New Status to Credit Card Orders
Now, we want Magento to automatically assign this new status to orders that were paid using a credit card payment method. For this, you’ll need to override the core order model’s setState function.
Create an observer for the ‘sales_order_place_after’ event in your custom module:
public function setCreditCardPaymentStatus($observer)
{
$event = $observer->getEvent();
$order = $event->getOrder();
if ($order->getPayment()->getMethodInstance()->getCode() == 'authorizenet_directpost_cc' ||
$order->getPayment()->getMethodInstance()->getCode() == 'paypal_express_checkout'
) {
$order->setState(self::STATE_PROCESSING, true, null);
}
}
In this code:
- We check if the payment method is either Authorize.net or PayPal.
- If it is, we set the order state to
processingand mark it as notifiable.
Remember to register your observer in the etc/config.xml file of your module:
<global>
<events>
<sales_order_place_after>
<observers>
<your_module_set_credit_card_payment_status>
<type>model</type>
<class>YourModule/Observer</class>
<method>setCreditCardPaymentStatus</method>
</your_module_set_credit_card_payment_status>
</observers>
</sales_order_place_after>
</events>
</global>
Best Practices and Further Reading
For a more robust solution, consider implementing a custom payment method module that handles the payment processing internally. This allows for better control over the order status flow.
Check out this detailed guide from Laravel Company on creating custom payment methods in Magento 2 for more advanced scenarios.
Conclusion
By following these steps, you can now create a new ‘Payment through Credit Card’ status and assign it to orders processed using a credit card. This provides clarity to your admin team about the order’s final payment status.
Remember that any code modifications require proper testing before deployment in a production environment. If you need assistance with custom development or have other Magento-related inquiries, feel free to reach out to us at Laravel Company.