Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Can we get a tutorial on hooks?


John W

Recommended Posts

There's been a lot mentioned about hooks and the hook system, but I haven't seen a how to.  I realy like the idea and instead of trying to reverse engineer I would like to know the accepted system.  I did some searching but haven't really seen one.

I'm not really a dog.

Link to comment
Share on other sites

somewhere in product_info.php

 

$OSCOM_Hooks->register('product');
$OSCOM_Hooks->call('product', 'HelloWorld');
Hook file:

 

class hook_shop_product_pi_hooks {
  function listen_HelloWorld() {
    return '<p>Hello World</p>';    
  }  
}
Have at it.
Link to comment
Share on other sites

In my Gustavo template, I use a hook to display the breadcrumb, as the breadcrumb is moved from the Header area into the "body" of each page (just above the <h1> heading on each page). There is no modular call available to use at that point, so easiest was siply to invoke a hook.

 

In that "loaded" build I have a number of Hooks;

- format_postcode

formats the postcode as shown in a thread from the other day

- must_agree_terms

enforce a checkbox on checkout_shipping.php

- call_to_action

if customer has items in cart, displays a message and button

- already_bought

if customer has previously purchased the product they are looking at, message warns them

- specials

adds in a whole new system to give another layer of special offers (eg end of line items)

 

It's a system that has it's uses. I explain Hooks best as;

 

Imagine it is an "always on" module that can be placed anywhere in the page

Link to comment
Share on other sites

Its usefull who wants to develop something https://github.com/haraldpdl/oscommerce2/commit/e4344f7d93b2ed07e36e26ac20a7555ef2f1d23d

 

Hooks helped me a lot in payment modules at admin orders and very easy to add some new extended tabs, buttons or built in API-s if you use paypal apps tab formed orders.php

:blink:
osCommerce based shop owner with minimal design and focused on background works. When the less is more.
Email managment with tracking pixel, package managment for shipping, stock management, warehouse managment with bar code reader, parcel shops management on 3000 pickup points without local store.

Link to comment
Share on other sites

  • 5 months later...

George (multimixer) has a good tutorial on how to install the hook system here...

 

http://multimixer.gr/30/10/2015/how-to-install-the-oscommerce-hook-system/

 

Dan

Link to comment
Share on other sites

Thought I would see if I could get the example hook to work. 

 

I'm such a dummy at times (don't rush to agree).   I must have looked at the hook that Gary posted above, for an hour or so, before I figured out that I needed to add the php tags to the pi_hooks class and add an echo before the $OSCOM_Hooks->call('product', 'HelloWorld'); call.  Duh...call me slow... (w00t)

For those who are as code challenged as I am, the example that worked for me is as follows:

 

somewhere in product_info.php add

 

$OSCOM_Hooks->register('product');
echo $OSCOM_Hooks->call('product', 'HelloWorld');

 

 

and create the pi_hooks.php hook file....it should be located in /includes/hooks/shop/product a directory that you'll need to create.

 

<?php
class hook_shop_product_pi_hooks {
  function listen_HelloWorld() {
    return '<p>Hello World</p>';    
  }  
}
?>

 

 

I presume the closing php tag is optional but I like using it.  I guess I'm not only slow but anal too. :D

 

Okay now that we have our hook working who has an example or two to share to demonstrate their potential? :rolleyes:

 

Dan
 

Link to comment
Share on other sites

Dan, you've just shown the community how it's done ... so now go one step further and make a new hook that does something more interesting...

 

Try this:  

Show Product Stock

in a Badged Button

placed right underneath the "buy now" button on product_info.php

 

Hints:

1.  in the hook function, use a global something to grab the product_id

2.  there is an inbuilt function for stock that can be used in that same hook function

Link to comment
Share on other sites

@@burt

 

Gary that sounds like a good challenge and I think it's within my pay grade, as someone else likes to say on here...it'll be a few days before I can get to it but I'm looking forward to it...it should be good learning experience for me.  Thanks for the suggestion....

 

Dan

Link to comment
Share on other sites

@@burt Gary that was a bit easier then I thought it would be.  Nice!

 

Here is the code...

<?php
class hook_shop_product_pi_hooks {
  function listen_StockBadge() {
     global $product_info;
     $output = '<button class="btn btn-primary" type="button">Number in Stock: <span class="badge">' . tep_get_products_stock($product_info['products_id']) . '</span>
                </button>';
    return $output;    
  }  
}
?>

If you have the time could you point me at where the global $product_info is set up....I'd like to learn a bit more about that.  Your input on how I set this up would be appreciated as well.

 

Dan

Link to comment
Share on other sites

I'm trying to get my head around hooks. Under what circumstances would you use a hook?

 

I can see Dan's example above, if I wanted to show that badge on a modularised product info page how would I do that?

 

Or can you use hooks in a module?

 

Or is it more useful as Harald has done in this example, to bust sections out of the core code? https://github.com/haraldpdl/oscommerce2/commit/d43814b46f196733bd2bbeadc87254c2144f7e75

 

Another thing, looking at this line in Harald's example:

foreach ( $initialize_checkout_methods as $value ) {
 $output .= '<p align="right">' . $value . '</p>';

Is it possible to link a template file to a hook as is done with modules? I'm thinking of "drop on top" addons in the future which may need separate css etc.

osCommerce user since 2003! :thumbsup:

Link to comment
Share on other sites

Further to that, what is the advantage of using

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2014 osCommerce

  Released under the GNU General Public License
*/

  class hook_admin_orders_paypal {
    function listen_orderAction() {
      if ( !class_exists('paypal_hook_admin_orders_action') ) {
        include(DIR_FS_CATALOG . 'includes/apps/paypal/hooks/admin/orders/action.php');
      }

      $hook = new paypal_hook_admin_orders_action();

      return $hook->execute();
    }

    function listen_orderTab() {
      if ( !class_exists('paypal_hook_admin_orders_tab') ) {
        include(DIR_FS_CATALOG . 'includes/apps/paypal/hooks/admin/orders/tab.php');
      }

      $hook = new paypal_hook_admin_orders_tab();

      return $hook->execute();
    }
  }
?>

as a hook, and then including the hook on the page, over just using the following code directly on the page where you want it?

      if ( !class_exists('paypal_hook_admin_orders_action') ) {
        include(DIR_FS_CATALOG . 'includes/apps/paypal/hooks/admin/orders/action.php');
      }

      if ( !class_exists('paypal_hook_admin_orders_tab') ) {
        include(DIR_FS_CATALOG . 'includes/apps/paypal/hooks/admin/orders/tab.php');
      }

I know these may be dumb questions but I don't quite get 'hooks' yet.

osCommerce user since 2003! :thumbsup:

Link to comment
Share on other sites

You can use hooks in place of a module, where there is no need to disable or reorder the code as you would in a module. Look on them as an always-on module.

 

I see most of the use for hooks in the processing part of a page, or in application_top.php. You can use them to add the code to process a new variable that you have added to a page. They also make sense in  the Admin, wherever you would want to add new functionality, such as code to add/modify the content of new database fields.

 

Edit: Hooks are a response to the "Don't modify core code" philosophy. The less you touch a core page, the better.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

At the moment, they are not much used, I don't think I've seen anything at addons that is a Hook. Let's take an example I made some time back (recapcha on contact us)...

Had it been made as a "old style" addon code: open up contact_us,paste 20 lines of code here, paste 10 lines of code there, save the file, upload.

Hook style:
open up contact_us,paste 1 line of code here, paste 1 line of code there, save the file, upload.

Much easier and less prone to copy/paste mistake.

IN THE FUTURE:

There will be pre-placed hooks throughout osCommerce.

Now imagine the possibilities of simply hooking into an already placed hook.  In the example above, had there been hook calls in the correct places, it would have been:  upload this hook file, done.

 

Now we can see possibilities ?

 

 

Haralds Example:

https://github.com/haraldpdl/oscommerce2/commit/d43814b46f196733bd2bbeadc87254c2144f7e75

This shows Harald removes L138 - L154 and replacing with 1 line of code.  

 

 

How about this:

A module could be not only a module, but also hook file.  Let's take a recent example which raised a discussion;  a rotator content module.  Instead of putting the .css via that module, do it via a Hook and hook into the pre-placed hook in the Header. That's a simple example, but shows the possibility.

 

Apps

If a module does things with modules and hooks (and perhaps other things), it's called an APP.  Hence Haralds rather large, yet solid "Paypal App"...

Link to comment
Share on other sites

@@frankl  Morning Frank.

 

 

I can see Dan's example above, if I wanted to show that badge on a modularised product info page how would I do that?

 

To add that hook to the product_info.php page, follow the initial instructions I posted in the "HelloWorld" post....replace the hook I used there with the new one containing the StockBadge and then just call the hook adding the code below where you'd like the badge to appear on your page.

 

 

<?php
$OSCOM_Hooks->register('product');
echo $OSCOM_Hooks->call('product', 'StockBadge');
?>

 

If you have any trouble, post back and I'll help as best I can.

 

Regarding your general question about hooks and why use them....I liked Gary's brief description...

 

 

Imagine it is an "always on" module that can be placed anywhere in the page

 

Dan

 

 

Link to comment
Share on other sites

I can see Dan's example above, if I wanted to show that badge on a modularised product info page how would I do that?

 

 

You'd need to find the relevant module ... and amend that module to call in the Hook.  

Eg, if you wanted Dans button right underneath the Price, you'd amend the module that outputs the price display.

Link to comment
Share on other sites

@@burt  Clear as mud.  :)  Actually I think I understand.  I printed out the global array from within the hook and all I can say is wow...there is a ton of stuff there. 

 

Is it fair to say that I can create a global from any of the variables/arrays that are present on the page I'm calling the hook from, so that they are available for use within the hook?

 

Dan

Link to comment
Share on other sites

@@burt @@Dan Cole @@kymation

 

Thank you gentleman for the additional info. This is quite exciting!

 

Have I got this right? In the modularised product_info.php page, with modules we can change the sort order of modules, but with hooks we add the hook to an existing (or new, on it's own) module to place it?

 

Can you give me an example of a pre-placed hook Gary?

 

Is that what you refer to as to the possibilities for your recaptcha code on contact_us.php?

osCommerce user since 2003! :thumbsup:

Link to comment
Share on other sites

Is it fair to say that I can create a global from any of the variables/arrays that are present on the page I'm calling the hook from, so that they are available for use within the hook?

 

In general, yes...

Link to comment
Share on other sites

Have I got this right? In the modularised product_info.php page, with modules we can change the sort order of modules, but with hooks we add the hook to an existing (or new, on it's own) module to place it?

 

Can you give me an example of a pre-placed hook Gary?

 

Is that what you refer to as to the possibilities for your recaptcha code on contact_us.php?

 

1. Yes.  A hook can extend a module.

2. Imagine if contact_us.php had an already existing hook right before it's ending /form and somewhere in the processing (after submit).  

- you want to add a recapcha...simply upload a file

- you want to add in a form field...simply upload a file

- and so on

3.  see above

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...