如何在“我的帐户”页面的新部分中访问订单 ID?

How to Access the Order ID in the New Section on the My Account Page?

客户在购买产品时必须确认每笔订单的合同。未确认此协议,客户不能购买产品。

我在结帐页面上为此创建了一个复选框。客户确认后,他们购买产品。我将批准的合同保存到数据库中。我可以通过订单号找到它。到目前为止,没问题。

在“我的帐户”页面上,我创建了一个新部分(例如订单):合同。 从这里,每个客户都可以访问合同。但是,我无法从我创建的此部分访问合同。因为我拿不到订单号。

我可以用当前的用户id打到订单号吗?我找不到任何结果。或者有什么不同的方法吗?

<?php
class WooContractMyAccount
{ 
 private $MyAccountContractsEndPoint = 'registered-contracts ';
    public function __construct()
    {   
    add_filter('woocommerce_account_menu_items', array($this, 'MyAccountRegistredContracts'), 10, 1);
    add_action('woocommerce_account_' . $this->MyAccountContractsEndPoint . '_endpoint', array($this, 'MyAccountRegistredContractsEndPoint'), 10, 1);
    add_action('init', array($this, 'WooContractInit'));
    }
  
    public function MyAccountRegistredContracts($items)
    {
        $items = array($this->MyAccountContractsEndPoint => __('Registered Contracts')) + $items;
        return $items;
    }



    public function WooContractInit()
    {

        add_rewrite_endpoint($this->MyAccountContractsEndPoint, EP_ROOT | EP_PAGES);
    }
  
}


  public function MyAccountRegistredContractsEndPoint()
    {
        global $wpdb;
        $table = $wpdb->prefix . 'registered_contracts';
        $table_contracts = $wpdb->prefix . 'contracts';
        
        ////The problem is here! ($contracts)
        
        $contracts = $wpdb->get_results($wpdb->prepare("SELECT contracts.name,registeredcontracts.date,registeredcontracts.file FROM $table registeredcontracts
            left join $table_contracts contracts ON contracts.id=registeredcontracts.contracts WHERE ref = %d", _______order id must be written_______));
    ?><div class="woocommerce-MyAccount-content">
            <?php if (count($contracts) > 0) : ?>
                <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
                    <thead>
                        <tr>
                            <th class="woocommerce-orders-table__header">
                                <?php _e('Contract') ?>
                            </th>
                            <th class="woocommerce-orders-table__header">
                                <?php _e('Date') ?>
                            </th>
                            <th class="woocommerce-orders-table__header">
                                <?php _e('File') ?>
                            </th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php foreach ($contracts as $contract) : ?>
                            <tr>
                                <td><?php _e($contract->name); ?></td>
                                <td><?php echo $contract->date; ?></td>
                                <td><a href="<?php echo CONTRACT_URL . '/' . $contract->file; ?>" target="_blank" class="woocommerce-button button"><?php _e('Download Contract'); ?></a></td>
                            </tr>
                        <?php endforeach; ?>



                    </tbody>
                </table>
            <?php else : ?>
                <p><?php _e('You do not have a registered contract. ') ?></p>
            <?php endif; ?>

        </div>

    <?php
    }

对于此类问题,了解它在 WooCommerce 中的默认应用方式可能很有用,并以此为基础回答

/includes/wc-template-functions.php 行 3157 - 3186 @version 2.5.0 我们发现以下内容:

if ( ! function_exists( 'woocommerce_account_orders' ) ) {

    /**
     * My Account > Orders template.
     *
     * @param int $current_page Current page number.
     */
    function woocommerce_account_orders( $current_page ) {
        $current_page    = empty( $current_page ) ? 1 : absint( $current_page );
        $customer_orders = wc_get_orders(
            apply_filters(
                'woocommerce_my_account_my_orders_query',
                array(
                    'customer' => get_current_user_id(),
                    'page'     => $current_page,
                    'paginate' => true,
                )
            )
        );

        wc_get_template(
            'myaccount/orders.php',
            array(
                'current_page'    => absint( $current_page ),
                'customer_orders' => $customer_orders,
                'has_orders'      => 0 < $customer_orders->total,
            )
        );
    }
}

如您所见,wc_get_orders() and get_current_user_id()用于获取当前客户的订单。

然后将结果 ($customer_orders) 传递到 myaccount/orders.php 模板文件。

在模板文件中,foreach 用于显示订单和 necessary/desired 详细信息

<?php
foreach ( $customer_orders->orders as $customer_order ) {
    $order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
    // Get order ID
    $order_id = $order->get_id();

所以回答你的问题“我可以用当前的用户 ID 找到订单号吗?”,是的,这是可能的,确实是正确的方法