Magento1.9.3.6:如何移除后台销售订单的Authorize.Net支付评论
嘿,我来帮你搞定这个问题——要阻止Authorize.Net自动把支付成功/失败这类评论存到Magento订单里,有几个稳妥的方案,分Magento 1和Magento 2两种场景给你细说:
Magento 2 解决方案
方法1:使用插件(Plugin)拦截评论添加
这是Magento 2推荐的扩展方式,不会修改核心代码,升级也安全。
- 先创建一个自定义模块(比如命名为
Vendor_PaymentCommentBlocker),如果没有现成的自定义模块的话。 - 在模块的
etc/di.xml文件中配置插件:
<type name="Magento\Sales\Model\Order"> <plugin name="vendor_order_block_authorizenet_comments" type="Vendor\PaymentCommentBlocker\Plugin\OrderCommentBlocker" sortOrder="10" /> </type>
- 创建插件类
Vendor/PaymentCommentBlocker/Plugin/OrderCommentBlocker.php:
<?php namespace Vendor\PaymentCommentBlocker\Plugin; use Magento\Sales\Model\Order; class OrderCommentBlocker { public function beforeAddStatusHistoryComment(Order $subject, $comment, $status = false, $isCustomerNotified = null) { // 这里的关键词要根据你后台实际出现的评论内容调整 $blockedKeywords = ['Authorize.Net', 'Transaction ID', 'Payment approved', 'Payment declined']; foreach ($blockedKeywords as $keyword) { if (strpos($comment, $keyword) !== false) { // 返回空评论,阻止原方法添加这条记录 return [null, $status, $isCustomerNotified]; } } // 非目标评论,正常执行原方法 return [$comment, $status, $isCustomerNotified]; } }
- 启用模块并清理缓存:
bin/magento module:enable Vendor_PaymentCommentBlocker bin/magento setup:upgrade bin/magento cache:clean
方法2:通过事件观察者阻止评论保存
另一种方式是监听评论保存前的事件,直接清空目标评论内容:
- 在模块的
etc/events.xml中配置观察者:
<event name="sales_order_status_history_save_before"> <observer name="vendor_block_authorizenet_comments" instance="Vendor\PaymentCommentBlocker\Observer\BlockAuthorizenetComments" /> </event>
- 创建观察者类
Vendor/PaymentCommentBlocker/Observer/BlockAuthorizenetComments.php:
<?php namespace Vendor\PaymentCommentBlocker\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class BlockAuthorizenetComments implements ObserverInterface { public function execute(Observer $observer) { $history = $observer->getEvent()->getData('status_history'); $comment = $history->getComment(); // 匹配Authorize.Net相关评论关键词 $blockedKeywords = ['Authorize.Net', 'Transaction ID', 'Approved', 'Declined']; foreach ($blockedKeywords as $keyword) { if (strpos($comment, $keyword) !== false) { $history->setComment(null); $history->setIsCustomerNotified(false); // 同时取消给客户发通知(如果有的话) break; } } return $this; } }
- 同样启用模块并清理缓存即可。
Magento 1 解决方案
如果还在使用Magento 1,同样可以通过事件观察者实现:
- 在自定义模块的
config.xml中添加事件配置:
<global> <events> <sales_order_status_history_save_before> <observers> <vendor_block_authorizenet_comments> <class>Vendor_PaymentCommentBlocker_Model_Observer</class> <method>blockAuthorizenetComments</method> </vendor_block_authorizenet_comments> </observers> </sales_order_status_history_save_before> </events> </global>
- 创建观察者模型
Vendor/PaymentCommentBlocker/Model/Observer.php:
<?php class Vendor_PaymentCommentBlocker_Model_Observer { public function blockAuthorizenetComments($observer) { $history = $observer->getStatusHistory(); $comment = $history->getComment(); $blockedKeywords = ['Authorize.Net', 'Transaction ID', 'Payment approved', 'Payment declined']; foreach ($blockedKeywords as $keyword) { if (strpos($comment, $keyword) !== false) { $history->setComment(''); break; } } return $this; } }
- 刷新Magento缓存即可生效。
注意事项
- 一定要根据你后台实际出现的Authorize.Net评论内容调整
$blockedKeywords里的关键词,确保能精准匹配到要拦截的评论。 - 绝对不要直接修改Magento核心代码,用自定义模块的方式扩展,避免升级时丢失修改。
- 如果是Magento 2,优先推荐使用插件方法,它比观察者更灵活,也符合Magento的最佳实践。
内容的提问来源于stack exchange,提问作者Rakesh Kumar
相关产品推荐
相关产品推荐

