/cocart-cart-callback-example

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

Primary LanguageJavaScript

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 v3.1 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.

// import CoCartAPI from "@cocart/cocart-rest-api"; // Supports ESM
const CoCartAPI = require("@cocart/cocart-rest-api").default;

const CoCart = new CoCartAPI({
  url: "https://example.com",
  version: "cocart/v2"
});

var data = {
  "namespace": "my-custom-callback",
  "data": {
    "points": "10",
  }
};

CoCart.post("cart/update", data);

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 Slack 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.

Credits

CoCart Cart Callback Example is developed and maintained by Sébastien Dumont.

Founder of CoCart - Sébastien Dumont.


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