[英]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. 我是magento的新手,想由注册用户创建产品广告表格。 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. 我检查了在尚未在“ sales_flat_quote_address”或“ sales_flat_order_address”表中添加账单/送货地址的用户下,address_id,customer_id或customer_address_id中是否没有数据。
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 如果未定义$ customerAddressId,则以后再也不会拥有$ customAddress模型
My guess would be that your code isn't making it inside your if
statement. 我的猜测是您的代码没有在if
语句中包含它。 You are only defining $customAddress
here. 您仅在此处定义$customAddress
。 You need some sort of else statement, otherwise you're requesting a save
on something that doesn't exist. 您需要某种else语句,否则,您需要save
不存在的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.