Skip to main content
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 to add unique features to your cart.

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 as a practical example. We’ll break this down into manageable steps:

Step 1: Basic Class Structure

First, create the basic callback class structure:
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 and Safety Checks

Add the main callback method with proper validation:
Input Validation

Step 3: Implementing Your Custom Logic

Add your specific functionality within the callback:
Custom Logic Implementation

Step 4: Finalize and Calculate Totals

Complete the callback with proper totals calculation:
Finalize Callback

Complete Callback Class

Here’s the complete loyalty points callback:

Registering Your Callback

Once you’ve created your callback class, you need to register it with CoCart. This tells CoCart that your callback is available for use.

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 the callback using the CoCart action hook
Registration Code

Where to Place Registration Code

You can place this registration code in:
  • Your theme’s functions.php file
  • A custom plugin file
  • Your site’s wp-config.php file (not recommended)
If you place the code in your theme’s functions.php, remember that changing themes will disable your callback.

Using Your Callback

Once registered, you can call your callback via the CoCart update endpoint. Here’s how to use our loyalty points example:

API Request Structure

Basic Usage

JavaScript Usage

Frontend Integration

Response Examples

Success Response:
Success
Error Response:
Error - Insufficient Points

Additional Callback Examples

Here are some other practical callback ideas you can implement:

Gift Card Redemption

Gift Card Example

Shipping Insurance

Shipping Insurance Example

Custom Product Bundling

Product Bundle Example

Best Practices

  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. Recalculate totals - Always call $this->recalculate_totals() after making changes
  5. Provide clear feedback - Use WooCommerce notices to inform users of what happened
  6. Test thoroughly - Test all success and error scenarios

Troubleshooting

Common Issues

  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
  4. Totals not updating: Make sure you’re calling recalculate_totals()

Debug Mode

Enable debug mode to see detailed error messages:
Debug Mode