检查购物车是否在YITH WooCommerce Subscription Wordpress中有活动订阅

时间:2017-03-07 02:34:14

标签: wordpress woocommerce subscriptions

我正在使用YITH WooCommerce订阅,我想检查我的购物车中是否有订阅我要禁用其中一个支付网关。我该怎么办?

我已经尝试过此代码,但这不起作用。

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

1 个答案:

答案 0 :(得分:0)

您有两个问题,一个是您可以通过此功能测试的有效订阅:

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

    }

参考和解释: 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 - >结帐>所有支付网关都列在其中。用它来禁用。