Skip to main content

E-commerce AI Integration

1Setup
2Product Tools
3Support
4Analytics

🛒 Transform Your WooCommerce Store with AI

This guide shows you how to integrate MCP for WP with WooCommerce to automate product descriptions, enhance customer support, and optimize your sales process.


Step 1: Setup & Configuration

Install Required Plugins

bash
# Install WooCommerce if not already installed
wp plugin install woocommerce --activate

# Install MCP for WP
wp plugin install https://github.com/artificialpoets/mcp-wp-docs/archive/main.zip --activate

Configure AI Provider

php
// Add to wp-config.php
define('OPENAI_API_KEY', 'sk-your-openai-api-key-here');
define('ANTHROPIC_API_KEY', 'sk-ant-your-claude-api-key-here');

💡 Pro Tip: Use Claude for customer support and GPT-4 for content generation. Each AI excels at different tasks.


Step 2: Product Description Generator

Create the AI Tool

json
{
  "name": "Product Description Generator",
  "description": "Generate compelling product descriptions for WooCommerce products",
  "provider": "openai",
  "method": "chat/completions",
  "input_schema": {
    "type": "object",
    "properties": {
      "product_name": {
        "type": "string",
        "description": "Name of the product"
      },
      "product_features": {
        "type": "array",
        "items": {"type": "string"},
        "description": "List of product features"
      },
      "target_audience": {
        "type": "string",
        "description": "Target customer demographic"
      },
      "tone": {
        "type": "string",
        "enum": ["professional", "casual", "luxury", "technical"],
        "default": "professional"
      }
    },
    "required": ["product_name", "product_features"]
  },
  "prompt_template": "Write a compelling product description for {{product_name}} with the following features: {{product_features}}. Target audience: {{target_audience}}. Tone: {{tone}}. Make it SEO-friendly and conversion-optimized."
}

Automate Product Descriptions

php
// Add to functions.php
add_action('woocommerce_product_quick_edit_save', 'auto_generate_product_description', 10, 1);
add_action('woocommerce_product_bulk_edit_save', 'auto_generate_product_description', 10, 1);

function auto_generate_product_description($product_id) {
    $product = wc_get_product($product_id);
    
    // Skip if description already exists
    if (!empty($product->get_description())) {
        return;
    }
    
    // Extract product features from attributes
    $features = [];
    $attributes = $product->get_attributes();
    foreach ($attributes as $attribute) {
        if ($attribute->is_taxonomy()) {
            $terms = wp_get_post_terms($product_id, $attribute->get_name());
            foreach ($terms as $term) {
                $features[] = $term->name;
            }
        } else {
            $features = array_merge($features, $attribute->get_options());
        }
    }
    
    // Generate description
    $result = wp_mcp_execute_tool('product-description-generator', [
        'product_name' => $product->get_name(),
        'product_features' => $features,
        'target_audience' => 'online shoppers',
        'tone' => 'professional'
    ]);
    
    if ($result && !isset($result['error'])) {
        $description = $result['choices'][0]['message']['content'];
        $product->set_description($description);
        $product->save();
        
        // Log the generation
        error_log("Generated description for product {$product_id}: " . substr($description, 0, 100) . "...");
    }
}

Manual Generation Interface

php
// Add admin button for manual generation
add_action('woocommerce_product_options_general_product_data', 'add_ai_description_button');

function add_ai_description_button() {
    global $post;
    ?>
    <div class="options_group">
        <p class="form-field">
            <label for="generate_ai_description">AI Description</label>
            <button type="button" id="generate_ai_description" class="button">
                Generate AI Description
            </button>
            <span class="description">Click to generate a product description using AI</span>
        </p>
    </div>
    
    <script>
    jQuery('#generate_ai_description').on('click', function() {
        const button = jQuery(this);
        button.prop('disabled', true).text('Generating...');
        
        jQuery.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'generate_ai_description',
                product_id: <?php echo $post->ID; ?>,
                nonce: '<?php echo wp_create_nonce('generate_ai_description'); ?>'
            },
            success: function(response) {
                if (response.success) {
                    jQuery('#excerpt').val(response.data.description);
                    button.text('Generated!').addClass('button-primary');
                } else {
                    alert('Error: ' + response.data.message);
                    button.text('Generate AI Description');
                }
                button.prop('disabled', false);
            }
        });
    });
    </script>
    <?php
}

// AJAX handler
add_action('wp_ajax_generate_ai_description', 'handle_generate_ai_description');

function handle_generate_ai_description() {
    check_ajax_referer('generate_ai_description', 'nonce');
    
    $product_id = intval($_POST['product_id']);
    $product = wc_get_product($product_id);
    
    if (!$product) {
        wp_send_json_error(['message' => 'Product not found']);
    }
    
    // Generate description (same logic as above)
    $result = wp_mcp_execute_tool('product-description-generator', [
        'product_name' => $product->get_name(),
        'product_features' => ['feature1', 'feature2'], // Extract from product
        'target_audience' => 'online shoppers',
        'tone' => 'professional'
    ]);
    
    if ($result && !isset($result['error'])) {
        wp_send_json_success([
            'description' => $result['choices'][0]['message']['content']
        ]);
    } else {
        wp_send_json_error(['message' => 'Failed to generate description']);
    }
}

Step 3: Customer Support Assistant

Create Support AI Tool

json
{
  "name": "Customer Support Assistant",
  "description": "AI-powered customer support for WooCommerce",
  "provider": "anthropic",
  "method": "messages",
  "input_schema": {
    "type": "object",
    "properties": {
      "customer_message": {
        "type": "string",
        "description": "Customer's support request"
      },
      "order_info": {
        "type": "object",
        "properties": {
          "order_id": {"type": "string"},
          "order_status": {"type": "string"},
          "order_total": {"type": "string"}
        }
      },
      "product_info": {
        "type": "object",
        "properties": {
          "product_name": {"type": "string"},
          "product_price": {"type": "string"}
        }
      }
    },
    "required": ["customer_message"]
  },
  "prompt_template": "You are a helpful customer support representative for an e-commerce store. Respond to this customer inquiry professionally and helpfully. If you need order information, ask for the order number.\n\nCustomer: {{customer_message}}\nOrder Info: {{order_info}}\nProduct Info: {{product_info}}\n\nProvide a helpful response and suggest next steps if needed."
}

Integrate with Contact Forms

php
// Add AI support to Contact Form 7
add_action('wpcf7_before_send_mail', 'enhance_contact_form_with_ai');

function enhance_contact_form_with_ai($cf7) {
    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return;
    
    $data = $submission->get_posted_data();
    $message = $data['your-message'] ?? '';
    
    if (empty($message)) return;
    
    // Get customer info
    $customer_email = $data['your-email'] ?? '';
    $customer_name = $data['your-name'] ?? '';
    
    // Check if customer has orders
    $customer = get_user_by('email', $customer_email);
    $order_info = null;
    
    if ($customer) {
        $orders = wc_get_orders([
            'customer' => $customer->ID,
            'limit' => 1,
            'orderby' => 'date',
            'order' => 'DESC'
        ]);
        
        if (!empty($orders)) {
            $latest_order = $orders[0];
            $order_info = [
                'order_id' => $latest_order->get_order_number(),
                'order_status' => $latest_order->get_status(),
                'order_total' => $latest_order->get_total()
            ];
        }
    }
    
    // Generate AI response
    $ai_response = wp_mcp_execute_tool('customer-support-assistant', [
        'customer_message' => $message,
        'order_info' => $order_info,
        'product_info' => null
    ]);
    
    if ($ai_response && !isset($ai_response['error'])) {
        $response_text = $ai_response['content'][0]['text'];
        
        // Add AI response to email
        $mail = $cf7->prop('mail');
        $mail['body'] .= "\n\n--- AI Support Response ---\n" . $response_text;
        $cf7->set_properties(['mail' => $mail]);
        
        // Send immediate auto-reply
        wp_mail(
            $customer_email,
            'We received your message - ' . get_bloginfo('name'),
            $response_text,
            ['Content-Type: text/html; charset=UTF-8']
        );
    }
}

Live Chat Integration

php
// Add AI to live chat (example with custom chat system)
add_action('wp_ajax_ai_chat_response', 'handle_ai_chat_response');

function handle_ai_chat_response() {
    check_ajax_referer('ai_chat_nonce', 'nonce');
    
    $message = sanitize_text_field($_POST['message']);
    $session_id = sanitize_text_field($_POST['session_id']);
    
    // Get chat history
    $chat_history = get_transient('chat_history_' . $session_id) ?: [];
    
    // Generate AI response
    $response = wp_mcp_execute_tool('customer-support-assistant', [
        'customer_message' => $message,
        'chat_history' => $chat_history
    ]);
    
    if ($response && !isset($response['error'])) {
        $ai_response = $response['content'][0]['text'];
        
        // Store in chat history
        $chat_history[] = [
            'user' => $message,
            'ai' => $ai_response,
            'timestamp' => current_time('timestamp')
        ];
        
        // Keep only last 10 messages
        if (count($chat_history) > 10) {
            $chat_history = array_slice($chat_history, -10);
        }
        
        set_transient('chat_history_' . $session_id, $chat_history, HOUR_IN_SECONDS);
        
        wp_send_json_success([
            'response' => $ai_response,
            'timestamp' => current_time('mysql')
        ]);
    } else {
        wp_send_json_error(['message' => 'Unable to generate response']);
    }
}

Step 4: Sales Analytics & Optimization

Product Performance Analyzer

json
{
  "name": "Product Performance Analyzer",
  "description": "Analyze product performance and suggest optimizations",
  "provider": "openai",
  "method": "chat/completions",
  "input_schema": {
    "type": "object",
    "properties": {
      "product_data": {
        "type": "object",
        "properties": {
          "sales_count": {"type": "integer"},
          "revenue": {"type": "number"},
          "views": {"type": "integer"},
          "conversion_rate": {"type": "number"},
          "reviews": {"type": "array"}
        }
      },
      "competitor_data": {
        "type": "object",
        "description": "Competitor pricing and features"
      }
    }
  },
  "prompt_template": "Analyze this product performance data and provide actionable insights for improvement:\n\nProduct Data: {{product_data}}\nCompetitor Data: {{competitor_data}}\n\nProvide specific recommendations for pricing, marketing, and product improvements."
}

Automated Performance Reports

php
// Generate weekly performance reports
add_action('wp_schedule_event', 'schedule_ai_performance_reports');

function schedule_ai_performance_reports() {
    if (!wp_next_scheduled('ai_performance_report')) {
        wp_schedule_event(time(), 'weekly', 'ai_performance_report');
    }
}

add_action('ai_performance_report', 'generate_ai_performance_report');

function generate_ai_performance_report() {
    // Get top products
    $products = wc_get_products([
        'limit' => 10,
        'orderby' => 'total_sales',
        'order' => 'DESC'
    ]);
    
    $report_data = [];
    
    foreach ($products as $product) {
        $product_data = [
            'name' => $product->get_name(),
            'sales_count' => $product->get_total_sales(),
            'revenue' => $product->get_total_sales() * $product->get_price(),
            'views' => get_post_meta($product->get_id(), '_product_views', true) ?: 0,
            'conversion_rate' => calculate_conversion_rate($product),
            'reviews' => get_product_reviews($product->get_id())
        ];
        
        // Get AI analysis
        $analysis = wp_mcp_execute_tool('product-performance-analyzer', [
            'product_data' => $product_data,
            'competitor_data' => get_competitor_data($product)
        ]);
        
        if ($analysis && !isset($analysis['error'])) {
            $report_data[] = [
                'product' => $product_data,
                'ai_insights' => $analysis['choices'][0]['message']['content']
            ];
        }
    }
    
    // Send report to admin
    $report_html = generate_report_html($report_data);
    wp_mail(
        get_option('admin_email'),
        'Weekly AI Performance Report - ' . get_bloginfo('name'),
        $report_html,
        ['Content-Type: text/html; charset=UTF-8']
    );
}

function calculate_conversion_rate($product) {
    $views = get_post_meta($product->get_id(), '_product_views', true) ?: 1;
    $sales = $product->get_total_sales();
    return ($sales / $views) * 100;
}

function get_product_reviews($product_id) {
    $reviews = get_comments([
        'post_id' => $product_id,
        'status' => 'approve',
        'number' => 5
    ]);
    
    return array_map(function($review) {
        return [
            'rating' => get_comment_meta($review->comment_ID, 'rating', true),
            'comment' => $review->comment_content
        ];
    }, $reviews);
}

function generate_report_html($report_data) {
    $html = '<h2>Weekly AI Performance Report</h2>';
    
    foreach ($report_data as $item) {
        $html .= '<h3>' . $item['product']['name'] . '</h3>';
        $html .= '<p><strong>Sales:</strong> ' . $item['product']['sales_count'] . '</p>';
        $html .= '<p><strong>Revenue:</strong> $' . number_format($item['product']['revenue'], 2) . '</p>';
        $html .= '<p><strong>AI Insights:</strong></p>';
        $html .= '<p>' . nl2br($item['ai_insights']) . '</p>';
        $html .= '<hr>';
    }
    
    return $html;
}

🎯 Advanced Features

Dynamic Pricing Optimization

php
// AI-powered dynamic pricing
add_filter('woocommerce_get_price', 'ai_dynamic_pricing', 10, 2);

function ai_dynamic_pricing($price, $product) {
    // Only apply to specific products
    if (!in_array($product->get_id(), get_option('ai_pricing_products', []))) {
        return $price;
    }
    
    $pricing_data = [
        'current_price' => $price,
        'demand_level' => get_demand_level($product),
        'competitor_prices' => get_competitor_prices($product),
        'inventory_level' => $product->get_stock_quantity(),
        'seasonal_factors' => get_seasonal_factors()
    ];
    
    $ai_pricing = wp_mcp_execute_tool('dynamic-pricing-optimizer', $pricing_data);
    
    if ($ai_pricing && !isset($ai_pricing['error'])) {
        $suggested_price = json_decode($ai_pricing['choices'][0]['message']['content'], true);
        return $suggested_price['recommended_price'] ?? $price;
    }
    
    return $price;
}

Personalized Product Recommendations

php
// AI-powered product recommendations
add_action('woocommerce_after_single_product', 'show_ai_recommendations');

function show_ai_recommendations() {
    global $product;
    
    $user_id = get_current_user_id();
    $user_data = [
        'purchase_history' => get_user_purchase_history($user_id),
        'browsing_history' => get_user_browsing_history($user_id),
        'current_product' => [
            'id' => $product->get_id(),
            'category' => $product->get_categories(),
            'price' => $product->get_price()
        ]
    ];
    
    $recommendations = wp_mcp_execute_tool('product-recommendations', $user_data);
    
    if ($recommendations && !isset($recommendations['error'])) {
        $recommended_products = json_decode($recommendations['choices'][0]['message']['content'], true);
        
        if (!empty($recommended_products)) {
            echo '<div class="ai-recommendations">';
            echo '<h3>Recommended for You</h3>';
            echo '<div class="recommendations-grid">';
            
            foreach ($recommended_products as $rec_product_id) {
                $rec_product = wc_get_product($rec_product_id);
                if ($rec_product) {
                    echo '<div class="recommendation-item">';
                    echo '<a href="' . $rec_product->get_permalink() . '">';
                    echo $rec_product->get_image();
                    echo '<h4>' . $rec_product->get_name() . '</h4>';
                    echo '<span class="price">' . $rec_product->get_price_html() . '</span>';
                    echo '</a>';
                    echo '</div>';
                }
            }
            
            echo '</div></div>';
        }
    }
}

📊 Monitoring & Analytics

Track AI Performance

php
// Track AI tool performance
add_action('wp_mcp_tool_executed', 'track_ai_performance', 10, 3);

function track_ai_performance($tool_name, $input, $output) {
    $performance_data = [
        'tool' => $tool_name,
        'input_size' => strlen(json_encode($input)),
        'output_size' => strlen(json_encode($output)),
        'execution_time' => microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],
        'success' => !isset($output['error']),
        'timestamp' => current_time('mysql')
    ];
    
    // Store in database
    global $wpdb;
    $wpdb->insert(
        $wpdb->prefix . 'mcp_performance_logs',
        $performance_data
    );
    
    // Alert on high error rates
    $error_rate = get_ai_error_rate($tool_name);
    if ($error_rate > 0.1) { // 10% error rate
        wp_mail(
            get_option('admin_email'),
            'High AI Error Rate Alert',
            "Tool: $tool_name\nError Rate: " . ($error_rate * 100) . "%"
        );
    }
}

function get_ai_error_rate($tool_name) {
    global $wpdb;
    
    $total = $wpdb->get_var($wpdb->prepare(
        "SELECT COUNT(*) FROM {$wpdb->prefix}mcp_performance_logs 
         WHERE tool = %s AND timestamp > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
        $tool_name
    ));
    
    $errors = $wpdb->get_var($wpdb->prepare(
        "SELECT COUNT(*) FROM {$wpdb->prefix}mcp_performance_logs 
         WHERE tool = %s AND success = 0 AND timestamp > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
        $tool_name
    ));
    
    return $total > 0 ? $errors / $total : 0;
}

🎉 Your E-commerce AI Integration is Complete! You now have automated product descriptions, AI-powered customer support, and intelligent sales optimization working together to boost your store's performance.

🚀 Next Steps