简体   繁体   中英

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

I am new to magento, want to create a product advertising form by registered user. I just want to add only phone number to the user address field by checking check-box, if user not added the billing/shipping address information already. But, when user check the phone check-box for adding phone number to Default Billing/shipping 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

在此处输入图片说明

Bellow is my code;

    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 this line.**
    } catch (Exception $ex) {
        Zend_Debug::dump($ex->getMessage());
    }
}

NB: The Problem occurred when the user want to advertising product before adding user's billing/shipping address field. So I just want to add only phone number to the address field. I have checked that there is no data in address_id, customer_id or customer_address_id under that user who already not added the billing/shipping address in "sales_flat_quote_address" or "sales_flat_order_address" table.

Please, suggest me to what to do.

There is no value in customer address in your customer address. Add customer address using below code.

 if (isset($_POST['phone_check'])) { //save phone no to customer's address
     $_custom_address = array ( 'telephone' => $params['phone']);
     $customAddress = Mage::getModel('customer/address');

     $customAddress->setData($_custom_address)
      ->setCustomerId($customer->getId())
      ->setIsDefaultBilling('1')
      ->setIsDefaultShipping('1')
      ->setSaveInAddressBook('1');
     try {
        $customAddress->save();
     }
     catch (Exception $ex) {
        Zend_Debug::dump($ex->getMessage());
     }

  }
  $customerAddressId = $customer->getDefaultBilling();
    if ($customerAddressId) {
        $customAddress = Mage::getModel('customer/address')->load($customerAddressId);
        $customAddress->setTelephone($params['phone']);
    }

if $customerAddressId is not defined, you will never have your $customAddress model later

My guess would be that your code isn't making it inside your if statement. You are only defining $customAddress here. You need some sort of else statement, otherwise you're requesting a save on something that doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM