remove_action 来自 PHP Class

remove_action From PHP Class

我正在尝试删除一个操作并以不同的优先级添加它。 以下是帮助生成消息的所有代码片段:

包括所需的前端文件

private function frontend_includes() {

    require_once( $this->get_plugin_path() . '/includes/wc-memberships-template-functions.php' );
    require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );

    WC_Memberships_Shortcodes::initialize();

    $this->frontend     = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );
    $this->checkout     = $this->load_class( '/includes/frontend/class-wc-memberships-checkout.php', 'WC_Memberships_Checkout' );
    $this->restrictions = $this->load_class( '/includes/frontend/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );
}

获取商品限购信息

 /**
 * @param int $post_id Optional. Defaults to current post.
 * @return string
 */
public function get_product_purchasing_restricted_message( $post_id = null ) {

    if ( ! $post_id ) {

        global $post;
        $post_id = $post->ID;
    }

    $products = $this->get_products_that_grant_access( $post_id );
    $message  = $this->get_restriction_message( 'product_purchasing_restricted', $post_id, $products );

    /**
     * Filter the product purchasing restricted message
     *
     * @since 1.0.0
     * @param string $message The restriction message
     * @param int $product_id ID of the product being restricted
     * @param array $products Array of product IDs that grant access to this product
     */
    return apply_filters( 'wc_memberships_product_purchasing_restricted_message', $message, $post_id, $products );
}

限制class,处理前端的内容限制

class WC_Memberships_Restrictions {


/** @var array associative array of content conditions for current user **/
private $user_content_access_conditions;

/** @var array of post IDs that content restriction has been applied to **/
private $content_restriction_applied = array();

/** @var string Product content restriction password helper **/
private $product_restriction_password = null;

/** @var bool Product thumbnail removed helper **/
private $product_thumbnail_restricted = false;


public function __construct() {
    // Desired action to remove and re-prioritize
    add_action( 'woocommerce_single_product_summary', array( $this, 'single_product_purchasing_restricted_message' ), 30 );
}
}

我真的只需要在 WC_Memberships_Restrictions class 的操作中将优先级从 30 更改为 15。问题是没有明确的方法来调用删除。有什么建议吗?

好吧,您提供的代码显示 WC_Memberships_Restrictions class 的实例存储在主 class' restrictions 属性.

$this->restrictions = $this->load_class( '/includes/frontend/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );

从那里我只需要查找如何访问主要成员资格的实例 class,从您看到的主要插件文件的底部:

/**
 * Returns the One True Instance of Memberships
 *
 * @since 1.0.0
 * @return WC_Memberships
 */
function wc_memberships() {
    return WC_Memberships::instance();
}

这意味着现在要访问限制 class 的实例,我们需要访问主 class 的限制 属性。虽然这听起来很清楚,但基本上它的意思是:

wc_memberships()->restrictions

知道这一点,我们就可以知道从中删除和添加操作 class:

function so_41431558_change_hook_priority(){
    if( function_exists( 'wc_memberships' ) ){
        remove_action( 'woocommerce_single_product_summary', array( wc_memberships()->restrictions, 'single_product_purchasing_restricted_message' ), 30 );
        add_action( 'woocommerce_single_product_summary', array( wc_memberships()->restrictions, 'single_product_purchasing_restricted_message' ), 15 );
    }
}
add_action( 'woocommerce_single_product_summary', 'so_41431558_change_hook_priority', 1 );