WooCommerce用ChatGPT做你的專屬客服

自從Ai普及化後,有些功能在開發及使用真的變快許多,就連不會程式的麻瓜也能很快的透過ChatGPT開發出有模有樣的功能,下面是簡單的土炮分享,歡迎交流使用

ChatGPT客服程式碼

function get_woocommerce_product_details() {
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1
    );
    $products = get_posts($args);
    $product_details = array();

    foreach ($products as $product) {
        $product_id = $product->ID;
        $product_title = $product->post_title;
        $product_price = get_post_meta($product_id, '_price', true);
        $product_details[] = $product_title . ' - $' . $product_price;
    }

    // 添加額外的購物信息
    $additional_info = "可以填一些資訊,例如你的店在哪裡,如何付款,ATM帳號之類的";

    $product_details[] = $additional_info;

    return $product_details;
}

function chatgpt_response($input_text) {
    $api_key = 'api-key-here';
    $url = 'https://api.openai.com/v1/chat/completions';

    // 獲取 WooCommerce 商品詳細信息
    $product_details = get_woocommerce_product_details();
    $product_details_text = "Available products:\n" . implode("\n", $product_details);

    // 組合用戶輸入和商品詳細信息
    $prompt = $product_details_text . "\n\nUser input: " . $input_text;

    $data = array(
        'model' => 'gpt-3.5-turbo',
        'messages' => array(
            array('role' => 'user', 'content' => $prompt)
        ),
        'max_tokens' => 150
    );
    $data_string = json_encode($data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key
    ));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        return 'Error: Could not contact OpenAI API';
    }
    $response = json_decode($result, true);
    curl_close($ch);

    if (isset($response['choices'][0]['message']['content'])) {
        return $response['choices'][0]['message']['content'];
    } else {
        return 'Error: Unexpected API response: ' . json_encode($response);
    }
}

function display_chatgpt_form() {
    if (isset($_POST['user_input'])) {
        $response = chatgpt_response($_POST['user_input']);
        echo '<div style="text-align: center; margin-top: 20px;">' . esc_html($response) . '</div>';
    }
    echo '<div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
            <form method="post" style="margin: 20px; padding: 10px; border: 1px solid #ddd; text-align: center;">
                <input type="text" name="user_input" placeholder="Ask me anything" style="width: 100%; padding: 8px; margin-bottom: 10px;">
                <input type="submit" value="Submit" style="padding: 10px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">
            </form>
          </div>';
}
add_shortcode('chatgpt', 'display_chatgpt_form');

ChatGPT客服shortcode

[chatgpt]

ChatGPT客服如何運作

測試時有點嚇到,他並不是隨便回答,是根據你站內的產品資訊回覆。例如我問他5000元預算要吃火鍋,ChatGPT會回覆,有一個超過預算一點點5150元,但內容很讚大推之類的,非常有趣,比LINE的聊天機器人還強。
另外,還有一個案例,我問他我住在台南,你們有門市在台南嗎,他居然會回覆我這裡是電商,網購很方便

其他分享

  • 很貴,我一開始用ChatGPT4o 問沒幾次就噴了5美金,改成gpt-3.5-turbo會較便宜,但相對回覆的結果也會較陽春
  • 速度慢,因為我的作法不是包在模型,是由他去讀我站內的資料,程式碼的案例就是去讀商品名稱,簡介,金額以及一些交易的基本資料
  • 回覆內容如果要多一點,可改 'max_tokens' => 150 (費用相對也會較高唷)
瀏覽次數:10