How to get the reference to a model object instance in Magento - magento

I am creating a module dealing with a registration process which requires a certain amount of registration fee from its user. the basic flow is 1)user inputs personal information in the first page, and posts the form to the second page 2) user reviews the information in the second page which contains a PayPal button. 3) after confirmation, user clicks the PayPal button and is redirected the PayPal payment page 4) user is redirected to a success page if transaction is finished or to a cancel page if transaction not finished. I created a table called 'registration' that holds the general info for a registration such as id, register name and etc. In the 'PostAction' function (related to the second page), I set the model attributes by using info from the posted form, but do not call the save() method which I think should be done in the SuccesAction (related to success page). My question is how to get the reference to the model object in the SuccessAction so I can use the save() method to insert it into the database. Also, any suggestion or idea to improve the module is welcomed.

The registration object/model is disposed as soon as the 'postAction' page completes. You cannot get it back unless it has been saved somewhere. That somewhere could be a session variable, but those can expire when you least expect it (see Finagle's Law) so saving to the database is better. Either a separate table or by marking the registration with a status to show it is not yet complete - the 'successAction' would complete the process by setting that marker then saving again.
As an example consider how orders pass through checkout. Until the customer gets to the last step and submits the order, it is only a quote and has been saved in the sales_flat_quote table so far. After that point it is saved in sales_flat_order instead.

Related

Magento 2: Generating Customer Session Based Caching Pages

Scenario:
In an integration in Magento 2 with Full Caching.
We have two types of customers Type 1 and Type 2. Both these types of customers are saved under same Customer Group we are distinguishing the customers based on a customer type attribute.
Customer of type 1 can belong to different departments (1 to many). When he first visits the site after login, he is redirected to select his department.
This value is saved in customer session, on run time customer can change his department that value is updated in the existing customer session.
When we access a new page we get the updated session, but if we access a page that was accessed earlier, we get the old data from session.
Options tried:
We tried to load the data from customer session factory instead of customer session, but still we are facing the issue. Our understanding is that data which is saved in customer object is loaded as new, but since our data is saved under customer session it is not returning the latest data.
For some section of the page we have used sections to load the data, but in that scenario also, if a page is accessed earlier the correct data is not loaded in first refresh, in second refresh the data is loaded properly.
We also tried to update the session data from AJAX, but that was also not much useful since that data is not getting loaded on the pages.
We also tried to use context variables, but when we log the data while saving the context variable the correct data is logged but when we try to fetch the context data, no data was returned. Also even if different context variable is set for each department, the page seems to be rendered from cache.
We can’t use Cookie since this is sensitive data, this will be the last option.
In Magento 1 we had an option to generate new Cache key by extending app\code\local\Enterprise\PageCache\Model\Processor.php,
Example
if
(isset($_COOKIE[Enterprise_PageCache_Model_Cookie::IS_USER_ALLOWED_SAVE_COOKIE]))
{ $uri .= '_' .
$_COOKIE[Enterprise_PageCache_Model_Cookie::IS_USER_ALLOWED_SAVE_COOKIE];
}
Is there any similar option available in Magento 2. If yes, please share some reference links to integrate the same.

Laravel best way to store a function then retrieve it and execute it

I am creating a payment page which uses PayFast.co.za.
When the user completes payment on the PayFast server, Payfast, in the background fires off a call to a script on the server(itn.php) to verify. then the user is redirected to the thankyou page. I use the thank you page to execute the function that iterates through the session data stored in their cart to update the database.
I want to do the executing of the cart data in the itn.php file instead of the thank you page.
So my question is... How do I store the data of a function(the thank you page) before the user is directed to Payfast servers and then retrieve it and execute it?
Do I use serialization? queues??
I notice when I am using queues for sending emails the data in the database is stored then executed later. I need to be able to store the cart data and associate it with the invoice id then recall that on the itn.php that Payfast is executing. Payfast is also sending the new invoice id to the itn.php page.

Joomla 2.5 - custom registration field not remembering values

Im using custom profile plugin found at http://library.logicsistemi.it/images/joomla/plg_user_testprofile.zip
I have created several fields, both, optional and required and they all work. But there is a tiny problem.
When I enter some value in that custom fields and submit the form it can happen that some fields dont pass validation (which is good). However values entered are cleared and I must fill all the fields from the beggining.
Demo: http://goo.gl/eH1G2
Enter some (not all) data under the 'legend' named User Registration
Enter some (not all) data under the 'legend' named Company
Informations
Press Register button
Error message will pop in. -
Data entered in the User Registration will be saved
Data entered in the Company Informations (which are custom fields)
will not be saved.
If all required fields are entered, form will submit data to database
(which is ok).
I want that data in the Company Informations are saved, so users dont have to type in all over again.
The problem in your demo is on the template you are using.
For some reason it gives Javascript errors. These errors blocks the Javascript client side validation and your form is submitted with invalid data.
If you want only server side validation you can modify the onContentPrepareData function in your plugin by loading posted information from the user state. To do this you have to permeform a different action when user_id is not set under "Merge the profile data." comment.
I think next week I will write a new tutorial on http://library.logicsistemi.it to explain this. For now try to fix your Javascript code.
Regards

To find customer's first login

Magento 1.6.
Within the login processing code, is it possible to find out when the user/customer has logged in for the very first time?
If your Magento is configured not to use double-opt-in (email confirmation) for customer registrations, then you can use what #PauGNU already posted:
$created_at = $customer->getCreatedAt();
But when it comes to double-opt-in, Magento creates the customer account immediately, i.e. setting created_at to the current system time, but does not activate it (so that customer cannot login before confirming) and only sends a confirmation mail.
This means an unkown delay (minutes, days, weeks, whatever) between created_at and the very first login, so created_at wouldn't be of use anymore.
Actually, Magento has a place, where customer login times are being tracked by default: the table field log_customer.login_at, accessible by Mage_Log_Model_Customer, for example.
But, if you plan to use it:
by default the class has no method to get the very first login. You'd need to develop that yourself.
if "Log Cleaning" is active (to keep the database smaller), you'll gonna lose the saved login times.
In that case, I'd prefer identifying the most proper event, hooking into it and saving only the very first login time per customer to a separate table.
Given that the first login is always when the customer registers itself in the web, you only need to check out the field «created_at» on the customer_entity table.
If you load a customer, it's really easy to get that data:
$created_at = $customer->getCreatedAt();

Paypal Standard IPN Return Variable When Order Is Cancelled

I'm using Paypal Standard as the payment. If a user cancels does Paypal send custom variable back?
Here's the current workflow
Page 1 - User creates listing
Page 2 - User previews listing
Page 3 - Display total cost for the listing and if they hit pay it will redirect to Paypal
Post is inserted into the database and Paypal custom variable is sent
We send "custom" variable to Paypal and if user is successful it will return the "custom" variable back and enable the listing by changing the status in the database.
Does Paypal send the custom variable back when user decides to hit cancel? I want the variable to come back and delete the listing from the database.
As far as I know paypal IPN only sends data when an event occurs: Completed, refunded, etc...
What you can do is create a new field in your table called status with a default value of 0 and a timeStamp. When a user complete's payment, set that field to = 1.
Then, to weed out incomplete orders you can setup a CRON job that goes through your DB every few days and removes/archives rows that have WHERE status=0 AND timeStamp < '$today'.

Resources