Error: "Call to member function on a non-object..." in Magento - php

When I place the following code at the bottom of my new_grid.phtml template file, it works fine, without any any errors. But when it is placed at the top of the file, I get a fatal error: "Call to member function getRelatedLinkCollection() on a non-object in C:\wamp\www\MYSITE\magento\app\design\frontend\MYSITE\default\template\catalog\product\widget\new\content\new_grid.phtml on line 32.
<?php
foreach ($_product->getRelatedLinkCollection() as $link) {
$dats= $link->getLinkedProductId();
}
?>
I would like to get this code working specifically at the top of the file, because I'd like to output generated HTML in a particular structure. Any idea why I'm getting this error and what I can do to correct it?
Thank you!

Can you post the contents of your new_grid.phtml file?
Otherwise, make sure the foreach loop is immediately after the line which contains $_product =

Related

Unable to save the last xml file php

I am fairly new to php, and I have written code to work with the amazon API. When I request information from the API, I receive it, but am unable to sort through the XML. Here is the error:
Fatal error: Call to a member function children() on null in J:\XAMPP\htdocs\Phillip\src\MarketplaceWebServiceProducts\Samples\csv_prep.php on line 117
Here is the code:
if(is_array($xmlFiles)){
foreach($xmlFiles as $xmlFile){
$xml = simplexml_load_file($xmlFile);
foreach($xml->GetMatchingProductForIdResult as $items) {
//Line 117 ->
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount) !== False) {
$amount = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount
}else{
$amount = '0.00';
}
}
}
}
The tag in the XML that I am trying to get the value of looks like this:
<ns2:amount>9.99</ns2:amount>
It is in the same place as it says in the code. I only have this problem with large files and I am not sure what is happening. If someone could help, I would greatly appreciate it. Thanks in advance!
I'm not certain, but based on the error message, it looks like somewhere in the $items->Products->Product->AttributeSets hierarchy something you're specifying does not exist.
Do tests to see if $items or $items->Products or $items->Products->Product or $items->Products->Product->AttributeSets and whichever one fails, print out that fact and call exit;.
You could do a print_r on $items to help as well.

Magento - screen goes blank after saving products

When I am saving simple products from the magento backend, the screen goes blank.
The error I am getting is
Fatal error: Call to a member function getStores() on a non-object in /home/public_html/app/code/core/Mage/Catalog/Model/Url.php on line 155
Any help would be much appreciated.
Thanks
Your Mage::getResourceModel('catalog/url') returns a non-object - check whether it is missing or rewritten by any 3-rd party module.
Latter can be done by searching the *.php files in the directory for this string: "extends Mage_Catalog_Model_Resource_Url"

wordpress queries in mytheme/update.php

In my wordpress theme, I created a file called update.php. I can access this URL by
http://mydomain.com/wp-content/themes/mytheme/update.php
Here is the code inside update.php
global $wpdb;
// do some more stuff here
$wpdb->update( 'twitter_followers', array('count' => $followers), array('id' => '1') );
echo 'done';
When I go to this page I get this error.
PHP Fatal error: Call to a member function update() on a non-object in /var/www/vhosts/mydomain.com/subdomains/mytheme/httpdocs/wp-content/themes/mytheme/update.php on line 34
Any ideas on how I can fix this?
Also note, I did created twitter_followers table in my database.
The issue is that you don't have $wpdb yet, since your update.php is not part of wordpress. When you go to update.php wordpress is never loaded, so you get the non-object error. Try adding this line to the top of your update.php file, obviously changing the path:
require_once("/path/to/wordpress/wp-load.php");
the path will probably look something like this in a normal installation:
require_once("../../../../wp-load.php");

PHP Call to undefined function

I am trying to call a function from another function. I get an error:
Fatal error: Call to undefined function getInitialInformation()
in controller.php on line 24
controller.php file:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
Verified that the require_once works, and the file exists in the specified location.
Verified that the function exists in the file.
I am not able to figure this out. Am I missing something here?
How to reproduce the error, and how to fix it:
Put this code in a file called p.php:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
Solution: use $this->salt(); instead of salt();
So do it like this instead:
<?php
class yoyo{
function salt(){
}
function pepper(){
$this->salt();
}
}
$y = new yoyo();
$y->pepper();
?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
Searched for the php file with the function definition in it. Verified that the file exists.
Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
Verified that the filename is spelled correctly in the require statement.
Echoed a word in the included file, to see if it has been properly included.
Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Many times the problem comes because php does not support short open tags in php.ini file, i.e:
<?
phpinfo();
?>
You must use:
<?php
phpinfo();
?>
Your function is probably in a different namespace than the one you're calling it from.
http://php.net/manual/en/language.namespaces.basics.php
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I include or require_one works on all calls except in a file.
The problem of this file was the code < ?php ? > At the beginning and end of the text.
It was a script that was only < ?, and in that version of apache that was running did not work
This is obviously not the case in this Q,
but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:
I was porting code from JS to PHP and ended up having a class with some public method.
The code that was calling the class (being code that originated from JS) looked something like:
$myObject.method(...)
this is wrong because in PHP it should look like this:
$myObject->method(...)
and it also resulted with "PHP Call to undefined function".
change to use -> and the problem was solved.
Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.

How to Fetch Smarty Variables in PHP File?

I am display a smarty variable inside a php code, this is because i would like to compare something from Smarty into a PHP file.
Example:
<?php
$ThisIsTheSmartyVariable
If ($ThisIsTheSmartyVariable == Something) {
Do Something
}
?>
This is the main idea but i dont understand it why its not working.
When i am trying to fetch all the variables its giving me this output:
Fatal error: Call to undefined function logactivity() in public_html/includes/smarty/Smarty.class.php on line 1094
Let me know.

Categories