WooCommerce 订阅用于重复总计的自定义短代码

问题描述 投票:0回答:1

我需要一个短代码来显示 WooCommerce 订阅订单的每月经常性总计。结账后,我要求客户为他们刚刚下的租赁订单签署租约,并需要一种方式来显示他们的第一个月付款是多少。因为我按比例分配,所以订单小计和总计不包含正确的信息。

我根据此处的文档尝试使用以下内容:https://woocommerce.com/document/subscriptions/develop/functions/

add_shortcode('wdm_recurring_totals', 'wmd_my_custom_function');
function wmd_my_custom_function(){
    WC_Subscription::get_total();
}

似乎只是在测试时抛出错误(我对 PHP 缺乏经验)

php wordpress woocommerce woocommerce-subscriptions
1个回答
0
投票

您可以通过创建自定义短代码来计算 WooCommerce 订阅的每月经常性总额来实现此目的。操作方法如下: add_shortcode('wdm_recurring_totals', 'wmd_my_custom_function');

function wmd_my_custom_function() {
// Check if the user is logged in
if ( ! is_user_logged_in() ) {
    return 'You need to be logged in to view this information.';
}

// Get the current user's subscriptions
$user_id = get_current_user_id();
$subscriptions = wcs_get_users_subscriptions( $user_id );

// Initialize total
$total = 0;

// Loop through each subscription
foreach ( $subscriptions as $subscription ) {
    // Check if the subscription is active
    if ( $subscription->get_status() == 'active' ) {
        // Get the monthly recurring total
        $total += $subscription->get_total();
    }
}

// Check if total is zero
if ( $total === 0 ) {
    return 'You have no active subscriptions.';
}

// Return the formatted total
return 'Your first monthly payment will be: ' . wc_price( $total );

}

© www.soinside.com 2019 - 2024. All rights reserved.