WooCommerce:如何在orders.php中使用my-subscriptions.php的$subscriptions变量
解决WooCommerce Orders.php中“Undefined variable: subscriptions”错误的方案
你遇到的这个问题很典型——直接把my-subscriptions.php里的代码复制到orders.php后,$subscriptions变量根本没被定义过,自然会报错。my-subscriptions.php里的这个变量是由WooCommerce Subscriptions扩展的模板逻辑提前初始化的,但orders.php默认没有这个步骤。下面是具体的解决方法:
步骤1:在Orders.php中初始化订阅变量
首先,你需要在orders.php里添加代码,获取当前用户的订阅数据。WooCommerce Subscriptions提供了专门的函数wc_get_users_subscriptions()来做这件事。为了避免扩展未激活时的报错,记得先做函数存在性检查:
// 在orders.php合适的位置(比如订单列表之前/之后)添加这段代码来初始化订阅变量 if ( function_exists( 'wc_get_users_subscriptions' ) ) { // 获取当前登录用户的所有订阅 $subscriptions = wc_get_users_subscriptions( get_current_user_id() ); } else { // 如果Subscriptions扩展未激活,设为空数组防止后续代码报错 $subscriptions = array(); }
步骤2:整合订阅展示代码
接下来,把你从my-subscriptions.php复制来的订阅列表代码,包裹在一个判断$subscriptions非空的条件里,确保只有当用户有订阅时才展示:
<?php if ( ! empty( $subscriptions ) ) : ?> <h2><?php esc_html_e( 'My Subscriptions', 'woocommerce-subscriptions' ); ?></h2> <table class="woocommerce-MyAccount-subscriptions shop_table shop_table_responsive my_account_subscriptions"> <thead> <tr> <th class="subscription-id"><span class="nobr"><?php esc_html_e( 'Subscription', 'woocommerce-subscriptions' ); ?></span></th> <th class="subscription-status"><span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></span></th> <th class="subscription-next-payment"><span class="nobr"><?php esc_html_e( 'Next Payment', 'woocommerce-subscriptions' ); ?></span></th> <th class="subscription-total"><span class="nobr"><?php esc_html_e( 'Total', 'woocommerce-subscriptions' ); ?></span></th> <th class="subscription-actions"><?php esc_html_e( 'Actions', 'woocommerce-subscriptions' ); ?></th> </tr> </thead> <tbody> <?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?> <?php // 确保当前用户有权限查看该订阅 if ( ! current_user_can( 'view_subscription', $subscription_id ) ) { continue; } ?> <tr class="subscription subscription-<?php echo esc_attr( $subscription_id ); ?>"> <td class="subscription-id" data-title="<?php esc_attr_e( 'Subscription', 'woocommerce-subscriptions' ); ?>"> <a href="<?php echo esc_url( $subscription->get_view_order_url() ); ?>"><?php echo esc_html( sprintf( _x( '#%s', 'hash before subscription ID', 'woocommerce-subscriptions' ), $subscription->get_order_number() ) ); ?></a> <?php do_action( 'woocommerce_my_subscriptions_subscription_number', $subscription_id, $subscription ); ?> </td> <td class="subscription-status" data-title="<?php esc_attr_e( 'Status', 'woocommerce-subscriptions' ); ?>"> <?php echo esc_html( wc_get_order_status_name( $subscription->get_status() ) ); ?> </td> <td class="subscription-next-payment" data-title="<?php esc_attr_e( 'Next Payment', 'woocommerce-subscriptions' ); ?>"> <?php echo $subscription->get_next_payment_date_html(); ?> </td> <td class="subscription-total" data-title="<?php esc_attr_e( 'Total', 'woocommerce-subscriptions' ); ?>"> <?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?> </td> <td class="subscription-actions"> <?php // 获取订阅的可用操作按钮 $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() ); if ( ! empty( $actions ) ) { foreach ( $actions as $key => $action ) { echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>'; } } ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?>
步骤3:注意模板重载的最佳实践
不要直接修改WooCommerce或Subscriptions的核心模板文件,建议在你的子主题中创建woocommerce/myaccount/orders.php文件,然后把修改后的代码放进去。这样WooCommerce更新时不会覆盖你的自定义内容。
验证测试
- 确保WooCommerce Subscriptions扩展已激活
- 登录一个有订阅的用户账户
- 访问“我的账户 > 订单”页面,应该能正常看到订阅列表,且不再出现
Undefined variable: subscriptions错误
内容的提问来源于stack exchange,提问作者RForster
相关产品推荐
相关产品推荐

