In my custom module i have set a flag in session variable, and in controller_action_layout_render_before event, checking if the flag is set in then append a custom content.
I want to unset the flag in session variable after the page is loaded.
How can it be done.
Is there any event which is executed at the end when the page is getting loaded?
The
controller_front_send_response_after
event should work for you. Temporarily adding some logging to app/Mage.php
public static function dispatchEvent($name, array $data = array())
{
//Mage::Log($name);
file_put_contents('/tmp/event.log',"$name\n",FILE_APPEND);
Varien_Profiler::start('DISPATCH EVENT:'.$name);
$result = self::app()->dispatchEvent($name, $data);
#$result = self::registry('events')->dispatch($name, $data);
Varien_Profiler::stop('DISPATCH EVENT:'.$name);
return $result;
}
can get you a list of events that have fired for a particular request, which is usually enough to track down the event you're looking for yourself.
I've you're looking to store a variable only for the life of a single request, you might consider using Magento's registry. Basically, you insert a value with Mage::register('some_variable_name',$variable); and retrieve it in some other context in the same request with Mage::registry('some_variable_name');
Refer to #Alan Storm's answer here for more information.
Related
My code in user file-
I am trying to onUserBeforeSave, but in this event, Joomla code is executed, control does not come to my code:
class PlgUserotp extends JPlugin
{
public function onUserBeforeSave($oldUser,$isnew,$newuser)
{
$errors = NULL;
$phone_number = NULL;
foreach ($newuser as $key => $value)
{
if($key=="username")
$username = $value;
elseif ($key=="email1")
$email = $value;
elseif ($key=="password1")
$password = $value;
else
$extra_data[$key]=$value;
}
//echo $username . $email .$password;
$this->startVerificationProcess($username,$email,$errors,$phone_number,$password,$extra_data);
//MoCurlOTP::mo_send_otp_token('EMAIL',$newuser["email1"],'');
}
}
The first step in debugging this issue is to make sure that your plugin is really being loaded. First add a die('In my plugin'); at the very beginning and see if it's really dying. If that's the case, then this means that Joomla is really loading your plugin (if it's not, then check that your plugin is enabled, also check that the System - cache plugin is disabled, just in case).
The next step is to make sure that Joomla is instantiating your plugin object, and this is done by adding a construct method, and then adding a die('In constructor'); in that function. If it's not displaying this message, then this means that your class name is not in line with the class name defined in your XML manifest file.
The last step is to check whether the onUserBeforeSave is really being triggered, by adding a die('In onUserBeforeSave'); at the very beginning of the function. If it's not triggered, then try debugging the file libraries/src/User/User.php (I'm assuming you're using Joomla 3.8.x), as the this event is triggered from there. If is being triggered, then this means that your code is working, but has no effect.
I have a session containing some data that I access throughout several methods in my controller with no trouble. But one method in particular has problems.
I have made a little booking system. On completion of a booking, I call a function to send an e-mail to myself and the customer:
public function complete(Request $request)
{
$details = Session::get('details');
// send emails: call sendConfirmationEmail(to address, using this view)
$this->sendConfirmationEmail(env('EMAIL'), 'emails.ourconfirmation');
$this->sendConfirmationEmail($details->booker_email, 'emails.bookerconfirmation');
return view('booking/complete');
}
private function sendConfirmationEmail($to,$view)
{
$from = env('OFFICE_EMAIL');
$to_address = $to;
$details = Session::get('details');
$instance = Session::get('instance');
Mail::queue($view, compact(['details','instance']), function($message) use ($to){
$message->from(env('OFFICE_EMAIL'))
->to($to)
->subject('Thanks for booking');
});
}
First I got an error accessing a non-object in my e-mail view. So to test it I just set the sendConfirmationEmail function to return $instance - blank page. Then I tested it by commenting out the function call in complete() and returning $instance there. No problem, there's a nice shiny session full of data. Then I tried passing $instance from complete() to sendConfirmationEmail() and returning it: again, blank page. Why can't sendConfirmationEmail 'see' my session?!
To my knowledge, Sessions are for using across HTTP requests, not across functions in PHP. You are trying the use Sessions as global variables.
You can simply capture details and instance session data in the complete function, and then pass them along as parameters for the sendConfirmationEmail() function.
The session value lost after the redirect. I m redirecting from pay_order method to do_payment method. when I print the session value in do_payment method its return false. just simple calling the do_payment method display the session value. Please help what is the problem with redirect..?
function pay_order($order_id)
{
$this->load->helper('url'); //loading url helper
$this->load->library('session');//loading session lib
$this->load->library('cart'); //loading cart lib
$this->load->helper('form');//loading form helper
$output = $this->cart->contents();// getting data from cart.
$output = $this->sort_array($output);// sorting the array
$list['data'] = $output;
$list['order_id'] = $order_id;
$this->session->set_userdata('abc', $list);// setting the session
redirect('checkout/do_payment'); // redirecting to do_payment
}
function do_payment()
{
$this->load->helper('url'); //loading url helper
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
var_dump( $arr );// return false value.
//$this->load->view('order/pay_through_gateway');
}
when I redirect from pay_order method the session is not available in do_payment method. why?
There is a problem, check the line in the method pay_order() where you are setting the session.
$list['order_id'] = $order_id
$this->session->set_userdata('abc', $list);// setting the session
Before that line you have missed semicolon. And everything looks fine.
Update:
There is no error as I think, check whether you may have null value while you are storing the values in session. And check the values by following code in do_payment() method.
function do_payment(){
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
print_r( $arr );// print the $arr content
}
Change this:
$list['order_id'] = $order_id
To this:
$list['order_id'] = $order_id;
You have a semicolon missing. You do not see any errors because of the redirection.
Where and how I am overriding the save method in Joomla 3.0 custom component ?
Current situation:
Custom administrator component.
I have a list view that displays all people stored in table.
Clicking on one entry I get to the detailed view where a form is loaded and it's fields can be edited.
On save, the values are stored in the database. This all works fine.However, ....
When hitting save I wish to modify a field before storing it into the database. How do I override the save function and where? I have been searching this forum and googled quiet a bit to find ways to implement this. Anyone who give me a simple example or point me into the right direction ?
Thanks.
Just adding this for anyone who wants to know the answer to the question itself - this works if you explicitly wish to override the save function. However, look at the actual solution of how to manipulate values!
You override it in the controller, like this:
/**
* save a record (and redirect to main page)
* #return void
*/
function save()
{
$model = $this->getModel('hello');
if ($model->store()) {
$msg = JText::_( 'Greeting Saved!' );
} else {
$msg = JText::_( 'Error Saving Greeting' );
}
// Check the table in so it can be edited.... we are done with it anyway
$link = 'index.php?option=com_hello';
$this->setRedirect($link, $msg);
}
More details here: Joomla Docs - Adding Backend Actions
The prepareTable in the model (as mentioned above) is intended for that (prepare and sanitise the table prior to saving). In case you want to us the ID, though, you should consider using the postSaveHook in the controller:
protected function postSaveHook($model, $validData) {
$item = $model->getItem();
$itemid = $item->get('id');
}
The postSaveHook is called after save is done, thus allowing for newly inserted ID's to be used.
You can use the prepareTable function in the model file (administrator/components/yourComponent/models/yourComponent.php)
protected function prepareTable($table)
{
$table->fieldname = newvalue;
}
isn't this the right method to get Name of logged in customer?
<?php echo Mage::helper('customer')->getCustomer()->getName(); ?>
I have a website with live chat functionality. Yesterday I have been asked to pass email address and the name of the logged into the user into the Javascript Tracking variable code placed in the head section of the website. So that the operators could see who is on the website and whom are they talking to without any need to ask about their information.
So I passed the information from Magento into the Javascript code but now I see this very strange thing happening. For example,
If I am logged in with credentials Name = John Email =
john12#yahoo.com
Then This name and email variable values are changing with the change of pages. For example if I click on any product page the variable values which I am passing changes to some other user's information.
Name becomes Ricky Email becomes ricky23#gmail.com
this variable values are kept on changing back to john and from john to something else with the change of pages. So operator does not have any idea whom are they talking because the values are kept on changing. Also, user ricky or who ever it changes to also exist in the database. so it is picking up random person from the database.
This is what i did to pass the code to javascript. Please let me know if that is not the right code to pass the information. Please check the php code I am using to fetch information from Magento. Roughly, I receive incorrect value once in 5 times. Please provide some assistance. Thanks in advance.
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$firstname = $customer->getFirstname();
$lastname= $customer->getLastname();
$name = $firstname . ' ' . $lastname;
?>
<script type="text/javascript">
if (typeof(lpMTagConfig) == "undefined"){ lpMTagConfig = {};}
if (typeof(lpMTagConfig.visitorVar) == "undefined"){ lpMTagConfig.visitorVar = [];}
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Email=<?php echo $email; ?>';
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Name=<?php echo $name; ?>';
</script>
I'm also attaching a snap shot
I'd be interested to hear how you're adding this code to the page? Is it in it's own block, or are you adding it to footer.phtml, or similar? If your adding to an existing block be sure to check the block caching settings of that template.
To confirm the caching hypothesis I'd ask the following:
Do you get the same name, all the time, on the same page? When you refresh the page, do you get the same name and email in the Javascript?
Does the problem persist with caching disabled?
This doesn't sound like a singleton problem at all. Each execution of the PHP script is isolated from the others, serving one page request. There's no chance of another customer's object moving between invokations of the script.
It is a matter of understanding the singleton pattern. If you call your code twice:
$customer_1 = Mage::helper('customer')->getCustomer()->getName();
$customer_2 = Mage::helper('customer')->getCustomer()->getName();
you get two different instances of the object. But... if one of them has already implemented a singleton pattern in its constructor or has implemented a singleton getInstance then both objects will actually point to the same thing.
Looking at the customer/helper/Data.php code you can see the function
public function getCustomer()
{
if (empty($this->_customer)) {
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
return $this->_customer;
}
That means that in one of the cases singleton is already implemented/called and in other one - not as the property is already set.
The correct way to work with quote/customer/cart in order to get always the correct data is always to use the singleton pattern.
So using this:
$customer = Mage::getSingleton('customer/session')->getCustomer();
always guarantee that you get the correct customer in that session. And as may be you know singleton pattern is based on registry pattern in app/Mage.php:
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}
and looking at app/Mage.php:
public static function register($key, $value, $graceful = false)
{
if (isset(self::$_registry[$key])) {
if ($graceful) {
return;
}
self::throwException('Mage registry key "'.$key.'" already exists');
}
self::$_registry[$key] = $value;
}
...
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
you can see that Magento checks is it is already set. If so, Magento will either throw an Exception, which is the default behavior or return null.
Hope this will help you to understand the issue you face.
I have sorted this out. I have moved the code from footer.phtml to head.phtml and it's working fine now.Values are not changing anymore. If anyone know the logic behind please post and I will change my answer. So far this is working.