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

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

我是 magento 新手,想通过注册用户创建产品广告表格。如果用户尚未添加 billing/shipping 地址信息,我只想通过选中复选框将 phone 号码添加到用户地址字段。但是,当用户选中 phone 复选框以将 phone 号码添加到默认 Billing/shipping 地址时(如果尚未添加 phone 号码),它是 return一个错误。

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

下面是我的代码;

    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());
    }
}

N.B: 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.

请建议我该怎么做。

我的猜测是您的代码没有包含在您的 if 语句中。您只是在此处定义 $customAddress。您需要某种 else 语句,否则您将请求 save 不存在的内容。

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

如果未定义 $customerAddressId,您以后将永远不会拥有 $customAddress 模型

您的客户地址中的客户地址没有值。使用以下代码添加客户地址。

 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());
     }

  }