Проверьте, есть ли в корзине активные подписки в YITH WooCommerce Subscription Wordpress

Я использую подписку 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;
    }
}

person Antonio    schedule 07.03.2017    source источник


Ответы (1)


У вас есть 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();

    }

Ссылка и пояснения по адресу: 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 -> Checkout > В нем перечислены все платежные шлюзы. Используйте его для отключения.

person Shahzaib Khan    schedule 22.09.2017