0

I am new to php/magento to for warn you. I am making a script to grab some quick info. I am using magento 1.7.0.2. This script works fine until the telephone number is empty and then it stops. It is in a php file. see anything?

    <?php


require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

$collection = Mage::getModel('customer/customer')->getCollection();
$sz=$collection->getSize();
echo "$sz total customers</br>";
$count=0;
echo '<table cellpadding="5"> ';
echo '<tr>';
echo '<td>&nbsp;</td><td>ID </td><td>Qty </td><td>Name </td><td> Email</td> <td> Phone</td>';
echo '</tr>';
foreach ($collection as $user){
    $orders = Mage::getModel('sales/order')
                ->getCollection()
                ->addFieldToSelect('increment_id')
                ->addFieldToFilter('customer_id',$user->getId());
    if($orders->getSize() >= 5){
        $u=Mage::getModel('customer/customer')->load($user->getId());
        $tmpID=$u->getId();
        $name=$u->getName();
        $email=$u->getEmail();
        $phone='NA';
        //$pri;
        $ordercount=$orders->getSize();
        try{
            $phone=$u->getPrimaryBillingAddress()->getTelephone();
        }
       catch(Exception $e){
            echo $e->errorMessage();
            $phone='NA';
        }

        $count++;
        echo '<tr>';
        echo "<td> $count </td>";
        echo "<td> $tmpID </td>";
        echo "<td> $ordercount </td>";
        echo "<td> $name </td>";
        echo "<td> $email </td>";
        echo "<td> $phone </td>";
        echo '</tr>';
        //echo "$count-ID: $tmpID  Number Of Orders: $ordercount $name $email $phone </br>";

    }
}
echo '</table>';
?>

2 Answers 2

0

in case the customer does not have a primary billing address this line with give a fatal error: $phone=$u->getPrimaryBillingAddress()->getTelephone();.
So it does not matter if you put it inside a try-catch because fatal errors cannot be caught.
So what you need to do is to check if the getPrimaryBillingAddress actually exists. Instead of

    try{
        $phone=$u->getPrimaryBillingAddress()->getTelephone();
    }
    catch(Exception $e){
        echo $e->errorMessage();
        $phone='NA';
    }

Use

if ($u->getPrimaryBillingAddress()) {
    $phone=$u->getPrimaryBillingAddress()->getTelephone();
}
else {
    $phone = 'N/A';
}
2
  • Ok. Thanks. I understand a bit better. I am used to .Net and Java programming. Shouldn't a fatal error like this write to a log file or something? I didn't see any listings.
    – lespaul36
    Commented Oct 7, 2014 at 19:59
  • @lespaul36. There should be an entry in the php log file. You would see the error on the screen if you had 'display_errors' and error reporting set to 'on'. See this
    – Marius
    Commented Oct 8, 2014 at 5:45
0

A better way to accomplish this would be to get the info from the order info since the customer info may not be up-to date.

foreach ($collection as $user){
    $orders = Mage::getModel('sales/order')
                ->getCollection()
                ->addFieldToSelect('increment_id')
                ->addFieldToFilter('customer_id',$user->getId());

    if($orders->getSize() >= 5){

        $tmpID=$user->getId();
        $name=$user->getName();
        $email=$user->getEmail();
        $phone='NA';

        $ordercount=$orders->getSize();
        if($user->getDefaultBilling()){
           $phone = $user->getPrimaryBillingAddress()->getTelephone();
        }
        ...
1
  • Good idea, I have a lot to learn still.
    – lespaul36
    Commented Oct 7, 2014 at 20:00

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.