wp短代码示例

一、生成评论

// 生成评论
add_action('admin_init', 'generate_furniture_reviews_test');

function generate_furniture_reviews_test() {
    if (!current_user_can('administrator')) return;

    if (isset($_GET['generate_reviews']) && $_GET['generate_reviews'] === '1') {
        $product_ids = [596, 593, 589, 587, 579, 577, 287, 285, 88, 26]; // 所有产品 ID
        $first_names = ['James', 'Emma', 'Liam', 'Olivia', 'Noah', 'Ava', 'William', 'Sophia', 'Michael', 'Isabella'];
        $last_names = ['Smith', 'Johnson', 'Brown', 'Taylor', 'Wilson', 'Davis', 'Clark', 'Lewis', 'Walker', 'Hall'];
        $positive_phrases = [
            'This furniture is amazing!',
            'So beautiful and stylish.',
            'Great value for the price!',
            'Very affordable and durable.',
            'Fast logistics, arrived quickly!',
            'Love the design, highly recommend.',
            'Excellent quality for the cost!',
            'Stunning piece, worth every penny.',
            'Super practical and elegant.',
            'Delivery was super fast, love it!'
        ];

        foreach ($product_ids as $product_id) {
            $comment_count = rand(15, 30);
            for ($i = 0; $i < $comment_count; $i++) {
                $first_name = $first_names[array_rand($first_names)];
                $last_name = $last_names[array_rand($last_names)];
                $author = $first_name . ' ' . $last_name;
                $email = strtolower(str_replace(' ', '.', $author)) . '@gmail.com';

                // 生成 10-30 字的英文评论
                $word_count = rand(10, 30);
                $words = [];
                for ($j = 0; $j < $word_count; $j++) {
                    $words[] = $positive_phrases[array_rand($positive_phrases)];
                }
                $comment_content = implode(' ', $words);
                $comment_content = preg_replace('/\.,/', '.', $comment_content); // 清理多余标点
                $comment_content = trim(preg_replace('/\s+/', ' ', $comment_content)); // 清理多余空格

                // 设置评论时间(2025年7月)
                $day = rand(1, 30); // 7月有31天,但避免溢出取30
                $hour = rand(0, 23);
                $minute = rand(0, 59);
                $second = rand(0, 59);
                $comment_date = "2025-07-" . str_pad($day, 2, '0', STR_PAD_LEFT) .
                               " " . str_pad($hour, 2, '0', STR_PAD_LEFT) . ":" .
                               str_pad($minute, 2, '0', STR_PAD_LEFT) . ":" .
                               str_pad($second, 2, '0', STR_PAD_LEFT);

                // 创建评论
                $comment_data = [
                    'comment_post_ID' => $product_id,
                    'comment_author' => $author,
                    'comment_author_email' => $email,
                    'comment_author_url' => '',
                    'comment_content' => $comment_content,
                    'comment_type' => 'review',
                    'comment_parent' => 0,
                    'user_id' => 0,
                    'comment_date' => $comment_date,
                    'comment_approved' => 1,
                ];

                $comment_id = wp_insert_comment($comment_data);

                // 添加评分 (1-5 星随机)
                $rating = rand(1, 5);
                add_comment_meta($comment_id, 'rating', $rating);
            }
        }

        wp_die('✅ Successfully generated furniture reviews for all product IDs!');
    }
}

一、生成订单

add_action('admin_init', 'create_50_realistic_orders_v2');

function create_50_realistic_orders_v2() {
    if (!current_user_can('administrator')) return;

    if (isset($_GET['create_us_orders']) && $_GET['create_us_orders'] === '1') {
        $states = ['NY' => 'New York', 'CA' => 'California', 'TX' => 'Texas', 'FL' => 'Florida', 'IL' => 'Illinois', 'AZ' => 'Arizona', 'PA' => 'Pennsylvania'];
        $cities = [
            'New York' => '10001',
            'Los Angeles' => '90001',
            'Houston' => '77001',
            'Chicago' => '60601',
            'Phoenix' => '85001',
            'Philadelphia' => '19019',
            'Miami' => '33101'
        ];
        $email_domains = ['gmail.com', 'yahoo.com', 'outlook.com', 'hotmail.com', 'icloud.com', 'protonmail.com', 'aol.com'];
        $first_names = ['james', 'luna', 'david', 'sarah', 'kevin', 'emma', 'michael', 'olivia', 'daniel', 'nora'];
        $last_names = ['lee', 'smith', 'johnson', 'brown', 'williams', 'miller', 'davis', 'clark', 'wilson', 'moore'];
        $used_emails = [];
        $product_ids = [596, 593, 589, 587, 579, 577, 287, 285, 88, 26];
        $total_orders = 0;

        foreach ($product_ids as $product_id) {
            $order_count = rand(5, 10);
            for ($i = 0; $i < $order_count && $total_orders < 100; $i++) {
                $quantity = rand(1, 2);

                // 生成随机邮箱
                do {
                    $first = $first_names[array_rand($first_names)];
                    $last = $last_names[array_rand($last_names)];
                    $number = rand(10, 9999);
                    $symbol = rand(0, 1) ? '.' : '_';
                    $domain = (rand(1, 100) <= 60) ? 'gmail.com' : $email_domains[array_rand($email_domains)];
                    $email = strtolower($first . $symbol . $last . $number . '@' . $domain);
                } while (in_array($email, $used_emails));
                $used_emails[] = $email;

                // 创建用户(注册会员)
                $user = get_user_by('email', $email);
                if (!$user) {
                    $user_id = wp_create_user($email, wp_generate_password(), $email);
                    wp_update_user(['ID' => $user_id, 'role' => 'customer']);
                } else {
                    $user_id = $user->ID;
                    $user->set_role('customer');
                }

                // 随机地址
                $city = array_rand($cities);
                $postcode = $cities[$city];
                $state_code = array_rand($states);
                $state = $states[$state_code];
                $phone = '1' . rand(200, 999) . rand(1000000, 9999999);
                $address = [
                    'first_name' => ucfirst($first),
                    'last_name' => ucfirst($last),
                    'email' => $email,
                    'phone' => $phone,
                    'address_1' => rand(100, 999) . " Main St",
                    'city' => $city,
                    'postcode' => $postcode,
                    'country' => 'US',
                    'state' => $state_code
                ];

                // 获取产品
                $product = wc_get_product($product_id);
                if (!$product) {
                    continue; // 如果产品不存在,跳过
                }

                // 创建订单
                $order = wc_create_order(['customer_id' => $user_id]);
                $order->add_product($product, $quantity);
                $order->set_address($address, 'billing');
                $order->set_address($address, 'shipping');
                $order->calculate_totals();
                $order->update_status('completed');

                // 设置下单时间(仅 7 月,随机每日、每时每分每秒)
                $day = rand(1, 31);
                $hour = rand(0, 23);
                $minute = rand(0, 59);
                $second = rand(0, 59);
                $order_date = "2025-07-" . str_pad($day, 2, '0', STR_PAD_LEFT) .
                             " " . str_pad($hour, 2, '0', STR_PAD_LEFT) . ":" .
                             str_pad($minute, 2, '0', STR_PAD_LEFT) . ":" .
                             str_pad($second, 2, '0', STR_PAD_LEFT);
                $order->set_date_created($order_date);
                $order->save();

                $total_orders++;
                if ($total_orders >= 100) break;
            }
            if ($total_orders >= 100) break;
        }

        wp_die('✅ 已成功创建 100 个 7 月订单!');
    }
}
https://charging-all.com/wp-admin/?generate_reviews=1
运行上面这个跑一遍评论生成
https://charging-all.com/wp-admin/?create_us_orders=1
上面这个是订单生成一遍

© 版权声明
THE END
喜欢就支持一下吧
点赞115 分享
评论 共19条

    暂无评论内容