Skip to main content
This comprehensive guide covers both CoCart v4 (stable) and CoCart v5+ (pre-release) callback systems. Choose the version that matches your CoCart installation.
Cart callbacks allow you to extend CoCart’s functionality by creating custom cart update operations. This tutorial shows you how to create, register, and use custom callbacks for both CoCart versions.

Version Differences Overview

  • 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:
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:
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

  1. Save your callback class to a PHP file (e.g., loyalty-points-callback.php)
  2. Include the file in your theme or plugin
  3. Register using the appropriate method for your CoCart version
v4 Registration Code

Migration from v4 to v5+

If you’re upgrading from CoCart v4, here’s what changes:
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.
What breaks:
  • Registration method (action hook vs filter)
  • Base class name change
  • Totals calculation method
What stays the same:
  • API endpoint usage
  • Callback $name property
  • Error handling patterns
  • Frontend integration code

Best Practices

General Best Practices (Both Versions)

  1. Always validate input data - Never trust user input
  2. Use proper error handling - Throw CoCart_Data_Exception for consistent error responses
  3. Check authentication when needed - Verify user permissions for sensitive operations
  4. Provide clear feedback - Use WooCommerce notices to inform users of what happened
  5. Test thoroughly - Test all success and error scenarios

Version-Specific Best Practices

  • 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)

  1. Callback not found: Check that your namespace matches the $name property
  2. Class not loaded: Ensure your include path is correct
  3. Authentication errors: Verify user login status for user-specific callbacks

Version-Specific Issues

  • 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

Debug v4 Callback Registration

Version Compatibility Summary

Choose the version that matches your CoCart installation and development needs.