Skip to content

An example of registering a callback that can be triggered when updating the cart with CoCart.

Notifications You must be signed in to change notification settings

cocart-headless/cocart-cart-callback-example

Repository files navigation

CoCart - Cart Callback Example

CoCart


This is a Cart Callback Example plugin for CoCart. It is setup so that it can be installed standalone as a WordPress plugin.

It provides you an example of registering a callback that can be triggered when updating the cart with CoCart.

Callback can only be triggered if CoCart v4.0 is installed.

Here's some more details on the configuration options:

$name (required)

This is the namespace string that is used to identifier the callback when called via the cart update API.

callback() (required)

The callback function is where you will place your code to do what you need to the cart before it returns the updated cart response.

If the callback function is missing an error will return when called.

Example of calling the callback.

This example shows we are triggering a callback to use 10 points to save on the cart total.

curl --location --request POST 'https://example.com/wp-json/cocart/v2/cart/update' \
--header 'Content-Type: application/json' \
--data '{
  "namespace": "my-custom-callback",
  "data": {
    "points": "10",
  }
}'

Register Callback

To register a callback, simply load and hook the callback like so.

add_action( 'cocart_register_extension_callback', 'register_extension_callback_test' );

function register_extension_callback_test( $callback ) {
  include_once( dirname( __FILE__) . '/callback/test.php' );

  $callback->register( new CoCart_Callback_Test() );
}

Main directories and files

  • cocart-cart-callback-example.php - The main plugin file. ONLY used when using this package as a plugin!

  • includes/class-cocart-cart-callback-example.php

  • includes/callback/test.php

    • init - Init your package. If it needs to hook into WordPress, do so here.

License

Released under GNU General Public License v3.0.


CoCart Channels

We have different channels at your disposal where you can find information about the CoCart project, discuss it and get involved:

Twitter: cocartapi CoCart GitHub Stars

  • 📖 Docs: this is the place to learn how to use CoCart API. Get started!
  • 🧰 Resources: this is the hub of all CoCart resources to help you build a headless store. Get resources!
  • 👪 Community: use our Discord chat room to share any doubts, feedback and meet great people. This is your place too to share how are you planning to use CoCart!
  • 🐞 GitHub: we use GitHub for bugs and pull requests, doubts are solved with the community.
  • 🐦 Social media: a more informal place to interact with CoCart users, reach out to us on Twitter.
  • 💌 Newsletter: do you want to receive the latest plugin updates and news? Subscribe here.

Get involved

Do you like the idea of creating a headless store with WooCommerce? Got questions or feedback? We'd love to hear from you. Come join our community! ❤️


Credits

Website cocartapi.com  ·  GitHub @co-cart  ·  Twitter @cocartapi


CoCart is developed and maintained by Sébastien Dumont. Founder of CoCart Headless, LLC.

Website sebastiendumont.com  ·  GitHub @seb86  ·  Twitter @sebd86

About

An example of registering a callback that can be triggered when updating the cart with CoCart.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •