WordPress WooCommerce在/my-account/subscriptions/页面的每一行中添加新字段 [英] WordPress WooCommerce add new field in each row at /my-account/subscriptions/ page

查看:59
本文介绍了WordPress WooCommerce在/my-account/subscriptions/页面的每一行中添加新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将WordPress与

这是我的工作代码.

 <?php//当我们进入添加/编辑订阅页面时触发的操作add_action('woocommerce_admin_order_data_after_order_details','showWCSubscriptionCustomFields');add_action('woocommerce_process_shop_order_meta','saveWCSubscriptionCustomFields');函数showWCSubscriptionCustomFields($ subscription){$ currentPage = get_current_screen();//如果页面是编辑订阅",页面,然后仅显示如果($ currentPage-> action =='add')返回;//获取所有用户$ mindeskUsers = getAllUsers();?>< br class =清除";/>< p class ="form-field form-field-wide"< label for ="mindesk_wc_subscriptions_var_client_user_id"> Mindesk VAR客户用户:</label><?php$ selectedUser = get_post_meta($ subscription-> get_id(),'mindesk_wc_subscriptions_var_client_user_id',true);回声getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id',$ selectedUser,$ mindeskUsers,'mindesk_select2');?></p><?php}函数saveWCSubscriptionCustomFields($ subscription_id){//wc_clean()和wc_sanitize_textarea()是WooCommerce消毒功能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/页面.

例如,此页面位于总计旁边.

单击该按钮后,弹出窗口应该带有一个表格,我应该能够保存一个字段转移到VAR客户用户"基于订阅ID,就像我在上面的自定义字段中向您显示的工作代码一样.

我尝试进行R&D,但大多数链接建议添加自定义字段,并且所有链接都显示在我的帐户"中页(/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-订单表woocommerce-MyAccount-subscriptions shop_table shop_table_response woocommerce-orders-tables-subscriptions"< thead>< tr>< th class ="subscription-id订单号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 =预订状态订单状态woocommerce-orders-table_header woocommerce-订单表__header-order-status woocommerce-orders-table__header-subscription-status"< span class ="nobr"<?php esc_html_e('Status','woocommerce-subscriptions');&/span/;/th.< th class =订购-下次付款订单日期woocommerce-orders-table__header woocommerce-订购表__header-order-date woocommerce-orders-table__header-subscription-next-payment">"span class ="nobr"<?php echo esc_html_x('下次付款','表格标题','woocommerce-subscriptions');&/span/;/th.< th class =预订-总订单-总woocommerce-订单-表__header woocommerce-订单-表__header-订单-总woocommerce-orders-表__header-subscription-total",< span class ="nobr"<?php echo esc_html_x('Total','table heading','woocommerce-subscriptions');&/span/;/th.< th class =预订-总订单-总woocommerce-订单-表__header woocommerce-订单-表__header-订单-总woocommerce-orders-表__header-subscription-total",< span class ="nobr"<?php echo esc_html_x('Transfer','table heading','woocommerce-subscriptions');&/span/;/th.< th class =订阅动作订单动作woocommerce-orders-table__header woocommerce-orders-table__header-subscription-actions"> </th></tr></thead>< tbody><?php/** @var WC_Subscription $ subscription */?><?php foreach($ subscriptions为$ 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','订单号前的哈希','woocommerce-subscriptions'),$ subscription-> get_order_number())));</a><?php do_action('woocommerce_my_subscriptions_after_subscription_id',$ subscription);?></td>< td class =订阅状态订单状态woocommerce订单表__单元woocommerce订单表__cell订阅状态woocommerce订单表__cell订单状态"data-title =<?php esc_attr_e('Status','woocommerce-subscriptions');?>><?php echo esc_attr(wcs_get_subscription_status_name($ subscription-> get_status()));?></td>< td class =预订-下次付款订单日期woocommerce订单表__单元woocommerce-订单表__cell-预订-下次付款woocommerce订单表__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 =订阅-总订单-总woocommerce-订单表__单元woocommerce-订单表-单元格-订阅-总woocommerce-订单表__单元格-总"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 ="订阅动作订单动作woocommerce-orders-table__cell woocommerce-订单表-__ cell-subscription-actions woocommerce-orders-table__cell-order-actions>< a href ="<?php echo esc_url($ subscription-> get_view_order_url())吗?php echo echo esc_html_x('Transfer','Transfer a subscription','woocommerce-subscriptions');</a></td>< td class ="订阅动作订单动作woocommerce-orders-table__cell woocommerce-订单表-__ 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-没有数字woocommerce-Pagination"><?php if(1!== $ current_page):?>< 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('上一个','woocommerce-subscriptions');</a><?php endif;?><?php if(intval($ max_num_pages)!== $ current_page):?><类别="woocommerce-按钮woocommerce-按钮-下一个woocommerce-Button woocommerce-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-消息woocommerce-消息-信息woocommerce-消息woocommerce-消息-信息woocommerce-info"><?php if(1< $ current_page):printf(esc_html __('您已结束订阅.转到%sfirst page%s.','woocommerce-subscriptions'),'< a href =''.esc_url(wc_get_endpoint_url('subscriptions',1)).别的 :esc_html_e('您没有有效的订阅.','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('浏览产品','woocommerce-subscriptions');?></a><?php万一;?></p><?php endif;?> 

经测试可正常工作

I am using WordPress with WooCommerce and WooCommerce subscriptions plugins and below is my code in which I have added a custom field called (Mindesk VAR Client User - Dropdown) to show in "Edit Subscription" admin page saving to my custom field based on subscription ID.

This is how it's looking like.

And this is my working code.

<?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']));
}

This is working fine for me.

Now I have a custom requirement to add a button something called as Transfer in each row in http:://www.mywebsite.com/my-account/subscriptions/ page.

For example this page somewhere beside Total.

After clicking on that button, the popup should come with a form and I should be able to save a field "Transfer to VAR Client User" based on subscription ID same as I have shown you a working code above with my custom field.

I have tried to do R & D but most of the links suggesting to add custom fields and all in "My Account" page (/my-account) But I want to achieve the same in "my-account/subscriptions" page.

Can anyone guide me how can I achieve this? Any suggestion will be highly appreciated.

Thanks

解决方案

You can copy the my-subscriptions.php file from the woocommerce-subscriptions/templates/myaccount and add it to your active theme woocommerce folder create folder myaccount and paste my-subscriptions.php. and then modify as per your requirement.

<?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; ?>

Tested and works

这篇关于WordPress WooCommerce在/my-account/subscriptions/页面的每一行中添加新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆