2017-01-02 4 views
3

조치를 제거하고 다른 우선 순위로 추가하려고합니다. ,remove_action PHP 클래스

이 필요 프론트 엔드 파일

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에서 30으로 변경해야합니다. 문제는 제거를 호출 할 명확한 방법이 없다는 것입니다. 어떤 제안?

답변

3

코드는 WC_Memberships_Restrictions 클래스의 인스턴스가 기본 클래스 'restrictions 속성에 저장되어 있음을 보여줍니다.

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

는 거기에서 난 그냥 당신이 볼 수있는 주요 플러그인 파일의 바닥에서, 주요 회원 클래스의 인스턴스에 액세스하는 방법을 찾아했다 :

/** 
* 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); 
+0

아름다운! 정말 고맙습니다! 'wc_memberships() -> restrictions'가 아닌'$ restrictions'를 배열하려고하는 것을 제외하고는 비슷한 해결책을 가졌습니다. – Nalakira