【问题标题】:Check if cart have active subscriptions in YITH WooCommerce Subscription Wordpress检查购物车是否在 YITH WooCommerce 订阅 Wordpress 中有有效订阅
【发布时间】:2017-03-07 02:34:13
【问题描述】:

我正在使用 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;
    }
}

【问题讨论】:

    标签: wordpress woocommerce subscriptions


    【解决方案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 > 所有支付网关都在其中列出。用它来禁用。

    【讨论】:

      猜你喜欢
      • 2016-12-29
      • 2017-06-18
      • 2015-01-28
      • 2021-01-10
      • 2017-09-10
      • 2011-07-04
      • 2021-07-01
      • 2022-10-14
      • 2018-07-04
      相关资源
      最近更新 更多