0

When I am trying to use the code

Mage::getModel('catalog/product')->loadByAttribute('sku', $sku2)->getProductUrl();

in my cart page i am getting the error as

Fatal Error: Call to a member function getProductUrl() in boolean

Can anyone kindly tell me why am I getting this.

2 Answers 2

1

You get this error, because Mage::getModel('catalog/product')->loadByAttribute('sku', $sku2) just returns false. Please check your SKU ($sku2) ... if this is invalid, you'll see this error.

I'd recommend adding a check if product exists ...

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku2);
if ($product->getId()) {
    echo $product->getProductUrl();
}
1
  • Thank you for the help I got the answer by changing the code that I have mentioned in the answer that I have written below Commented Aug 7, 2017 at 11:47
0

I resolved it by using the following line of code instead of the code that I have mentioned in the question above.

Mage::getModel('catalog/product')->load($sku2)->getProductUrl();
3
  • This does not match your question ... either looking for ID or SKU ;)
    – sv3n
    Commented Aug 7, 2017 at 11:58
  • I was getting that error when I was trying to get the URL :) Commented Aug 7, 2017 at 12:25
  • Yes, because invalid SKU in loadByAttribute ;) Change your code to the original one and use a valid SKU and it will work too :)
    – sv3n
    Commented Aug 7, 2017 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.