最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do I display a user's previous orders as a select box option on a product?

programmeradmin1浏览0评论

I have an audio customization site (Wordpress/Woocommerce) that allows users to purchase licensed music in either an 'as-is' state or a customized version of it. The specifications of a customization are determined before the product is added to cart via monetized on-page options (eg. voiceovers, sound fx, etc).

Turns out a lot of customers don't know they want it customized until days/weeks/months after purchasing. So I created individual products that mirror the product options so customers can still purchase them and basically 'apply them' to a previous order.

What I would like to do is to create a select box option on each of these products that calls the user's previous order numbers, so user's may specify which previous order they are applying this new purchase to.

If anyone can provide any direction on this I would greatly appreciate it.

I have an audio customization site (Wordpress/Woocommerce) that allows users to purchase licensed music in either an 'as-is' state or a customized version of it. The specifications of a customization are determined before the product is added to cart via monetized on-page options (eg. voiceovers, sound fx, etc).

Turns out a lot of customers don't know they want it customized until days/weeks/months after purchasing. So I created individual products that mirror the product options so customers can still purchase them and basically 'apply them' to a previous order.

What I would like to do is to create a select box option on each of these products that calls the user's previous order numbers, so user's may specify which previous order they are applying this new purchase to.

If anyone can provide any direction on this I would greatly appreciate it.

Share Improve this question edited Jan 12, 2021 at 21:21 Glorfindel 6093 gold badges10 silver badges18 bronze badges asked Jan 11, 2021 at 21:08 SchrodingersCatSchrodingersCat 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can try something like this:

add_action( 'woocommerce_before_add_to_cart_form', 'add_list_completed_orders' );
function add_list_completed_orders() {
    // get orders
    $orders = wc_get_orders (
        array (
            'status'        => array( 'wc-completed' ),
            'type'          => 'shop_order',
            'limit'         => -1,
            'return'        => 'ids',
            'customer_id'   => get_current_user_id(),
        )
    );

    if ( ! empty( $orders ) ) {
        // set value options
        $options = '';
        foreach ( $orders as $order_id ) {
            $options .= '<option value="' . $order_id . '">' . $order_id . '</option>';
        }
        // add list
        $form = '<label for="orders">Select completed orders:</label>';
        $form .= '<select id="orders" name="orders">' . $options . '</select>';
        echo $form;
    }

}

Tested and it works. The code goes into your theme's functions.php.

发布评论

评论列表(0)

  1. 暂无评论