I want to track the Magento shop data by hooks the Magento events. I'm new in Magento2 so I don't know to hook the events and where to call an observer. I want to know how to call events which directory can we called. what hierarchy can be used? How to know the name of events?
Create a event file: events.xml
File: app/code/Vendor_Name/Module_Name/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_add_product_complete">
<observer name="observername" instance="Vendor_Name\Module_Name\Observer\ObserverClass" />
</event>
</config>
Create Observer class
File: app/code/Vendor_Name/Module_Name/Observer/ObserverClass.php
<?php
namespace Vendor_Name\Module_Name\Observer;
class ObserverClass implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
//Your code to run when event is fired.
return 'Event fired';
}
}
In above example, whenever event "checkout_cart_add_product_complete" is fired, the code inside Observer class will be executed.
To get a list of events available in Magento 2, you can visit : Link
Thanks, I hope this will help you. If you have any doubt or problem, feel free to ask in comment.
Very basic information about the events and observers you can find in the manual, check here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/events-and-observers.html
Related
I want to add a "payonbill" feature for my shop so i am using the checkmo payment method to do so. I added a new module with this configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<payment>
<checkmo>
<active>1</active>
<model>Magento\OfflinePayments\Model\Checkmo</model>
<order_status>processing</order_status>
<title>Check / Money order</title>
<allowspecific>0</allowspecific>
<group>offline</group>
<payment_action>authorize_capture</payment_action>
</checkmo>
</payment>
</default>
</config>
By doing so that payment status will migrate to processing and the payment action will be triggered for authorization. When doing so i am no longer able to even place an order on the shop for this method. The problem actually is there because of the payment_action. When i remove that it works again.
Is there a way to handle this scenario? When someone can pay on bill i would like to trigger the event sales_order_invoice_pay as well and continue the flow.
Best
Pim
I fixed the issue by actually removing the stuff i previously did and by implementing this module:
https://github.com/dominicwatts/autoinvoice
The actual problem was that all was working fine but in the end for the offline payment the invoice is generated when booking it inside admin. Bypassing the status won't generate the invoice for you.
I am making use of the org.ofbiz.webtools.GenericWebEvent service which is invoking the updateGeneric method, instead of having multiple routes for all the different forms, I've created one which manages them all using the following code:
controller.xml
<request-map uri="process">
<security https="true" auth="true"/>
<event type="java" path="org.ofbiz.webtools.GenericWebEvent" invoke="updateGeneric"/>
<response name="success" type="view" value="home"/>
<response name="error" type="view" value="CURRENT_PAGE_HERE"/>
</request-map>
As you can see in the error part, the value shown is CURRENT_PAGE_HERE, I'd like the route to show whatever page that is being displayed, for example error occurs on login, re-show login with the notice, error occurs on register, re-show register etc.
How could this be achieved?
The GenericWebEvent#updateGeneric method is a functionality used to update GenericValues and is used in the Webtools to edit/update the data.
The class name GenericWebEvent might be a bit misleading here but if you have a look at the implementation it should be clear that it does not generically handle different web events like you want to do.
I have added an custom observer and added custom logic to redirect after saving the address. redirection works fine but address does not save. I also tried to print the new address in observer.It display the new address but after redirection previous address is displaying.
Redirection code
$this->_responseFactory->create()->setRedirect($r_url)->sendResponse();
exit();
Event xml code
<event name="customer_address_save_after">
<observer name="test_observer_name" instance="Namespane\ModuleName\Observer\RedirectafterAddressUpdate" />
</event>
I have to open minicart box after addtocart button click. Is it possible to using section.xml? Anyone know about sections.xml usage in magento2?
What are exactly those sections?
A section is a piece of customer data grouped together. Each section is represented by the key that is used to access and manage data and data itself. Magento loads sections by AJAX request to /customer/section/load/ and caches loaded data in the browser local storage under the key mage-cache-storage. Magento tracks when some section is changed and load updated section automatically.
How do you define a section?
A section defined in di.xml file by adding a new section into sections pool
<type name="Magento\Customer\CustomerData\SectionPoolInterface">
<arguments>
<argument name="sectionSourceMap" xsi:type="array">
<item name="cart" xsi:type="string">Magento\Checkout\CustomerData\Cart</item>
<item name="directory-data" xsi:type="string">Magento\Checkout\CustomerData\DirectoryData</item>
</argument>
</arguments>
So here two new sections are registered cart and directory-data. Magento\Checkout\CustomerData\Cart and Magento\Checkout\CustomerData\DirectoryData implements Magento\Customer\CustomerData\SectionSourceInterface and provides actual data as result of getSectionData method.
How are the section updates triggered?
Magento assumes that customer's private data is changed when a customer sends some state modification requests (POST, PUT, DELETE). To minimize the load on the server, developers should specify which action (or request) updates which customer data section in etc/section.xml.
<action name="checkout/cart/add">
<section name="cart"/>
</action>
Action name is an action key pattern. When a user calls to action that matches specified pattern Magento will detect that the corresponding section is outdated and loads it again. If the action name is * that means that section will be updated on each POST and PUT request. If section tag is missed then all sections will be updated.
So conceptually this is wrong to update mini cart when you rich cart page. At this point, the mini cart (or cart section) already should be updated.
You can find more information about Customer Data here
There is already a full-fledged free extension available on Magento's marketplace and Github
magento: https://marketplace.magento.com/ambab-module-slidingcart.html
Github: https://github.com/ambab-infotech/slidingcart
This might help someone.
Reference URL: https://roshanyadav007.wordpress.com/2020/01/24/sliding-cart-extension-for-magento-2-magento-2-3-x/
I am migrating a project from struts 1.3 to struts 2.0. Now I have read through various resources available on web but could not find a struts 1.3 reset() form method alternative in struts 2.0.
My action class extends ActionSupport and implements Modeldriven. I have implemented the validate method in action class and that works perfect. However, when the form has no errors and the submission is done properly, the page reloads with initial values.
I expected that I will receive a blank form. I looked for a reset() form method but could find none. Currently, I am explicitly setting all form values to blank. I don't see this as a good programming practice. Please suggest how to implement form reset() in struts 2.0.
EDIT: ok, you are working with Tiles (that I've never used), then I'll try another solution:
if you have an empty JSP tile, and after the user compiled its fields and submitted the form, you want to ALWAYS clear those fields... what about declaring only SETTERS in the Action, and not the GETTERS ?
You could only set from JSP to Action, but not read them from the JSP... then your fields will be always empty in the page, and always rewritten by setters in the Action.
If i got it, you are on JSP, you call the method saveStuff() of the Action (or of another action), then you come back to the page...
in this case, you can use a RedirectAction return type, that will strip all params from the request, and redirecting your request to the execute() method of your Action.
So, instead of
<action name="myAction" class="com.blabla.myAction">
<result name="success">/myPage.jsp</result>
</action>
just do
<action name="myAction" class="com.blabla.myAction">
<result name="success">/myPage.jsp</result>
<result name="stuffSaved" type="redirectAction">
<param name="actionName">myAction</param>
</result>
</action>
This way you re-enter in the initial state (assuming you are not putting anything in session that is read by the JSP).
Because your Action and Form are in the same class in Struts2, you have to implement your own reset() method to clear any data that you would like.