How to fix: Function name must be a string

I’m getting a Fatal error: Uncaught Error: Function name must be a string after upgrading to php7

Here is the line it points to:

if (function_exists('IscustomerEmailExists' == true)($email, $websiteId = null)){

How can I fix this?

function_exists takes a string as a string as a parameter, but you’re passing in a boolean expression.

It seems I need it to check if the email exists or not? I’m not sure where I’m using a string?

Here’s my code:

<?php
$websiteId = Mage::app()->getWebsite()->getId();
$email = Mage::getSingleton('checkout/session')->getQuote()->getCustomerEmail();


if (function_exists('IscustomerEmailExists' == true)($email, $websiteId = null)){

    $customer = Mage::getModel('customer/customer');
    if ($websiteId) {
        $customer->setWebsiteId($websiteId);
    }
    $customer->loadByEmail($email);
    if ($customer->getId()) {
        return $customer->getId();
    }
    return false;
}
$cust_exist = IscustomerEmailExists($email,$websiteId);
if($cust_exist){
    echo "";
}
else{
     echo "";
}
?>
<?php
$customer_id = $cust_exist;
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$customer_id);                        
$_orderCnt = $_orders->count(); //orders count
?>
<?php if (!$customer_id == null): ?>
<?php if ($_orderCnt == 0): ?>
<?php
$discountcode = uniqid(); 
$customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
$rule = Mage::getModel('salesrule/rule');
$rule->setName('Next Order Discount')                                             
    ->setDescription('Discount for customer on their second order.')
    ->setFromDate(date('Y-m-d', strtotime("-1 days")))
    ->setToDate(date('Y-m-d', strtotime("+30 days")))	
    ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
    ->setCouponCode($discountcode)
    ->setUsesPerCustomer(1)
    ->setUsesPerCoupon(1)
    ->setCustomerGroupIds($customerGroupIds)
    ->setIsActive(1)
    ->setConditionsSerialized('')
    ->setActionsSerialized('')
    ->setStopRulesProcessing(0)
    ->setIsAdvanced(1)
    ->setProductIds('')
    ->setSortOrder(0)
    ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION)
    ->setDiscountAmount(10)
    ->setDiscountQty(1)
    ->setDiscountStep(0)
    ->setSimpleFreeShipping('0')
    ->setApplyToShipping('0')
    ->setIsRss(0)
    ->setWebsiteIds(array(1))
    ->setStoreLabels(array('Next Order Discount'));
/* $productFoundCondition = Mage::getModel('salesrule/rule_condition_product_found')
    ->setType('salesrule/rule_condition_product_found')
    ->setValue(1)                   ->setAggregator('all');      
$attributeSetCondition = Mage::getModel('salesrule/rule_condition_product')
    ->setType('salesrule/rule_condition_product')
    ->setAttribute('attribute_set_id')
    ->setOperator('==')
    ->setValue(1);
$productFoundCondition->addCondition($attributeSetCondition);
$rule->getConditions()->addCondition($productFoundCondition);
$rule->getActions()->addCondition($attributeSetCondition); */
$rule->save();
?>
<?php endif ?>
<?php endif ?>

Oh sorry, that wasn’t actually causing the error (although it’s most certainly not what you want to achieve either). function_exists also returns a boolean, which you’re trying to call then. Examine that line step by step:

$argument = 'IscustomerEmailExists' == true;         // $argument === true
$functionExists = function_exists($argument);        // function_exists(true) === false
$result = $functionExists($email, $wbsiteId = null); // false($email, $wbsiteId = null) ... Fatal error

You can call function by it’s string name though, like e.g.


function fn() { 
  // Does something
}

$fnName = 'fn';
$fnName();

This is where PHP says that a function name must be string when the variable is a boolean.

I’m sorry I don’t know php good enough to understand what you mean.

What is the value of this expression:

'IscustomerEmailExists' == true

If you’re not sure, print the value with var_dump.

I done it this way, not sure if it is correct:

<?php
$websiteId = Mage::app()->getWebsite()->getId();
$email = Mage::getSingleton('checkout/session')->getQuote()->getCustomerEmail();
$IscustomerEmailExists ='IscustomerEmailExists' == true;
var_dump($IscustomerEmailExists);
if (function_exists('IscustomerEmailExists' == true)($email, $websiteId = null)){

    $customer = Mage::getModel('customer/customer');
    if ($websiteId) {
        $customer->setWebsiteId($websiteId);
    }
    $customer->loadByEmail($email);
    if ($customer->getId()) {
        return $customer->getId();
    }
    return false;
}
$cust_exist = IscustomerEmailExists($email,$websiteId);
if($cust_exist){
    echo "";
}
else{
     echo "";
}
?>

The result: bool(true)

Right, and what is the value of

function_exists('IscustomerEmailExists' == true)

bool(false)

And the boolean false is not a valid function name. So this

function_exists('IscustomerEmailExists' == true)($email, $websiteId = null)

will throw an error. If you want to call that function, you have to do so separately:

if (function_exists('IscustomerEmailExists')) {
  IscustomerEmailExists($email, $websiteId);
}

… or if you wanted to declare it if it does not exist:

if (!function_exists('IscustomerEmailExists')) {
  function IscustomerEmailExists($email, $websiteId = null) {
    // Do something
  }
}
1 Like

Those fixed the error but there were other issues. I figured out another way to get the information. Much simpler and accurate. Here’s what I did:

<?php
$email = Mage::getSingleton('checkout/session')->getQuote()->getCustomerEmail();
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$email);
$_orderCnt = $_orders->count();
?>
<?php if ($_orderCnt == 1): ?>
<?php
$discountcode = uniqid(); 
$customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
$rule = Mage::getModel('salesrule/rule');
$rule->setName('10% Off')
    ->setDescription('Discount for customer on their second order.')
    ->setFromDate(date('Y-m-d', strtotime("-1 days")))
    ->setToDate(date('Y-m-d', strtotime("+30 days")))	
    ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
    ->setCouponCode($discountcode)
    ->setUsesPerCustomer(1)
    ->setUsesPerCoupon(1)
    ->setCustomerGroupIds($customerGroupIds)
    ->setIsActive(1)
    ->setConditionsSerialized('')
    ->setActionsSerialized('')
    ->setStopRulesProcessing(0)
    ->setIsAdvanced(1)
    ->setProductIds('')
    ->setSortOrder(0)
    ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION)
    ->setDiscountAmount(10)
    ->setDiscountQty(1)
    ->setDiscountStep(0)
    ->setSimpleFreeShipping('0')
    ->setApplyToShipping('0')
    ->setIsRss(0)
    ->setWebsiteIds(array(1))
    ->setStoreLabels(array('Next Order Discount'));
/* $productFoundCondition = Mage::getModel('salesrule/rule_condition_product_found')
    ->setType('salesrule/rule_condition_product_found')
    ->setValue(1)                   ->setAggregator('all');      
$attributeSetCondition = Mage::getModel('salesrule/rule_condition_product')
    ->setType('salesrule/rule_condition_product')
    ->setAttribute('attribute_set_id')
    ->setOperator('==')
    ->setValue(1);
$productFoundCondition->addCondition($attributeSetCondition);
$rule->getConditions()->addCondition($productFoundCondition);
$rule->getActions()->addCondition($attributeSetCondition); */
$rule->save();
?>
<a href="<?php echo $this->getUrl('') ?>applycoupon/?code=<?php echo $discountcode; ?>&return_url=<?php echo Mage::getBaseUrl(); ?>"><?php echo $discountcode; ?></a> 
<?php endif ?>

Thanks

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