How it works...

In order to modify the existing business, we need to use the include() function. This function will internally do the monkey patching and replace the original object properties with the new ones. If you want to access the initial method, you can use the this._super attribute.

The set_value() function of the OrderWidget is used to update the product price from the keyboard. The same function is used to apply the discount. So, in our recipe, we replaced the set_value() method with our own implementation. We do not want to change the default implementation, so we called _super() first, which will call the default set_value() method. After the super call, we placed our logic to display a warning if the user sets a lower price than the purchase price of the product. To display the popup, we used the show_popup() method of the gui.

We placed our business logic after the default implementation (_super) is called. If you want to write business logic before the default implementation, you can do it by moving the _super call to the end of the function.

Using include() can break things if not used carefully. If the method is inherited from several files, you must call the super method, otherwise it will skip the logic in the subsequent inheritance. This sometimes leads to a broken internal data state.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset