无法实例化如何删除钩钩的非静态方法和类别



我正在使用WooCommerce产品供应商插件,在单个产品页面中,出售。我想用;

代替它

添加了

class WC_Product_Vendors_Vendor_Frontend {
    public static function init() {
        $self = new self();
        add_action( 'woocommerce_single_product_summary', array( $self, 'add_sold_by_single' ), 39 );
   return true;
   }
   public function add_sold_by_cart( $values, $cart_item ) {
        $sold_by = get_option( 'wcpv_vendor_settings_display_show_by', 'yes' );
        if ( 'yes' === $sold_by ) {
            $sold_by = WC_Product_Vendors_Utils::get_sold_by_link( $cart_item['data']->id );
            $values[] = array(
                'name' => apply_filters( 'wcpv_sold_by_text', esc_html__( 'Sold By', 'woocommerce-product-vendors' ) ),
                'display' => '<em class="wcpv-sold-by-cart"><a href="' . esc_url( $sold_by['link'] ) . '" title="' . esc_attr( $sold_by['name'] ) . '">' . $sold_by['name'] . '</a></em>',
            );
        }
        return $values;
    }
}
WC_Product_Vendors_Vendor_Frontend::init();

我试图像

那样解开它

1)

add_action( 'wp_head', 'my_remove_actions', 39 );
function my_remove_actions(){
    remove_action( 'woocommerce_single_product_summary', array( 'WC_Product_Vendors_Vendor_Frontend', 'add_sold_by_single' ), 39 );
}

2)

 add_action( 'wp_head', 'my_remove_actions', 39 );
function my_remove_actions(){
    global $wc_product_vendors_vendor_frontend;
    $wc_product_vendors_vendor_frontend = new WC_Product_Vendors_Vendor_Frontend();
    remove_action( 'woocommerce_single_product_summary', array( $wc_product_vendors_vendor_frontend, 'add_sold_by_single' ), 39 );
}

3)

class XWC_Product_Vendors_Vendor_Frontend extends WC_Product_Vendors_Vendor_Frontend{
    public function __construct(){}
}
add_action( 'wp_head', 'my_remove_actions', 39 );
function my_remove_actions(){
    global $xwc_product_vendors_vendor_frontend;
    $xwc_product_vendors_vendor_frontend = new XWC_Product_Vendors_Vendor_Frontend();
    remove_action( 'woocommerce_single_product_summary', array( $xwc_product_vendors_vendor_frontend, 'add_sold_by_single' ), 39 );
}

4)

class XWC_Product_Vendors_Vendor_Frontend extends WC_Product_Vendors_Vendor_Frontend{
        public function __construct() {
    remove_action( 'woocommerce_single_product_summary', array( $this, 'add_sold_by_single' ), 39 );
    }
}
new XWC_Product_Vendors_Vendor_Frontend();

5)

class XWC_Product_Vendors_Vendor_Frontend extends WC_Product_Vendors_Vendor_Frontend{
            public static function init() {
            $self = new self();
        remove_action( 'woocommerce_single_product_summary', array( $self, 'add_sold_by_single' ), 39 );
        }
    }
XWC_Product_Vendors_Vendor_Frontend::init();

,但无法删除此!请指导我这样做。

update

我有

的输出
print_r($wp_filter['woocommerce_single_product_summary']);
Array
(
    [10] => Array
        (
            [woocommerce_template_single_price] => Array
                (
                    [function] => woocommerce_template_single_price
                    [accepted_args] => 1
                )
        )
    [20] => Array
        (
            [woocommerce_template_single_excerpt] => Array
                (
                    [function] => woocommerce_template_single_excerpt
                    [accepted_args] => 1
                )
        )
    [40] => Array
        (
            [woocommerce_template_single_meta] => Array
                (
                    [function] => woocommerce_template_single_meta
                    [accepted_args] => 1
                )
        )
    [50] => Array
        (
            [woocommerce_template_single_sharing] => Array
                (
                    [function] => woocommerce_template_single_sharing
                    [accepted_args] => 1
                )
        )
    [30] => Array
        (
            [woocommerce_template_single_add_to_cart] => Array
                (
                    [function] => woocommerce_template_single_add_to_cart
                    [accepted_args] => 1
                )
        )
    [39] => Array
        (
            [00000000262d19ef000000007f4708faadd_sold_by_single] => Array
                (
                    [function] => Array
                        (
                            [0] => WC_Product_Vendors_Vendor_Frontend Object
                                (
                                )
                            [1] => add_sold_by_single
                        )
                    [accepted_args] => 1
                )
            [le_child_add_sold_by_single] => Array
                (
                    [function] => le_child_add_sold_by_single
                    [accepted_args] => 1
                )
        )
)

为什么解决方案不适合您?

为了取消回调,您必须获取对象的实例。此插件的问题是:通过给您抓住实例的方法,它无法正确使用单身。因此,您无法获得实例。嘘。

围绕

工作

我们需要解决方法。让我解释以后帮助您。

WordPress Core及其事件注册表存储所有注册的回调。当您执行add_actionadd_filter时,回调将存储在此事件注册表表中(这是一个大型多维数组)。为了取消(或删除)作为特定对象中一种方法的回调(而不是静态方法),您必须具有对象的实例。这意味着您需要代表要定位的对象的变量。

在此示例中,您没有直接的。但是WordPress使用其对象哈希ID与方法名称串联的对象哈希ID存储了该方法的键。我们可以使用该模式在事件注册表中获取记录(元素)。

/**
 * #EXPLANATION#
 * The plugin loads the file using `plugins_loaded` with a priority of 0.  Here
 * we are doing the same thing but after the file is loaded, meaning after the
 * events are registered.
 */
add_action( 'plugins_loaded', 'remove_woocommerce_add_sold_by_single_callback', 1 );
/**
 * Unregister the WooCommerce `WC_Product_Vendors_Vendor_Frontend::add_sold_by_single` from the
 * event `woocommerce_single_product_summary`.
 *
 * #EXPLANATION#
 * I'm adding comments in the code only to illustrate what is happening and why.  Please
 * remove the inline comments when using this code. Thank you.
 *
 * @since 1.0.0
 *
 * @return void
 */
function remove_woocommerce_add_sold_by_single_callback() {
    /**
     * #EXPLANATION#
     * WordPress keeps an event registry table for the callbacks that
     * are pre-registered to each event name.  The format of the table is:
     *
     * $wp_filter[ event name ][ priority number ][ callback name ]
     *
     * The registry table is a global variable called $wp_filter.  You need to include
     * that global into this function's scope.
     */
    global $wp_filter;
    /**
     * #EXPLANATION#
     * Let's make sure that the event name has a callback registered to the priority
     * number of 39. If no, then return early (bail out).
     */
    if ( ! isset( $wp_filter['woocommerce_single_product_summary']['39'] ) ) {
        return;
    }
    /**
     * #EXPLANATION#
     * We will loop through each of the registered callbacks for the priority of 39. Why?
     * You can't assume that only that one function is registered at 39.  There may be more.
     * Therefore, let's loop.
     *
     * We are grabbing the callback function that is registered.  This is a unique key. For
     * objects, WordPress uses the PHP construct spl_object_hash() to grab its hash ID. Then it
     * concatenates it together with the method's name.  You are not going to know what the unique
     * ID is. Why? The first 32 characters are the hash ID, which is a memory location and not
     * relative to the human readable name of `WC_Product_Vendors_Vendor_Frontend`.  Rather, it's
     * related to its object.
     */
    foreach( $wp_filter['woocommerce_single_product_summary']['39'] as $callback_function => $registration ) {
        /**
         * #EXPLANATION#
         * Give that we don't know what the first 32 characters of the object's hash ID is, we need
         * to check if the callback function includes the method we are looking for.  This line
         * of code says: "Hey, do you contain the method name `add_sold_by_single` starting at
         * the 32's character."  If yes, then this is the one we want.
         */
        if ( strpos( $callback_function, 'add_sold_by_single', 32) !== false) {
            /**
             * #EXPLANATION#
             * Now we have the actual callback function key that is registered in the
             * event registry.  We can use remove_action to unregister it.
             */
            remove_action( 'woocommerce_single_product_summary', $callback_function, 39 );
            break;
        }
    }
}

本质上,该代码在做什么:

  1. 检查是否已经注册到事件名称和优先级号。如果没有,没有什么事。让我们纾困。
  2. 我们需要获得唯一的ID,或者按照WordPress称为$idx。这是回调的唯一键,由哈希ID组成。方法名称。
  3. 一旦我们拥有独特的ID,您就可以使用remove_action登录回调。它也适用于remove_filter

可重复使用的代码

上面的代码是说明如何做以及如何工作。但是该代码无法重复使用,因为这些参数是硬编码的。相反,您将需要使用此代码,然后将其称为此代码(在插件中):

add_action( 'plugins_loaded', 'remove_woocommerce_add_sold_by_single_callback', 1 );
/**
 * Unregister the WooCommerce `WC_Product_Vendors_Vendor_Frontend::add_sold_by_single` from the
 * event `woocommerce_single_product_summary`.
 *
 * @since 1.0.0
 *
 * @return void
 */
function remove_woocommerce_add_sold_by_single_callback() {
    do_hard_unregister_object_callback( 'woocommerce_single_product_summary', 39, 'add_sold_by_single');
}

哪个事件?

如果您在插件中使用此代码(应该是),请使用plugins_loaded

如果将其放入主题中,则无法使用plugins_loaded,因为它已经在主题加载之前已经触发。对于主题,您需要使用after_setup_theme而不是plugins_loaded

进行测试

要测试,让我们在之前和之后倒出注册表,以查看是否确实将其删除。执行以下操作:

function remove_woocommerce_add_sold_by_single_callback() {
    global $wp_filter;
    var_dump( $wp_filter['woocommerce_single_product_summary'][39] );
    do_hard_unregister_object_callback( 'woocommerce_single_product_summary', 39, 'add_sold_by_single');
    if ( ! isset( $wp_filter['woocommerce_single_product_summary'][39] ) ) {
        var_dump( $wp_filter['woocommerce_single_product_summary'] );
    } else {
        var_dump( $wp_filter['woocommerce_single_product_summary'][39] );
    }
}

结果将使我们看看它是否在那里开始和之后。

尝试以下:

add_action( 'woocommerce_before_single_product', 'my_remove_actions' );
function my_remove_actions(){
    remove_action( 'woocommerce_single_product_summary', array( 'WC_Product_Vendors_Vendor_Frontend', 'add_sold_by_single' ), 39 );
}

显示此过滤器的所有连接功能:

add_action( 'woocommerce_before_single_product', 'lets_have_a_look' );
function lets_have_a_look(){        
    global $wp_filter;
    print_r($wp_filter['woocommerce_single_product_summary']);
}

,或者您可以通过JavaScript进行。

add_action( 'woocommerce_archive_description', "move_author_summary_to_image");
/**
 * Move the author summary right after the image so we can float them left and right.
 */
function move_author_summary_to_image() {
?>
    <script>
    $('document').ready (function () {
        var summary = $("#isle_body .wcpv-vendor-profile.entry-summary").html();
        $("#isle_body .wcpv-vendor-profile.entry-summary").hide();
        $(summary).addClass("author_summary").insertAfter($("#isle_body p.wcpv-vendor-logo")); 
    });
    </script>
<?php
}

相关内容

  • 没有找到相关文章

最新更新