Infinite loop error when using header in observer - magento

I'm just trying to set up terms and conditions page after a successful login. All seems to to working well. I am getting the post data through the form which I have placed in CMS Page with identifier 'general-conditions'. The problem is only in the
header('Location: '.Mage::getUrl('general-conditions'));
If I comment out this line.. page loads properly but if I don't it gets caught in infinite loop.
Could anyone please help me, that's the only thing left and I've spent a lot of time on it.
Thanks in advance.
<?php
Class Rik_Terms_Model_Observer{
public function checkGeneralTerms(){
if (Mage::helper('customer')->isLoggedIn()) {
$id = Mage::getModel('customer/session')->getId();
$customer = Mage::getModel('customer/customer')->load($id);
$customerData = $customer->getData();
$groupId = Mage::helper('customer')->getCustomer()->getGroupId();
$groupName = Mage::getModel('customer/group')->load($groupId)->getCode();
$storeId = Mage::app()->getStore()->getId();
if($customer->getGeneralTerms()=='0'){
$pageIdentifier = Mage::getModel('cms/page')->checkIdentifier("general-conditions", $storeId);
if ($pageIdentifier){
header('Location: '.Mage::getUrl('general-conditions')); // problem
die();
}
}
}
}
}

Found later, it was actually conflicting with extended version of my CMS Module.

Related

Magento OPC Observer redirect

I'm struggling with a special little problem related to the redirect out of some Magento observer.
I wrote an extension and put an observer to the "checkout_submit_all_after" event, which works out just fine. My little extension automatically creates an invoice and sets the order status to processing, once the payment method is "Invoice". Unfortunately the redirect after submitting an order in the one page checkout doesn't work anymore. It always redirects to "checkout/cart" instead of "checkout/onepage/success".
Someone any ideas what I'm doing wrong?
Here's my code:
class Shostra_AutoInvoice_Model_Order_Observer
{
public function __construct()
{
}
public function auto_create_invoice($observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order->hasInvoices()) {
$payment = $order->getPayment()->getMethodInstance()->getTitle();
Mage::log("payment method: " . $payment);
if($payment=="Rechnung"){
Mage::log("autocreating invoice");
$invoice = $order->prepareInvoice();
$invoice->register();
$invoice->pay();
$invoice->save();
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
Mage::log("invoice created and saved");
}
$this->addComment('Order automatically set to paid.');
} else {
$this->addComment('no invoices found.');
}
$response = $observer->getResponse();
$response->setRedirect(Mage::getUrl('checkout/onepage/success'));
Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
}
}
Thanks a lot!
Why don't you try sales_order_save_after event and try saving it, so no issues with redirection manually as Magento will take its own course,
you can refer to this link for more explanation
http://inchoo.net/magento/magento-orders/automatically-invoice-ship-complete-order-in-magento/

Magento returning incorrect customer data on frontend pages

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.

CodeIgniter flashdata

I'm running into a problem I just can't seem to fix. I'm working on kind of a CMS kind of thing. And one of the functions is making image slideshows.
In one controller, I check if a slideshow with a certain ID exists, and if it exists, it should take the data and work with it, otherwise, set an error message (CodeIgniter flashdata) and redirect to the homepage.
The code I use for this is the following:
if($this->Mslideshow->slideshowExists($showid) === FALSE){
echo 'I\'m getting here';
$this->session->set_flashdata('error',$showid);
redirect('admin/index','refresh');
}else{
echo 'Slideshow exists';
}
And the code of the slideshowExists() function is:
public function slideshowExists($showid)
{
$this->db->where('id',$showid)
->limit(1);
$query = $this->db->get('slideshows');
if($query->num_rows() < 1){
return FALSE;
}
$this->currentquery = $query;
return TRUE;
}
And the problem is this, very strange actually. If the result I get back is FALSE, everything goes as planned. Error message gets set and redirect goes to 'admin/index'.
But if what I get back is TRUE, then the stange thing happens. I do get an echo with 'Slideshow exists', but I also get the error message.
I've tried everything, deleted cookies. Reset all sessions etc.
And even stranger is that when I tried to put the $showid I use to check into the error message, all of a sudden $showid is ' img '. While everywhere else is '1' or '2'...
Hope someone can help. Thanks!
=====EDIT=====
I tried to edit the code and simplify it. Right now I have this code in my Controller:
public function slideshow($showid){
$query = $this->db->where('id',$showid)->get('slideshows');
if($query->num_rows() < 1){
$this->session->set_flashdata('error','Slideshow doesn\'t exist.');
redirect('admin/index','refresh');
}
$data['page'] = 'slideshow';
$data['title'] = 'Slideshows';
$this->scripts->load_scripts(array());
$this->scripts->load_functions(array());
$this->load->view('admin/dashboard_template.php',$data);
}
When I run this with a $showid, that doesn't exist, I get the error message after being redirected to 'admin/index'. When I use a $showid that does exist, I do get the error, but no redirect but just the rest of the code.
I think you have to read your flash data in your view:
$error = $this->session->flashdata('error');
var_dump($error);
or in your Controller:
$error = $this->session->flashdata('error');
if(isset($error)) {
var_dump($error);
}
Also you can read this question: CodeIgniter "flashdata" doesn't work
you can get the flash data by following way
$error = $this->session->flashdata('error');
if($error)
{
echo $error;
}
You can try
if($this->session->flashdata('msg') != "")
This works for me.

Magento - module is not updating prices on a per-website basis

I am having trouble with a module that is supposed to update prices on a per-website basis.
At the top of my module I have :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
This should mean that when I call the module (the module has a frontend) I am able to update prices etc as 'admin'. However, this just isn't happening. Currently I have gone on a few desperate efforts to get it working - seeing if number formats trip it up or indexes not reindexed, cache problems and anything else.
The following snippet actually produces the right numbers but it just is not saving to the database. Any ideas?
$product = Mage::getModel('catalog/product')->
setStoreId($storeId)->setWebsiteId($websites[$store])->
loadByAttribute('sku',$price['SKU']);
$oldprice=(float)str_replace(",","",$product->getPrice());
if($newprice!=$oldprice) {
Mage::log($product->getPrice());
$product->setPrice(number_format($newprice,4,".",""));
Mage::log($product->getPrice());
$product->getResource()->saveAttribute($product, 'price');
$product->save();
Mage::log($product->getPrice());
unset($product);
}
Let's assume that you are updating the prices of products using a method in your module's class, with the following sample code:-
public function updateProductPrices ($sku, $newPrice) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$websites = Mage::app()->getWebsites();
$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);
foreach ($websites as $_eachWebsite) {
$_websiteId = $_eachWebsite->getWebsiteId();
$websiteObj = new Mage_Core_Model_Website();
$websiteObj->load($_websiteId);
$storeIds = $websiteObj->getStoreIds();
if (count($storeIds)) {
foreach ($storeIds as $_eachStoreId) {
$product->setStoreId($_eachStoreId)
->load($productId);
$oldPrice = $product->getPrice();
if ($oldPrice != $newPrice) {
$product->setPrice($newPrice);
$product->save();
}
}
}
unset($storeIds, $websiteObj, $_websiteId);
}
unset($product);
}
Hope it helps.
Try:
$product->setPrice($newprice)->save();
I am having similar issue and I think there is a bug in the answer from Knowledge Craving. The code will actually not update on a per website basis but rather update one unique price to each and every website
The bug I am talking about actually happened to me with similar code and is that other product attributes will be unlinked from default (at least, it is what happened to me).
Below is a suggestion based on Knowledge Craving's solution, I just amended a bit
public function updateProductPrices ($sku, $newPrice, $websiteId) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);
$websiteObj = new Mage_Core_Model_Website();
$websiteObj->load($websiteId);
$storeIds = $websiteObj->getStoreIds();
if (count($storeIds)) {
foreach ($storeIds as $_eachStoreId) { //Does it need to do for each storeview, price being anyway website scope?
$product->load($productId);
$oldPrice = $product->getPrice();
if ($oldPrice != $newPrice) { //actually, this if would not be necessary as you anyway want to update, right?
$product->setStoreId($_eachStoreId)
->setPrice($newPrice)
->save();
}
}
}
unset($productId, $storeIds, $websiteObj, $product);
}

Magento createBlock method not working, displaying static block data

Ok so Ive created static blocks in my CMS area, and Im trying to output them inside of a custom homepage template Ive built.
Every document I can find says to output the block as follows
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-block-identifier')->toHtml() ?>
That didnt work for me, so then I tried another way. --
<?php $block = Mage::getSingleton('core/layout')->createBlock('cms/block')->setBlockId('my-block-identifier');
echo $block->toHtml();
All the sites referencing this tell me to use the actual blocks identifier to get the block. So then I decide to manually lookup the block_id in my cms_block table and see if using the block_id number in place of the literal my-block-identifier name will work - and it did. So I am confused... Can anyone tell me how I can get the block by the actual identifier, or look up the blocks id by the identifier so that I can grab the block by block name?
Any help much appreciated.
Looking at the cms/block block source, those tutorials have mislead you, or you misinterpreted them.
#File: app/code/core/Mage/Cms/Block/Block.php
class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
if (!$this->_beforeToHtml()) {
return '';
}
$html = '';
if ($blockId = $this->getBlockId()) {
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load($blockId);
if (!$block->getIsActive()) {
$html = '';
} else {
$content = $block->getContent();
$processor = Mage::getModel('core/email_template_filter');
$html = $processor->filter($content);
}
}
return $html;
}
}
The block content is always loaded with ->load($blockId); -- load with one parameter always means loding by a database ID.
So, with no supported way of doing this built into the block, you'll need to look up the block ID.
$model = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier','footer_links')
->getFirstItem();
var_dump($model->getBlockId());
In the admin, when you are editing the contents of the static block, you will see a field called Identifier second from the top. Copy the value of that field, and insert it into your code. So if your Block is called contact-info in the admin, then your code will look like:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('contact-info')->toHtml() ?>
The value in that Identifier textbox in the admin is also what will be saved into the cms_block table, as you're worked out.
HTH,
JD

Resources