Home > Software engineering >  How to Access the Order ID in the New Section on the My Account Page?
How to Access the Order ID in the New Section on the My Account Page?

Time:10-22

Customers must confirm a contract for each order when purchasing products. Customers cannot purchase the product without confirming this agreement.

I created a checkbox for this on the checkout page. After customers confirm this, they buy the product. I save the approved contracts to the database. I can find it with the order number. So far, no problem.

On the My Account page, I created a new section (eg orders): Contracts. From here, every customer can access the contracts. However, I cannot access the contracts from this section that I have created. Because I can't get the order id.

Can I reach the order number with the current user id? I couldn't find any results for this. Or is there a different way?

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

CodePudding user response:

For questions like this it can be useful to see how it is applied by default in WooCommerce, and base your answer on that

In /includes/wc-template-functions.php on line 3157 - 3186 @version 2.5.0 we find the following:

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,
            )
        );
    }
}

As you can see, wc_get_orders() and get_current_user_id() is used to get the orders for the current customer.

Then the results ($customer_orders) are passed to the myaccount/orders.php template file.

In the template file, a foreach is then used to display the orders and the necessary/desired details

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

So to answer your question "Can I reach the order number with the current user id?", yes this is possible and is indeed the right way to proceed

  • Related