How to add custom code to shop back end?


#1

I’d like to inject some custom business logic into the shopping cart. For instances, upon a customer purchasing a specific item, I’d like to update an external database and send them email.

I can write in Java, but I don’t have any OFBiz background. I’m not readily seeing where one can insert / revise the Java behind this sort of event. I’d love to find something in the Java like a call to a method akin to “onPurchaseEvent( int sku )” or something simple and obvious like that.

The OFBiz tutorials I see out there are just about creating simple hello world apps from scratch - I’m not seeing resources describing much about revising the innards of the existing programs.

Could you point me in the right direction as to how I can get started?


#2

Hey Buvin,

sure… here’s what you need:

https://www.scipioerp.com/community/developer/events-actions/

That link should explain how events in general are triggered. The controller events are the ones you are most likely looking for here (although depending on your use case you could also hook your service onto an existing service such as “createOrder” or even a change to the database -the table OrderHeader may be work in this case).

In general, I would propose you also check out

https://www.scipioerp.com/community/developer/services/ and
https://www.scipioerp.com/community/developer/entities/

That should explain fairly well how you can create new services on your own and how you can make changes to the database from there (if you want to do that).

The docs are written in such a way that if you start with “Architecture” they should guide you through the most important concepts, so I recommend starting from there, if you haven’t familiarized yourself.

That being said, there is loads of examples for what you are trying to do here - just depending on whether you want to mess with the existing controller entries (those handle the request flow) or a service or database call, you may need to check for different events.

Let me know if you require any example of a service and I will try to find one for you first thing tomorrow.


#3

Great! Thank you, that helped a lot! Since I’m totally new to this development framework, it’s clear that I have a whole lot to learn if I want to be “real” OFBiz developer, but I have some notion of how the design works now.

Since I’m looking to jump right into this and have some instant customization, I searched for the “createOrder” key term. That lead me to:

scipio-erp\applications\shop\webapp\shop\WEB-INF\controller.xml

In which I found :

<request-map uri="createOrder">
    <security https="true" auth="false"/>
    <event type="java" path="org.ofbiz.order.shoppingcart.CheckOutEvents" invoke="createOrder"/>
    <response name="error" type="view" value="checkoutreview"/>
    <response name="success" type="view" value="checkoutreview"/>
</request-map>

Using Eclipse, I quick jumped to the source of org.ofbiz.order.shoppingcart.CheckOutEvents. That was found in this file:

scipio-erp\applications\order\src\org\ofbiz\order\shoppingcart\CheckOutEvents.java

There I found a method with this signature:

public static String createOrder(HttpServletRequest request, HttpServletResponse response)

And this notable object instantiation:

ShoppingCart cart = ShoppingCartEvents.getCartObject(request);

So, I’m guessing I can evaluate the contents of the cart object to get what products where purchased, and then go off into my own custom code using that?

Here’s two important questions:

  1. Is createOrder() only called after a successfully confirmed transaction? I.e when I’m at this point in the code, can I be certain the customer has paid for their items, or there an additional check to be made for that?

  2. Is there an officially recommended way for me to insert custom code like this, and still permit an easy means for upgrading the system as a whole? Or do I need to just create my own repository and branches and then step through manual merges periodically?


#4

Hello,

Depending on the complexity of your needs, you can avoid modifying existing code by replacing the controller event to point to a method of your own, and your method delegates to the original one. Alternatively, you can define your own request-map with event pointing to your method (with security direct-request=“false”), and chain the responses above to it with type=“request” and value="[your new request-map name]".

If your needs are more complex you may need to edit the existing java code in place. Projects are updated through git, so you have to resolve git conflicts in that case should they occur.


#5

For createOrder you could chain a custom request-map/event to the “success” response, and at that point order should have been created successfully. The cart will have been cleared so you must lookup order details through the database with the orderId (sometimes OrderReadHelper.java is enough).


#6

Thanks for the help! You guys are so responsive!

That definitely points me in the right direction. I’m a bit uncertain about which path to take just yet, to ease the git merging conflicts and/or limit the work on this end in such regards, but I’m going to explore that a bit.

As, I’m only working on all this a little a time right now, unfortunately, some of my questions are about getting a decent idea of what I will need to do, but with the need to defer some of it for the time being.

I’ve bumped up the priority of just getting a production environment setup now. I ran into a handful of issues, and gotten passed some, but am hung up currently on one in particular. I’ll start a new thread or two on these matters.


#7

Alright, just keep posting. We are here to support.