Need if statement on error: Call to a member function on null

I’m getting the following error if there are no products on sale:

Fatal error: Uncaught Error: Call to a member function getSpecialToDate() on null on line 39

Line 39:

$dateto = ''.$_product->getSpecialToDate().'';

I’m sure I need some kind of if statement here but I don’t know.

Can someone show me how to fix this. Thank you.

You answered your own question - if you have no products, then there will be no member function available. I think the bigger question would be why you’re hitting that line of code if you have no products available.

I don’t know. I’ve been trying several things I think I need it something like this but this doesn’t work either.

<?php if ($_product->getSpecialToDate() = null ($_product->getSpecialToDate() = $datetox) ?><?php endif ?>

: )

You need a if ($_product == null) check


Fixed a typo found by @droopsnoot

1 Like

I got it. Here how:


<?php if ($_product != null) ?> <?php $datetox = ''.$_product->getSpecialToDate().''; ?>[/code]

Thank you


Surely that would be if ($_product == null) check? I know OP has solved the issue, but surely using a single equal-sign in this context would be incorrect?

You are correct. I was going quick and missed it. Fixed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.