我是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"表
请建议我做什么。
答案 0 :(得分:3)
客户地址中的客户地址没有价值。使用以下代码添加客户地址。
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());
}
}
答案 1 :(得分:2)
$customerAddressId = $customer->getDefaultBilling();
if ($customerAddressId) {
$customAddress = Mage::getModel('customer/address')->load($customerAddressId);
$customAddress->setTelephone($params['phone']);
}
如果未定义$ customerAddressId,您将永远不会拥有$ customAddress模型
答案 2 :(得分:0)
我的猜测是你的代码没有在if
语句中。您只是在这里定义$customAddress
。你需要某种else语句,否则你在一些不存在的东西上请求save
。