> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pureclarity.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Extending WooCommerce Data Feeds

> Developer guide for customizing and extending PureClarity data feeds in WooCommerce with hooks, filters, and custom data

<Note>
  This guide is for developers who want to customize data feeds. Basic PHP and WordPress development knowledge is required. Always test customizations in a staging environment first.
</Note>

## Overview

The PureClarity WooCommerce plugin provides several hooks and filters that allow developers to customize the data that is sent to PureClarity. This enables advanced customizations for specific business requirements.

<Warning>
  Feed modifications can affect recommendation quality. Test thoroughly and monitor results when implementing custom feed logic.
</Warning>

## Available Hooks and Filters

### Product Feed Customization

**Product Data Filter**

```php theme={null}
// Modify product data before sending to PureClarity
add_filter('pureclarity_product_data', 'custom_product_data', 10, 2);

function custom_product_data($product_data, $product_id) {
    // Add custom fields
    $product_data['custom_field'] = get_post_meta($product_id, 'custom_meta_key', true);
    
    // Modify existing fields
    $product_data['description'] = strip_tags($product_data['description']);
    
    // Add computed values
    $product_data['profit_margin'] = calculate_profit_margin($product_id);
    
    return $product_data;
}
```

**Product Exclusion Filter**

```php theme={null}
// Exclude specific products from feeds
add_filter('pureclarity_exclude_product', 'custom_product_exclusion', 10, 2);

function custom_product_exclusion($exclude, $product_id) {
    $product = wc_get_product($product_id);
    
    // Exclude products with specific attributes
    if ($product->get_attribute('exclude_from_recommendations')) {
        return true;
    }
    
    // Exclude based on custom business logic
    if (custom_business_logic($product)) {
        return true;
    }
    
    return $exclude;
}
```

### Category Feed Customization

**Category Data Filter**

```php theme={null}
// Modify category data before sending
add_filter('pureclarity_category_data', 'custom_category_data', 10, 2);

function custom_category_data($category_data, $category_id) {
    // Add custom category metadata
    $category_data['custom_sort_order'] = get_term_meta($category_id, 'sort_order', true);
    
    // Add SEO information
    $category_data['seo_title'] = get_term_meta($category_id, 'seo_title', true);
    
    return $category_data;
}
```

### User Feed Customization

**User Data Filter**

```php theme={null}
// Extend user data with custom information
add_filter('pureclarity_user_data', 'custom_user_data', 10, 2);

function custom_user_data($user_data, $user_id) {
    // Add customer segment information
    $user_data['customer_segment'] = get_user_meta($user_id, 'customer_segment', true);
    
    // Add loyalty program data
    $user_data['loyalty_points'] = get_user_meta($user_id, 'loyalty_points', true);
    
    // Add purchase history summary
    $user_data['total_orders'] = count_user_orders($user_id);
    $user_data['average_order_value'] = calculate_average_order_value($user_id);
    
    return $user_data;
}
```

<Info>
  Custom user data helps PureClarity create more accurate customer segments and personalized recommendations.
</Info>

## Advanced Customizations

### Custom Product Attributes

**Adding Third-Party Plugin Data**

```php theme={null}
// Integrate with Advanced Custom Fields (ACF)
add_filter('pureclarity_product_data', 'add_acf_fields', 10, 2);

function add_acf_fields($product_data, $product_id) {
    if (function_exists('get_field')) {
        $product_data['brand'] = get_field('product_brand', $product_id);
        $product_data['material'] = get_field('product_material', $product_id);
        $product_data['size_guide'] = get_field('size_guide_url', $product_id);
    }
    
    return $product_data;
}
```

**Dynamic Pricing Integration**

```php theme={null}
// Include dynamic pricing information
add_filter('pureclarity_product_data', 'add_dynamic_pricing', 10, 2);

function add_dynamic_pricing($product_data, $product_id) {
    $product = wc_get_product($product_id);
    
    // Get role-based pricing
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;
    
    if (in_array('wholesale', $user_roles)) {
        $product_data['wholesale_price'] = get_wholesale_price($product_id);
    }
    
    // Include bulk pricing tiers
    $product_data['bulk_pricing'] = get_bulk_pricing_tiers($product_id);
    
    return $product_data;
}
```

### Custom Business Logic

**Multi-Language Support**

```php theme={null}
// Add WPML/Polylang language data
add_filter('pureclarity_product_data', 'add_language_data', 10, 2);

function add_language_data($product_data, $product_id) {
    // WPML integration
    if (function_exists('icl_get_languages')) {
        $product_data['language'] = ICL_LANGUAGE_CODE;
        $product_data['translations'] = get_product_translations($product_id);
    }
    
    return $product_data;
}
```

**Inventory Management Integration**

```php theme={null}
// Advanced inventory tracking
add_filter('pureclarity_product_data', 'add_inventory_data', 10, 2);

function add_inventory_data($product_data, $product_id) {
    $product = wc_get_product($product_id);
    
    // Add detailed stock information
    $product_data['stock_status'] = $product->get_stock_status();
    $product_data['stock_quantity'] = $product->get_stock_quantity();
    $product_data['backorders_allowed'] = $product->get_backorders();
    
    // Add restock date if available
    $restock_date = get_post_meta($product_id, 'restock_date', true);
    if ($restock_date) {
        $product_data['restock_date'] = $restock_date;
    }
    
    return $product_data;
}
```

<Tip>
  Include inventory data to enable PureClarity to avoid recommending out-of-stock products or prioritize items with higher availability.
</Tip>

## Feed Processing Hooks

### Pre-Processing Hooks

**Before Feed Generation**

```php theme={null}
// Perform actions before feed processing starts
add_action('pureclarity_before_feed_generation', 'before_feed_processing');

function before_feed_processing($feed_type) {
    // Log feed start
    error_log("Starting {$feed_type} feed generation at " . current_time('mysql'));
    
    // Prepare data if needed
    if ($feed_type === 'product') {
        update_custom_product_cache();
    }
    
    // Send notification
    wp_mail('admin@example.com', 'Feed Started', "Starting {$feed_type} feed");
}
```

### Post-Processing Hooks

**After Feed Completion**

```php theme={null}
// Actions after feed processing completes
add_action('pureclarity_after_feed_generation', 'after_feed_processing', 10, 2);

function after_feed_processing($feed_type, $result) {
    // Log completion
    $status = $result ? 'successful' : 'failed';
    error_log("Feed {$feed_type} completed with status: {$status}");
    
    // Update analytics
    update_feed_analytics($feed_type, $result);
    
    // Trigger related processes
    if ($feed_type === 'product' && $result) {
        trigger_search_index_update();
    }
}
```

## Error Handling and Validation

### Custom Validation

**Product Data Validation**

```php theme={null}
// Validate product data before sending
add_filter('pureclarity_validate_product_data', 'validate_product_data', 10, 2);

function validate_product_data($is_valid, $product_data) {
    // Required fields validation
    $required_fields = ['id', 'title', 'price', 'image'];
    
    foreach ($required_fields as $field) {
        if (empty($product_data[$field])) {
            error_log("Product {$product_data['id']} missing required field: {$field}");
            return false;
        }
    }
    
    // Custom business rules
    if ($product_data['price'] <= 0) {
        error_log("Product {$product_data['id']} has invalid price: {$product_data['price']}");
        return false;
    }
    
    return $is_valid;
}
```

### Error Logging

**Custom Error Handling**

```php theme={null}
// Enhanced error logging
add_action('pureclarity_feed_error', 'custom_error_handling', 10, 3);

function custom_error_handling($error_message, $feed_type, $context) {
    // Log to custom file
    $log_file = WP_CONTENT_DIR . '/pureclarity-errors.log';
    $timestamp = current_time('Y-m-d H:i:s');
    $log_entry = "[{$timestamp}] {$feed_type}: {$error_message} - Context: " . json_encode($context) . "\n";
    
    file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX);
    
    // Send critical error notifications
    if (strpos($error_message, 'critical') !== false) {
        wp_mail('admin@example.com', 'Critical PureClarity Error', $error_message);
    }
}
```

## Performance Optimization

### Batch Processing Customization

```php theme={null}
// Customize batch processing
add_filter('pureclarity_feed_batch_size', 'custom_batch_size', 10, 2);

function custom_batch_size($batch_size, $feed_type) {
    // Adjust batch size based on server resources
    if ($feed_type === 'product') {
        // Smaller batches for product feeds with many custom fields
        return 50;
    }
    
    return $batch_size;
}
```

### Caching Integration

```php theme={null}
// Implement custom caching for expensive operations
add_filter('pureclarity_product_data', 'cached_product_data', 10, 2);

function cached_product_data($product_data, $product_id) {
    $cache_key = "pureclarity_product_data_{$product_id}";
    $cached_data = wp_cache_get($cache_key);
    
    if ($cached_data !== false) {
        return array_merge($product_data, $cached_data);
    }
    
    // Expensive computation
    $custom_data = perform_expensive_calculation($product_id);
    
    // Cache for 1 hour
    wp_cache_set($cache_key, $custom_data, '', 3600);
    
    return array_merge($product_data, $custom_data);
}
```

<Warning>
  Be cautious with caching duration. Product data that changes frequently should have shorter cache times to ensure accuracy.
</Warning>

## Testing and Debugging

### Debug Output

```php theme={null}
// Add debug information to feeds
add_action('pureclarity_debug_product_data', 'debug_product_data', 10, 2);

function debug_product_data($product_data, $product_id) {
    if (defined('WP_DEBUG') && WP_DEBUG) {
        error_log("Product {$product_id} data: " . json_encode($product_data));
    }
}
```

### Test Mode Enhancements

```php theme={null}
// Special handling for test environments
add_filter('pureclarity_product_data', 'test_mode_modifications', 10, 2);

function test_mode_modifications($product_data, $product_id) {
    if (wp_get_environment_type() === 'development') {
        // Add test identifiers
        $product_data['test_mode'] = true;
        $product_data['debug_id'] = "test_{$product_id}";
    }
    
    return $product_data;
}
```

## Best Practices

### Code Organization

**Create a Custom Plugin**

```php theme={null}
<?php
/**
 * Plugin Name: PureClarity Custom Extensions
 * Description: Custom modifications for PureClarity WooCommerce integration
 * Version: 1.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class PureClarityCustomExtensions {
    public function __construct() {
        add_action('init', array($this, 'init'));
    }
    
    public function init() {
        // Initialize customizations
        $this->add_product_filters();
        $this->add_category_filters();
        $this->add_user_filters();
    }
    
    private function add_product_filters() {
        add_filter('pureclarity_product_data', array($this, 'modify_product_data'), 10, 2);
    }
    
    // Additional methods...
}

new PureClarityCustomExtensions();
```

### Documentation

**Comment Your Code**

```php theme={null}
/**
 * Add custom brand information to product feed
 * 
 * @param array $product_data Existing product data
 * @param int   $product_id   WooCommerce product ID
 * @return array Modified product data with brand information
 */
function add_brand_data($product_data, $product_id) {
    // Implementation here
}
```

## Related Resources

* [WooCommerce Installation Guide](/integrations/woocommerce/installation)
* [Data Feeds Management](/integrations/woocommerce/data-feeds)
* [Settings Configuration](/integrations/woocommerce/settings)
* [Magento Feed Extensions](/integrations/magento/magento-2/extending-feed)
* [Troubleshooting Guide](/integrations/woocommerce/troubleshooting)
