views:

1027

answers:

3

I have come across a bizarre error in the Magento shop I'm developing and despite my inquiries online, it appears no one else has ever seen this exact error under the same circumstances. Lemme 'splain.

The full text of the error message is this:

Fatal error: Call to a member function getSku() on a non-object in /path/on/server/app/code/core/Mage/Catalog/Model/Product/Option/Type/Select.php on line 221.

Now, others have gotten this error message--it was addressed and supposedly fixed in the 1.3.1 roadmap (http://www.magentocommerce.com/roadmap/release/1.3.1). However, the circumstances of those other error messages were where they tried to add an item to the cart--if the item had custom options, it would loop to this error message.

My situation is that I have a SIMPLE item--not bundled or configurable--without any custom options. I can add it to the cart without any trouble. But if I run through the entire checkout procedure, upon placing the order, the error message appears on a white screen. The URL in the browser shows me I’m on the checkout success page.

AND, the order appears to go through perfectly, getting registered by both Magento AND Authorize.net.

I’ve tried debugging the error as far as I can go, but this one’s got me stumped.

For reference, I’m in Magento 1.3.2.4. When I first received the error I reinstalled all the core files and was still able to replicate the error.

I’m going to continue to test, but if anyone has ANY bright ideas about why this is happening, I’d love to hear your thoughts. I’m so close to launch and this thing could put the kibosh on the whole thing.

+2  A: 

I've had this error before and was also not getting any hits on how to solve it. So, I had to modify the core files to fix the error message. The problem is Mage_Catalog_Model_Product_Option::getValueById() can return a null value BUT Catalog/Model/Product/Option/Type/Select.php is NOT checking for this possibility.

Here's the solution that worked for me:

Open app/code/core/Mage/Catalog/Model/Product/Option/Type/Select.php

Change line 121 from:

$result = $option->getValueById($optionValue)->getSku();

To:

$o= $option->getValueById($optionValue);
$result = is_object($o) ? $o->getSku() : null;

I always hate changing core files but when there's a bug there's not much else I can do!

pygorex1
Going to give your solution a go. I'll let you know if it works! Thanks for this, regardless!
f8xmulder
If this works, remember that instead of changing core files you can copy this file into "local" (`app/code/local/Mage/Catalog/Model/Product/Option/Type/Select.php`) so that upgrades will not kill it.
philfreo
Well I implemented your solution, but I've been unable to test it out, as I began receiving redirect loops upon Checkout success. Which may or may not have anything to do with SSL switchover.I've got this bookmarked, so as soon as I can I'll come back and let you know how things go.
f8xmulder
I was finally able to implement this solution. And it seemed to work. However, another one popped up in it's place:Fatal error: Call to a member function getPrice() on a non-object in /home/basepath/app/code/local/Mage/Catalog/Model/Product/Option/Type/Select.php on line 192So there's something weird going on with the product id, or some value that it's trying to find but isn't.
f8xmulder
Thanks @pygorex1 for one half of the equation. Your fix eliminated the bug but I was still seeing problems with the checkout. Looks like a corrupt user account with funky session data was bolluxing up getPrice() somehow, causing another fatal error. Weird, huh?
f8xmulder
A: 

Got same problem. Backtraced it and identified it happens for me when editing orders in admin and that order contains at least one product with an individual option that was removed since the order has been placed.

Fixed three files then so that product is simply removed when editing such an order - all modifications working in "local" scope so core files left untouched:

1. app/code/local/Mage/Catalog/Model/Product/Option/Type/Select.php:221

(search)

$result = $option->getValueById($optionValue)->getSku();

(prepend)

/* hotfix for - PHP Fatal error:  Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
if (is_null($option->getValueById($optionValue))) { throw new Exception('missing product option'); }

2. app/code/local/Mage/Sales/Model/Quote.php:695

(search)

$item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());

(replace)

/* hotfix for - PHP Fatal error:  Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
try {
$item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
} catch ( Exception $e ) {
if ($e->getMessage()=='missing product option') { return null; }
throw new Exception($e->getMessage());
}

3. app/code/local/Mage/Adminhtml/Model/Sales/Order/Create.php:288

(search)

if (is_string($item)) {
return $item;
}

(replace)

if (is_string($item)) {
return $item;
/* hotfix for - PHP Fatal error:  Call to a member function getSku() on a non-object - occurs on admin order edit if a product option has been removed */
} elseif (is_null($item)) {
return $this;
}
waRhawK
A: 

I followed that steps that pygorex1 posted but also replaced lines 191-195 in the Select.php file, because I was still getting another similar error about the getPrice(). I have Magento ver. 1.3.2.4.

Original code from lines 191-195:

$result = $this->_getChargableOptionPrice(
$option->getValueById($optionValue)->getPrice(),
$option->getValueById($optionValue)->getPriceType() == 'percent',
$basePrice
);

Here is the code I created to replace lines 191-195:

$z= $option->getValueById($optionValue);
$result = is_object($z) ? $z ->getPrice() : null;

$zz = $option->getValueById($optionValue);
$result = is_object($zz) ? $zz ->getPriceType() == 'percent' : $basePrice;

FYI - I am NOT a PHP programmer. I just happened to figure out how to rework the code to make fix this problem based on pygorex1's code. So, there is a good chance I didn't write it correctly. However, it did fix the problem for me (which I am rather proud of :)