首页 > 解决方案 > 在插件类中设置时撤消 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' ) );
        :
    }
    :
 }  

标签: wordpress

解决方案


您需要像这样找到类的实例化位置:

$ivole_sender = new Ivole_Sender();

因此,您可以在删除操作中使用此变量。

global $ivole_sender;
remove_action( 'woocommerce_order_status_completed_notification', array( $ivole_sender, 'sender_trigger' ), 10 );

推荐阅读