This comprehensive guide covers both CoCart v4 (stable) and CoCart v5+ (pre-release) callback systems. Choose the version that matches your CoCart installation.
Version Differences Overview
- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
- Base class:
CoCart_Cart_Extension_Callback
- Registration:
cocart_register_extension_callback
action hook - Totals calculation:
$this->recalculate_totals()
- Status: Production ready
What are Cart Callbacks?
Cart callbacks are custom PHP classes that execute when the cart is updated via the/wp-json/cocart/v2/cart/update
endpoint. They allow you to:
- Apply discounts or loyalty points
- Update cart metadata
- Integrate with third-party services
- Implement custom cart validation rules
- Add special promotions or fees
Creating a Custom Callback
Let’s create a loyalty points callback that works with both CoCart versions:Step 1: Basic Class Structure
The base class differs between versions:- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
v4 Basic Callback Structure
The
$name
property is crucial - it’s used as the namespace
parameter when calling your callback via the API.Step 2: Input Validation
The validation logic remains the same across both versions:Input Validation
Step 3: Custom Logic Implementation
The business logic implementation is identical across both versions:Custom Logic Implementation
Step 4: Totals Calculation
The totals calculation method differs between versions:- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
v4 Finalize Callback
Registering Your Callback
The registration method differs between CoCart versions:If you registered a callback before with version 4 or lower of CoCart, you will need to register them again for v5+. We simplified the callback system to perform better in version 5.
Registration Steps
- Save your callback class to a PHP file (e.g.,
loyalty-points-callback.php
) - Include the file in your theme or plugin
- Register using the appropriate method for your CoCart version
- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
v4 Registration Code
Migration from v4 to v5+
If you’re upgrading from CoCart v4, here’s what changes:- v4 Registration (Old)
- v5+ Registration (New)
v4 Method
Multiple Callbacks Registration
You can register multiple callbacks in one function:Multiple Callbacks
Using Your Callback
The API usage remains identical across both versions - only the backend registration method differs.API Request Structure
API Usage (Both Versions)
JavaScript Usage
Frontend Integration (Both Versions)
v5+ Performance Improvements
The new callback system offers several advantages:Improved Performance
- Reduced overhead: Filter-based registration is more efficient
- Better memory usage: Callbacks are loaded only when needed
- Faster initialization: Streamlined callback discovery
Enhanced Developer Experience
- Simpler registration: No need for complex callback objects
- Better debugging: Clearer callback flow
- Easier testing: Direct instantiation for unit tests
Backward Compatibility Considerations
v5+ callbacks are not backward compatible with v4. You must update your registration code when upgrading.
- Registration method (action hook vs filter)
- Base class name change
- Totals calculation method
- API endpoint usage
- Callback
$name
property - Error handling patterns
- Frontend integration code
Best Practices
General Best Practices (Both Versions)
- Always validate input data - Never trust user input
- Use proper error handling - Throw CoCart_Data_Exception for consistent error responses
- Check authentication when needed - Verify user permissions for sensitive operations
- Provide clear feedback - Use WooCommerce notices to inform users of what happened
- Test thoroughly - Test all success and error scenarios
Version-Specific Best Practices
- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
- Use action hook registration -
cocart_register_extension_callback
- Extend correct base class -
CoCart_Cart_Extension_Callback
- Use class totals method - Call
$this->recalculate_totals()
- Follow established patterns - Production-tested approach
Troubleshooting
Common Issues (Both Versions)
- Callback not found: Check that your namespace matches the
$name
property - Class not loaded: Ensure your include path is correct
- Authentication errors: Verify user login status for user-specific callbacks
Version-Specific Issues
- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
- Registration not working: Check
cocart_register_extension_callback
action - Totals not updating: Ensure you’re calling
$this->recalculate_totals()
- Base class error: Verify extending
CoCart_Cart_Extension_Callback
Debug Mode
Enable debug mode to see detailed error messages:Debug Mode
Debug Callback Registration
- CoCart v4 (Stable)
- CoCart v5+ (Pre-release)
Debug v4 Callback Registration
Version Compatibility Summary
Feature | CoCart v4 (Stable) | CoCart v5+ (Pre-release) |
---|---|---|
Base Class | CoCart_Cart_Extension_Callback | CoCart_REST_Callback |
Registration | Action hook | Filter |
Hook Name | cocart_register_extension_callback | cocart_rest_callbacks |
Registration Method | $callback->register() | $callbacks[] = new Class() |
Totals Calculation | $this->recalculate_totals() | $controller->calculate_totals() |
Performance | Standard | Improved |
API Usage | Same | Same |
Backward Compatible | Yes (with older versions) | No (requires migration) |
Production Ready | ✅ Yes | ⚠️ Testing only |