3

I am new to magento, I have a custom module for product advertising by registered user. When user check the phone check-box for adding phone number to Default Billing address (if not added the phone number already), it's return an error.

Fatal error: Call to a member function save() on a non-object

enter image description here

My code is,

if (isset($_POST['request_flag'])):
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$firstName = $customer->getFirstname();
$lastName = $customer->getLastname();
$fullName = $firstName . " " . $lastName;
$sellerId = $customer->getentity_id();

$params = $this->getRequest()->getParams();
$newProduct = new Mage_Catalog_Model_Product();

if (isset($_POST['phone_check'])) { //save phone no to customer's address
    $customerAddressId = $customer->getDefaultBilling();
    if ($customerAddressId) {
        $customAddress = Mage::getModel('customer/address')->load($customerAddressId);
        $customAddress->setTelephone($params['phone']);
    }
    try {
        $customAddress->save(); //**Error occurred here.**
    } catch (Exception $ex) {
        Zend_Debug::dump($ex->getMessage());
    }
}

Please, any one tell me where I went wrong? or suggest me to what to do.

Thanks.

3
  • Is it coming in "if ($customerAddressId) { }" in this condition ? Oct 26, 2015 at 6:44
  • Yes, it is in "if ($customerAddressId) { }"
    – Pk Ray
    Oct 26, 2015 at 6:51
  • @PkRay added answer Oct 26, 2015 at 7:01

2 Answers 2

2

Try this

  if (isset($_POST['request_flag'])){
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $email = $customer->getEmail();
    $firstName = $customer->getFirstname();
    $lastName = $customer->getLastname();
    $fullName = $firstName . " " . $lastName;
    $sellerId = $customer->getentity_id();

    $params = $this->getRequest()->getParams();
    $newProduct = new Mage_Catalog_Model_Product();

    if (isset($_POST['phone_check'])) { //save phone no to customer's address
       try {
           $customerAddressId = $customer->getDefaultBilling();
           if ($customerAddressId) {
                $customAddress = Mage::getModel('customer/address')->load($customerAddressId);
                $customAddress->setTelephone($params['phone']);
                $customAddress->save(); //**Error occurred here.**
           }

        } catch (Exception $ex) {
            Zend_Debug::dump($ex->getMessage());
        }
    }
  }
3
  • Thanks. Your code solved the Fatal error problem but the checked phone no not added to customer's Billing address. Please suggest, how can I add phone number to the billing address when the check-box is checked.
    – Pk Ray
    Oct 26, 2015 at 7:17
  • Are you getting address in $customAddress = Mage::getModel('customer/address')->load($customerAddressId); ? Oct 26, 2015 at 7:20
  • I've checked that, $customAddress = Mage::getModel('customer/address')->load($customerAddressId); create Fatal error when the code looks like $customerAddressId = $customer->getDefaultBilling(); if ($customerAddressId) { $customAddress = Mage::getModel('customer/address')->load($customerAddressId); } that the echo $customAddress; show fatal error. But instead of that it show the user address if user already added, $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); otherwise no data in array.
    – Pk Ray
    Oct 26, 2015 at 12:19
2

Try below code in your if condition.

if (isset($_POST['phone_check'])) { //save phone no to customer's address
   //$customerAddressId = $customer->getDefaultBilling();
   $customerId  = Mage::getSingleton('customer/session')->getCustomer()->getId();
    if ($customerId) {
        $customdata = Mage::getModel("customer/customer")->load($customerId );
        $customAddress= Mage::getModel('customer/address')->load($customdata->default_billing);
        $customAddress->setTelephone($params['phone']);

    }
     try{
         $customAddress->save(); //**Error occurred here.**
     }
     catch (Exception $e) {
        Zend_Debug::dump($e->getMessage());
     }
}
1
  • You forgot to load the object in your code. In this case, Magento will simply try to create a new address object which will fail as there are many required fields not present, including customer itself.
    – Prateek
    Oct 26, 2015 at 11:47

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.