2017-03-07 107 views
0

我正在使用YITH WooCommerce訂閱,並且我想檢查我的購物車是否訂閱了我想禁用我的支付網關之一。我怎麼辦?檢查購物車在YITH WooCommerce訂閱Wordpress中是否有活動訂閱

我已經試過這段代碼,但這是行不通的。

add_filter('woocommerce_available_payment_gateways', 'filter_gateways', 1); 
function filter_gateways($gateways){ 

    if (has_woocommerce_subscription('','','active')) { 
     unset($gateways['pin_payments']); 
    } 
    return $gateways; 
} 

function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) { 
    $current_user = wp_get_current_user(); 
    if (empty($the_user_id)) { 
     $the_user_id = $current_user->ID; 
    } 
    if (WC_Subscriptions_Manager::user_has_subscription($the_user_id, $the_product_id, $the_status)) { 
     return true; 
    } 
} 

回答

0

你有2個問題,一個是主動訂閱,您可以通過此功能測試:

function CheckSubscriptionByEmail($email) { 

     // getting all the records of the user by email for the subscription 
     $subscriptions = get_posts(array(
      'numberposts' => -1, 
      'post_type' => 'ywsbs_subscription', // Subscription post type 
      'orderby' => 'post_date', // ordered by date 
      'order' => 'ASC', 
      'meta_query' => array(
      array(
       'key' => '_billing_email', //additional fields being used to get the data 
       'value' => $email, 
       'compare' => '=' 
      ), 
      array(
       'key' => '_status', 
       'value' => 'active', //active subscriptions 
       'compare' => '=' 
      )) 
     )); 

     // check if user has any active subscription 
     foreach ($subscriptions as $post) : setup_postdata($post); 
      $nextdue = get_post_meta(get_the_id(), '_payment_due_date',true); 
      $current_date = date("Y-m-d"); 
      $date_to_compare = date("Y-m-d",strtotime($nextdue)); 
      if (strtotime($date_to_compare) > strtotime($current_date)) { 
       echo "active"; //returns active 
       break; 
      } 
     endforeach; 
     wp_reset_postdata(); 

    } 

Refernece和解釋在: https://makersbyte.com/check-active-subscriptions-using-yith-woocommerce/

如果你只想測試所有然後在上面的函數中改變如下:

 $subscriptions = get_posts(array(
      'numberposts' => -1, 
      'post_type' => 'ywsbs_subscription', // Subscription post type 
      'orderby' => 'post_date', // ordered by date 
      'order' => 'ASC', 
      'meta_query' => array(
      array(
       'key' => '_status', 
       'value' => 'active', //active subscriptions 
       'compare' => '=' 
      )) 
     )); 

一旦您擁有活動訂閱的列表,如果需要禁用它們,它會相當直接和簡單。

支付網關,只需轉到Woocommerce - >結帳>所有支付網關都列在其中。使用它來禁用。