【问题标题】:Magento Fatal error: Call to a member function save() on a non-objectMagento 致命错误:在非对象上调用成员函数 save()
【发布时间】:2016-02-02 08:25:30
【问题描述】:

我是 magento 的新手,想通过注册用户创建一个产品广告表单。如果用户尚未添加帐单/送货地址信息,我只想通过选中复选框将电话号码添加到用户地址字段。但是,当用户选中电话复选框以将电话号码添加到默认帐单/送货地址时(如果尚未添加电话号码),则会返回错误。

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:当用户想要在添加用户的帐单/送货地址字段之前为产品做广告时,就会出现问题。所以我只想在地址字段中添加电话号码。我已检查该用户下的 address_id、customer_id 或 customer_address_id 中没有数据,该用户尚未在“sales_flat_quote_address”或“sales_flat_order_address”表中添加帐单/送货地址。

请告诉我该怎么做。

【问题讨论】:

    标签: php magento fatal-error


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
        $customerAddressId = $customer->getDefaultBilling();
          if ($customerAddressId) {
              $customAddress = Mage::getModel('customer/address')->load($customerAddressId);
              $customAddress->setTelephone($params['phone']);
          }
      

      如果 $customerAddressId 没有定义,你以后永远不会有你的 $customAddress 模型

      【讨论】:

        【解决方案3】:

        我的猜测是您的代码没有包含在您的 if 语句中。你只是在这里定义$customAddress。您需要某种 else 语句,否则您请求 save 处理不存在的东西。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-20
          • 2015-06-16
          • 2014-05-06
          • 1970-01-01
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多