用Claude写的一个WordPress查询WooCommerce购买记录的功能

 用Claude写的一个WordPress查询WooCommerce购买记录的功能
使用短代码创建自定义页面
1. 首先,创建一个新的WordPress页面
2. 然后,将以下短代码添加到你的主题的functions.php文件中
// 添加显示用户订单历史的短代码
function display_user_orders_shortcode() {
    // 如果用户未登录,显示登录提示
    if (!is_user_logged_in()) {
        return '<p>请先<a href="' . wp_login_url(get_permalink()) . '">登录</a>查看您的订单历史。</p>';
    }
    
    // 获取当前用户ID
    $current_user_id = get_current_user_id();
    
    // 获取用户订单
    $customer_orders = wc_get_orders(array(
        'customer_id' => $current_user_id,
        'limit' => -1, // 获取所有订单
        'orderby' => 'date',
        'order' => 'DESC',
    ));
    
    // 如果没有订单,显示提示信息
    if (empty($customer_orders)) {
        return '<p>您还没有任何订单记录。</p>';
    }
    
    // 开始构建订单表格
    $output = '<h2>我的订单历史</h2>';
    $output .= '<table class="woocommerce-orders-table">';
    $output .= '<thead><tr>
                    <th>订单号</th>
                    <th>日期</th>
                    <th>商品</th>
                    <th>状态</th>
                    <th>总金额</th>
                    <th>操作</th>
                </tr></thead>';
    $output .= '<tbody>';
    
    // 遍历所有订单
    foreach ($customer_orders as $order) {
        $order_id = $order->get_id();
        $output .= '<tr>';
        $output .= '<td>#' . $order_id . '</td>';
        $output .= '<td>' . $order->get_date_created()->date_i18n('Y-m-d H:i:s') . '</td>';
        
        // 添加商品名称列
        $output .= '<td>';
        $items = $order->get_items();
        if (!empty($items)) {
            $product_names = array();
            foreach ($items as $item) {
                $product_names[] = $item->get_name() . ' x ' . $item->get_quantity();
            }
            $output .= implode('<br>', $product_names);
        } else {
            $output .= '无商品信息';
        }
        $output .= '</td>';
        
        $output .= '<td>' . wc_get_order_status_name($order->get_status()) . '</td>';
        $output .= '<td>' . $order->get_formatted_order_total() . '</td>';
        $output .= '<td><a href="' . $order->get_view_order_url() . '">查看详情</a></td>';
        $output .= '</tr>';
    }
    
    $output .= '</tbody></table>';
    
    return $output;
}
add_shortcode('user_orders', 'display_user_orders_shortcode');

在需要查询订单记录的页面或文章中,添加以下短码:
user_orders

如果是要查询特定产品的购买记录,如提问“新增一个短码,仅显示woocommerce的特定订单记录,仅显示商品名称以“PCB设计一板即成功专栏”开头,并且支付状态是“已完成”的订单信息。”

广告
// 添加显示用户订单历史的短代码
function display_user_orders_shortcode() {
    // 如果用户未登录,显示登录提示
    if (!is_user_logged_in()) {
        return '<p>请先<a href="' . wp_login_url(get_permalink()) . '">登录</a>查看您的订单历史。</p>';
    }
    
    // 获取当前用户ID
    $current_user_id = get_current_user_id();
    
    // 获取用户订单
    $customer_orders = wc_get_orders(array(
        'customer_id' => $current_user_id,
        'limit' => -1, // 获取所有订单
        'orderby' => 'date',
        'order' => 'DESC',
    ));
    
    // 如果没有订单,显示提示信息
    if (empty($customer_orders)) {
        return '<p>您还没有任何订单记录。</p>';
    }
    
    // 开始构建订单表格
    $output = '<h2>我的订单历史</h2>';
    $output .= '<table class="woocommerce-orders-table">';
    $output .= '<thead><tr>
                    <th>订单号</th>
                    <th>日期</th>
                    <th>商品</th>
                    <th>状态</th>
                    <th>总金额</th>
                    <th>操作</th>
                </tr></thead>';
    $output .= '<tbody>';
    
    // 遍历所有订单
    foreach ($customer_orders as $order) {
        $order_id = $order->get_id();
        $output .= '<tr>';
        $output .= '<td>#' . $order_id . '</td>';
        $output .= '<td>' . $order->get_date_created()->date_i18n('Y-m-d H:i:s') . '</td>';
        
        // 添加商品名称列
        $output .= '<td>';
        $items = $order->get_items();
        if (!empty($items)) {
            $product_names = array();
            foreach ($items as $item) {
                $product_names[] = $item->get_name() . ' x ' . $item->get_quantity();
            }
            $output .= implode('<br>', $product_names);
        } else {
            $output .= '无商品信息';
        }
        $output .= '</td>';
        
        $output .= '<td>' . wc_get_order_status_name($order->get_status()) . '</td>';
        $output .= '<td>' . $order->get_formatted_order_total() . '</td>';
        $output .= '<td><a href="' . $order->get_view_order_url() . '">查看详情</a></td>';
        $output .= '</tr>';
    }
    
    $output .= '</tbody></table>';
    
    return $output;
}
add_shortcode('user_orders', 'display_user_orders_shortcode');

// 仅显示"一板成功专栏"的购买记录
function display_pcb_column_orders_shortcode() {
    // 如果用户未登录,显示登录提示
    if (!is_user_logged_in()) {
        return '<p>请先<a href="' . wp_login_url(get_permalink()) . '">登录</a>查看您的专栏订阅记录。</p>';
    }
    
    // 获取当前用户ID
    $current_user_id = get_current_user_id();
    
    // 获取用户订单
    $customer_orders = wc_get_orders(array(
        'customer_id' => $current_user_id,
        'limit' => -1, // 获取所有订单
        'orderby' => 'date',
        'order' => 'DESC',
        'status' => array('wc-completed'), // 仅显示已完成的订单
    ));
    
    // 筛选出包含特定商品的订单
    $filtered_orders = array();
    foreach ($customer_orders as $order) {
        $items = $order->get_items();
        $has_pcb_column = false;
        
        foreach ($items as $item) {
            $product_name = $item->get_name();
            if (strpos($product_name, 'PCB设计一板即成功专栏') === 0) {
                $has_pcb_column = true;
                break;
            }
        }
        
        if ($has_pcb_column) {
            $filtered_orders[] = $order;
        }
    }
    
    // 如果没有符合条件的订单,显示提示信息
    if (empty($filtered_orders)) {
        return '<p>您还没有购买过"PCB设计一板即成功专栏"相关产品。</p>';
    }
    
    // 开始构建订单表格
    $output = '<h2>我的专栏订阅记录</h2>';
    $output .= '<table class="woocommerce-orders-table">';
    $output .= '<thead><tr>
                    <th>订单号</th>
                    <th>日期</th>
                    <th>专栏名称</th>
                    <th>总金额</th>
                    <th>操作</th>
                </tr></thead>';
    $output .= '<tbody>';
    
    // 遍历筛选后的订单
    foreach ($filtered_orders as $order) {
        $order_id = $order->get_id();
        $output .= '<tr>';
        $output .= '<td>#' . $order_id . '</td>';
        $output .= '<td>' . $order->get_date_created()->date_i18n('Y-m-d H:i:s') . '</td>';
        
        // 添加专栏名称列
        $output .= '<td>';
        $items = $order->get_items();
        $pcb_products = array();
        foreach ($items as $item) {
            $product_name = $item->get_name();
            if (strpos($product_name, 'PCB设计一板即成功专栏') === 0) {
                $pcb_products[] = $product_name . ' x ' . $item->get_quantity();
            }
        }
        $output .= implode('<br>', $pcb_products);
        $output .= '</td>';
        
        $output .= '<td>' . $order->get_formatted_order_total() . '</td>';
        $output .= '<td><a href="' . $order->get_view_order_url() . '">查看详情</a></td>';
        $output .= '</tr>';
    }
    
    $output .= '</tbody></table>';
    
    return $output;
}
add_shortcode('pcb_column_orders', 'display_pcb_column_orders_shortcode');

在您需要显示特定订单记录的页面或文章中,只需添加以下短码:
pcb_column_orders

吴川斌

吴川斌

Leave a Reply