Reading list Switch to dark mode

    How to Use Cs-Cart Hooks (Part -1)

    Updated 28 December 2022

    In this Blog, we will learn about Cs-Cart Hooks in the coding scenario and their types.

    What is hooking?

    ‘Hooking’ is a programming concept that is used to control flow. Hook are the entry points in the code for external code and for modifications. We can use hook in Cs-cart to manipulate the data or get the data in between the flow of the code.
    There is also a Cs-Cart hook explanation.

    The hook is very important in creating add-ons in CS-Cart.

    Cs-Cart Hooks Types

    There are two types of hooks used in Cs-Cart.

    • PHP Hooks
    • TPL Hooks

    PHP Hooks

    PHP hook are specifically specified in the core code of the Cs-Cart. We can use these hooks by calling a specified function of the hook in the addon.

    Start your headless eCommerce
    now.
    Find out More
    image-24
    CsCart hook setup

    How to use Hooks in Cs-Cart

    There are two ways to set hooks in the add-on:-

    1. Using addon Init and func file
    2. Using addon XML scheme 4.0 and Bootstrap.

    Using the addon Init and func file

    Firstly, we have to register the hook which we are going to use in ‘init.php’ by using ‘fn_registers_hooks’.

    image-25
    Register hook

    Now Hook is registered, you can use it by creating a hook function in the addon. We have to create a specified function in the addon ‘func.php’ file. The hook Function name should be specified in this way: –

    fn_addon_name_hook_name(arguments) {

    //Your code

    }

    Example

    blog_1
    Created a change_order_status hook function
    blog2
    Changing Order status
    blog_33
    Result of change_order_status hook

    Using addon XML scheme 4.0 and Bootstrap

    The OOPS concept is needed for using the hook in this way.
    In this, we have to use scheme 4.0 so that we can use autoloader PSR-4 in the addon XML file.

    blog_7
    Scheme 4.0
    blog_8
    Autoloader PSR4

    In the addon.xml file, you can load the controller using namespace through the PSR4 tag.

    Now, you have to create the src folder inside the addon.

    blog_13
    Structure for hook

    Use ServiceProvider.php for registering hook handlers classes.

    blog_14
    Register hooks class

    Now, map hook in bootstrap.php using getHookHandlerMap() function.

    blog_15
    Map Hook methods

    Then, you have to create methods in the hook handler class according to the map.

    blog_16
    Hook method

    Note:- The blog will contain TPL Hook.

    For Rest API documentation:- Refer to this link

    Current Product Version - 1.0

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home