php中的action类,来自PHP类的remove_action

我试图删除一个动作并以不同的优先级添加它.

以下是有助于生成消息的所有代码段:

包括必需的前端文件

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 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类的操作中将优先级从30更改为15.问题在于,没有明确的方法来调用移除.有什么建议么?

解决方法:

好了,您提供的代码表明WC_Memberships_Restrictions类的实例存储在主类的stricts属性中.

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

从那里,我只需要从主插件文件的底部查找如何访问主要Membership类的实例:

/**

* Returns the One True Instance of Memberships

*

* @since 1.0.0

* @return WC_Memberships

*/

function wc_memberships() {

return WC_Memberships::instance();

}

这意味着现在要访问限制类的实例,我们需要访问主类的限制属性.虽然听起来像泥泞,但基本上意味着:

wc_memberships()->restrictions

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

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 );

标签:woocommerce,hook-woocommerce,wordpress,php

来源: https://codeday.me/bug/20191111/2023050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值