检查购物车在YITH WooCommerce订阅Wordpress中是否有活动订阅
问题描述:
我正在使用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;
}
}
答
你有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 - >结帐>所有支付网关都列在其中。使用它来禁用。