00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 abstract class Mage_Checkout_Block_Onepage_Abstract extends Mage_Core_Block_Template
00033 {
00034 protected $_customer;
00035 protected $_checkout;
00036 protected $_quote;
00037 protected $_countryCollection;
00038 protected $_regionCollection;
00039 protected $_addressesCollection;
00040
00041
00042
00043
00044
00045
00046 public function getCustomer()
00047 {
00048 if (empty($this->_customer)) {
00049 $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
00050 }
00051 return $this->_customer;
00052 }
00053
00054
00055
00056
00057
00058
00059 public function getCheckout()
00060 {
00061 if (empty($this->_checkout)) {
00062 $this->_checkout = Mage::getSingleton('checkout/session');
00063 }
00064 return $this->_checkout;
00065 }
00066
00067
00068
00069
00070
00071
00072 public function getQuote()
00073 {
00074 if (empty($this->_quote)) {
00075 $this->_quote = $this->getCheckout()->getQuote();
00076 }
00077 return $this->_quote;
00078 }
00079
00080 public function isCustomerLoggedIn()
00081 {
00082 return Mage::getSingleton('customer/session')->isLoggedIn();
00083 }
00084
00085 public function getCountryCollection()
00086 {
00087 if (!$this->_countryCollection) {
00088 $this->_countryCollection = Mage::getSingleton('directory/country')->getResourceCollection()
00089 ->loadByStore();
00090 }
00091 return $this->_countryCollection;
00092 }
00093
00094 public function getRegionCollection()
00095 {
00096 if (!$this->_regionCollection) {
00097 $this->_regionCollection = Mage::getModel('directory/region')->getResourceCollection()
00098 ->addCountryFilter($this->getAddress()->getCountryId())
00099 ->load();
00100 }
00101 return $this->_regionCollection;
00102 }
00103
00104 public function customerHasAddresses()
00105 {
00106 return count($this->getCustomer()->getAddresses());
00107 }
00108
00109
00110 public function getAddressesHtmlSelect($type)
00111 {
00112 if ($this->isCustomerLoggedIn()) {
00113 $options = array();
00114 foreach ($this->getCustomer()->getAddresses() as $address) {
00115 $options[] = array(
00116 'value'=>$address->getId(),
00117 'label'=>$address->format('oneline')
00118 );
00119 }
00120
00121 $addressId = $this->getAddress()->getId();
00122 if (empty($addressId)) {
00123 if ($type=='billing') {
00124 $address = $this->getCustomer()->getPrimaryBillingAddress();
00125 } else {
00126 $address = $this->getCustomer()->getPrimaryShippingAddress();
00127 }
00128 if ($address) {
00129 $addressId = $address->getId();
00130 }
00131 }
00132
00133 $select = $this->getLayout()->createBlock('core/html_select')
00134 ->setName($type.'_address_id')
00135 ->setId($type.'-address-select')
00136 ->setClass('address-select')
00137 ->setExtraParams('onchange="'.$type.'.newAddress(!this.value)"')
00138 ->setValue($addressId)
00139 ->setOptions($options);
00140
00141 $select->addOption('', Mage::helper('checkout')->__('New Address'));
00142
00143 return $select->getHtml();
00144 }
00145 return '';
00146 }
00147
00148 public function getCountryHtmlSelect($type)
00149 {
00150 $countryId = $this->getAddress()->getCountryId();
00151 if (is_null($countryId)) {
00152 $countryId = Mage::getStoreConfig('general/country/default');
00153 }
00154 $select = $this->getLayout()->createBlock('core/html_select')
00155 ->setName($type.'[country_id]')
00156 ->setId($type.':country_id')
00157 ->setTitle(Mage::helper('checkout')->__('Country'))
00158 ->setClass('validate-select')
00159 ->setValue($countryId)
00160 ->setOptions($this->getCountryOptions());
00161 if ($type === 'shipping') {
00162 $select->setExtraParams('onchange="shipping.setSameAsBilling(false);"');
00163 }
00164
00165 return $select->getHtml();
00166 }
00167
00168
00169 public function getRegionHtmlSelect($type)
00170 {
00171 $select = $this->getLayout()->createBlock('core/html_select')
00172 ->setName($type.'[region]')
00173 ->setId($type.':region')
00174 ->setTitle(Mage::helper('checkout')->__('State/Province'))
00175 ->setClass('required-entry validate-state')
00176 ->setValue($this->getAddress()->getRegionId())
00177 ->setOptions($this->getRegionCollection()->toOptionArray());
00178
00179 return $select->getHtml();
00180 }
00181
00182 public function getCountryOptions()
00183 {
00184 $options = false;
00185 $useCache = Mage::app()->useCache('config');
00186 if ($useCache) {
00187 $cacheId = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
00188 $cacheTags = array('config');
00189 if ($optionsCache = Mage::app()->loadCache($cacheId)) {
00190 $options = unserialize($optionsCache);
00191 }
00192 }
00193
00194 if ($options == false) {
00195 $options = $this->getCountryCollection()->toOptionArray();
00196 if ($useCache) {
00197 Mage::app()->saveCache(serialize($options), $cacheId, $cacheTags);
00198 }
00199 }
00200 return $options;
00201 }
00202
00203
00204
00205
00206
00207
00208 public function isShow()
00209 {
00210 return true;
00211 }
00212
00213 }