WordPress WooCommerce 在 /my-account/subscriptions/ 页的每一行中添加新字段

WordPress WooCommerce add new field in each row at /my-account/subscriptions/ page

我正在使用带有 WooCommerce and WooCommerce subscriptions 插件的 WordPress,下面是我的代码,我在其中添加了一个名为 (Mindesk VAR Client User - Dropdown) 的自定义字段以显示在“编辑订阅”管理页面中,根据订阅 ID 保存到我的自定义字段。

这是它的样子。

这是我的工作代码。

<?php 
// action triggered when we go to add/edit subscription page  
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');

add_action('woocommerce_process_shop_order_meta', 'saveWCSubscriptionCustomFields');

function showWCSubscriptionCustomFields($subscription) {

    $currentPage = get_current_screen();

    // If page is "Edit Subscription" page, then only show 
    if ($currentPage->action == 'add')
        return;
    

    // Getting all the users 
    $mindeskUsers = getAllUsers();

?>
    <br class="clear" />
    <p class="form-field form-field-wide">
        <label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
        <?php

        $selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);

        echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
        ?>
    </p>
<?php

}


function saveWCSubscriptionCustomFields($subscription_id) {

    // wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions 
    update_post_meta($subscription_id, 'mindesk_wc_subscriptions_var_client_user_id', wc_clean($_POST['mindesk_wc_subscriptions_var_client_user_id']));
}

这对我来说很好。

现在我有一个自定义要求,即在 http:://www.mywebsite.com/my-account/subscriptions/ 页面的每一行中添加一个名为 Transfer 的按钮。

例如这个页面在 Total.

旁边的某处

点击那个按钮后,弹出窗口应该带有一个表单,我应该能够保存一个字段“转移到 VAR 客户端用户”基于订阅 ID 与我已经用我的自定义字段向您展示了上面的工作代码。

我曾尝试进行研发,但大多数链接都建议添加自定义字段,并且都在“我的帐户”页面中 (/my-account) 但我想要在“my-account/subscriptions”页面中实现相同。

任何人都可以指导我如何实现这一目标吗?任何建议将不胜感激。

谢谢

您可以从 woocommerce-subscriptions/templates/myaccount 复制 my-subscriptions.php 文件并将其添加到您的活动主题 woocommerce 文件夹创建文件夹 myaccount 并粘贴 my-subscriptions.php。然后根据您的要求进行修改。

<?php if ( ! empty( $subscriptions ) ) : ?>
<table class="my_account_subscriptions my_account_orders woocommerce-orders-table woocommerce-MyAccount-subscriptions shop_table shop_table_responsive woocommerce-orders-table--subscriptions">

<thead>
    <tr>
        <th class="subscription-id order-number woocommerce-orders-table__header woocommerce-orders-table__header-order-number woocommerce-orders-table__header-subscription-id"><span class="nobr"><?php esc_html_e( 'Subscription', 'woocommerce-subscriptions' ); ?></span></th>
        <th class="subscription-status order-status woocommerce-orders-table__header woocommerce-orders-table__header-order-status woocommerce-orders-table__header-subscription-status"><span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></span></th>
        <th class="subscription-next-payment order-date woocommerce-orders-table__header woocommerce-orders-table__header-order-date woocommerce-orders-table__header-subscription-next-payment"><span class="nobr"><?php echo esc_html_x( 'Next payment', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
        <th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Total', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
        <th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Transfer', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
        <th class="subscription-actions order-actions woocommerce-orders-table__header woocommerce-orders-table__header-order-actions woocommerce-orders-table__header-subscription-actions">&nbsp;</th>
    </tr>
</thead>

<tbody>
<?php /** @var WC_Subscription $subscription */ ?>
<?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?>
    <tr class="order woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $subscription->get_status() ); ?>">
        <td class="subscription-id order-number woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-id woocommerce-orders-table__cell-order-number" data-title="<?php esc_attr_e( 'ID', 'woocommerce-subscriptions' ); ?>">
            <a href="<?php echo esc_url( $subscription->get_view_order_url() ); ?>"><?php echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'woocommerce-subscriptions' ), $subscription->get_order_number() ) ); ?></a>
            <?php do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription ); ?>
        </td>
        <td class="subscription-status order-status woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-status woocommerce-orders-table__cell-order-status" data-title="<?php esc_attr_e( 'Status', 'woocommerce-subscriptions' ); ?>">
            <?php echo esc_attr( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?>
        </td>
        <td class="subscription-next-payment order-date woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-next-payment woocommerce-orders-table__cell-order-date" data-title="<?php echo esc_attr_x( 'Next Payment', 'table heading', 'woocommerce-subscriptions' ); ?>">
            <?php echo esc_attr( $subscription->get_date_to_display( 'next_payment' ) ); ?>
            <?php if ( ! $subscription->is_manual() && $subscription->has_status( 'active' ) && $subscription->get_time( 'next_payment' ) > 0 ) : ?>
            <br/><small><?php echo esc_attr( $subscription->get_payment_method_to_display( 'customer' ) ); ?></small>
            <?php endif; ?>
        </td>
        <td class="subscription-total order-total woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-total woocommerce-orders-table__cell-order-total" data-title="<?php echo esc_attr_x( 'Total', 'Used in data attribute. Escaped', 'woocommerce-subscriptions' ); ?>">
            <?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?>
        </td>
        <td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
            <a href="<?php echo esc_url( $subscription->get_view_order_url() ) ?>" class="woocommerce-button button view"><?php echo esc_html_x( 'Transfer', 'Transfer a subscription', 'woocommerce-subscriptions' ); ?></a>
        </td>
        <td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
            <a href="<?php echo esc_url( $subscription->get_view_order_url() ) ?>" class="woocommerce-button button view"><?php echo esc_html_x( 'View', 'view a subscription', 'woocommerce-subscriptions' ); ?></a>
            <?php do_action( 'woocommerce_my_subscriptions_actions', $subscription ); ?>
        </td>
    </tr>
<?php endforeach; ?>
</tbody>

</table>
    <?php if ( 1 < $max_num_pages ) : ?>
        <div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
        <?php if ( 1 !== $current_page ) : ?>
            <a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce-subscriptions' ); ?></a>
        <?php endif; ?>

        <?php if ( intval( $max_num_pages ) !== $current_page ) : ?>
            <a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce-subscriptions' ); ?></a>
        <?php endif; ?>
        </div>
    <?php endif; ?>
<?php else : ?>
    <p class="no_subscriptions woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
        <?php if ( 1 < $current_page ) :
            printf( esc_html__( 'You have reached the end of subscriptions. Go to the %sfirst page%s.', 'woocommerce-subscriptions' ), '<a href="' . esc_url( wc_get_endpoint_url( 'subscriptions', 1 ) ) . '">', '</a>' );
        else :
            esc_html_e( 'You have no active subscriptions.', 'woocommerce-subscriptions' );
            ?>
            <a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
                <?php esc_html_e( 'Browse products', 'woocommerce-subscriptions' ); ?>
            </a>
        <?php
    endif; ?>
    </p>

<?php endif; ?>

已测试并有效