wordpress - 在插件类中设置时撤消 add_action
问题描述
我已经继承了以下设置,并带有一个 PHP 问题,我希望能有这样的想法:我们使用 ivole 的 Customer Reviews for WooCommerce 插件和 Automattic 的 WooCommerce Bookings。
WooCommerce 的 Customer Revierws 包含以下类构造函数,它设置一个操作以在订单完成时向客户发送电子邮件(即在这种情况下已付款):
class Ivole_Sender {
public function __construct() {
:
add_action( 'woocommerce_order_status_completed_notification', array( $this, 'sender_trigger' ) );
:
}
:
}
问题是,因为在预订附加日期过去之前预订才完成,它实际上需要附加到不同的操作:
add_action( 'woocommerce_booking_complete', array( $this, 'sender_trigger' ) );
通常,remove_action
在 function.php (或类似的)中使用,然后重新添加操作就可以了,但在这种情况下,当add_action
发生在类中时,我如何remove_action
不物理地修改插件中的类?
插件加载顺序如下所示:
ivole.php(插件加载器):
require_once( 'class-ivole.php' );
类-ivole.php:
require_once('class-ivole-sender.php');
类-ivole-sender.php:
class Ivole_Sender {
public function __construct() {
:
add_action( 'woocommerce_order_status_completed_notification', array( $this, 'sender_trigger' ) );
:
}
:
}
解决方案
您需要像这样找到类的实例化位置:
$ivole_sender = new Ivole_Sender();
因此,您可以在删除操作中使用此变量。
global $ivole_sender;
remove_action( 'woocommerce_order_status_completed_notification', array( $ivole_sender, 'sender_trigger' ), 10 );
推荐阅读
- javascript - 如何使用键获取 javascript 对象的值
- xcode - macOS 单元测试中的 NSAllowsArbitraryLoads
- kubernetes - 如何跳过 Argo 工作流程的步骤
- java - 用实际值替换字符串中的变量 - Groovy
- javascript - 如何正确拆分 url 然后在 javascript 中获取剩余部分?
- sql - 来自多个表的聚合不计算在内
- python - 如何在 python 中创建列表?
- reactjs - 如何对齐表格分页内部组件(标题、操作)
- android - 在 recyclerview 中使用 ItemTouchHelper 控制滑动项目速度
- javascript - 我的 JavaScript 按钮只有在我不先编辑字段内容时才会运行