我正在自定义产品视图页面,我需要显示用户的名字.如何访问当前用户的帐户信息(如果他已登录)以获取姓名等?
在"app/code/core/Mage/Page/Block/Html/Header.php"下找到:
public function getWelcome() { if (empty($this->_data['welcome'])) { if (Mage::app()->isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) { $this->_data['welcome'] = $this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getName()); } else { $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome'); } } return $this->_data['welcome']; }
所以它看起来Mage::getSingleton('customer/session')->getCustomer()
会让你当前登录的客户;)
要获取当前登录的管理员:
Mage::getSingleton('admin/session')->getUser();
看一下辅助类:Mage_Customer_Helper_Data
要简单地获取客户名称,您可以编写以下代码: -
$customerName = Mage::helper('customer')->getCustomerName();
有关客户的实体ID,网站ID,电子邮件等的更多信息,您可以使用getCustomer功能.以下代码显示了您可以从中获得的内容: -
echo ""; print_r(Mage::helper('customer')->getCustomer()->getData()); echo "";
从帮助程序类,您还可以获取有关客户登录URL,注册URL,注销URL等的信息.
从帮助程序类中的isLoggedIn函数,您还可以检查客户是否已登录.
您可以通过以下方式从会话中获取当前登录客户名称:
$customer = Mage::getSingleton('customer/session')->getCustomer();
这将返回当前登录客户的客户详细信息.
现在您可以使用获取客户名称 getName()
echo $customer->getName();
电子邮件使用此代码
$email=$this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getEmail()); echo $email;
$customer = Mage::getSingleton('customer/session')->getCustomer(); $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); $address = Mage::getModel('customer/address')->load($customerAddressId); $fullname = $customer->getName(); $firstname = $customer->getFirstname(); $lastname = $customer->getLastname(); $email = $customer->getEmail(); $taxvat = $customer->getTaxvat(); $tele = $customer->getTelephone(); $telephone = $address->getTelephone(); $street = $address->getStreet(); $City = $address->getCity(); $region = $address->getRegion(); $postcode = $address->getPostcode();
获取客户默认结算地址