DIY:magento 自动用户信息管理

1. 创建自己的model 

    1.1 创建自己目录框架     

%~d0 cd %~p0 mkdir app\code\local\Bruce mkdir app\code\local\Bruce\Account mkdir app\code\local\Bruce\Account\Block mkdir app\code\local\Bruce\Account\controllers mkdir app\code\local\Bruce\Account\etc mkdir app\code\local\Bruce\Account\Helper mkdir app\code\local\Bruce\Account\Model mkdir app\code\local\Bruce\Account\sql

pause 

 1.2  生成model自己的xml注册文件

app/code/local/Bruce/Account/etc/config.xml
<config>
<modules>
<Bruce_Account>
<version>0.1.0</version>
</Bruce_Account>
</modules>
</config>

 1.3 创建一个系统配置文件来激活这个模块
 app/etc/modules/Bruce_Account.xml
<config>
<modules>
<Bruce_Account>
<active>true</active>
<codePool>local</codePool>
</Bruce_Account>
</modules>
 1.4  配置路由

  在app/code/local/Bruce/Account/etc/config.xml 增加如下代码

<config>
...
<frontend>
<routers>
<account>
<use>standard</use>
<args>
<module>Bruce_Account</module>
<frontName>account</frontName>
</args>
</account>
</routers>
</frontend>
...
</config>

2. 增加控制器

2.1 增加文件addressController.php

2.2  编辑内容

 

<?php
class Bruce_Account_addressController extends Mage_Core_Controller_Front_Action {
	public function addAddressAction() {
		$email = "----";  //email='1@1.com';
		if (array_key_exists ( 'email', $_REQUEST )) {
			$email = $_REQUEST ['email'];
		}	
		//localhost/jia1v3/account/address/addAddress?email=1@1.com&firstname=web1&street=streetestweb&city=xian&postcode=3434&telephone=12312321
		$firstname = array_key_exists ( 'firstname', $_REQUEST ) ?  $_REQUEST ['firstname'] : "";
		$lastname = array_key_exists ( 'lastname', $_REQUEST ) ?  $_REQUEST ['lastname'] : "";		
		$street = array_key_exists ( 'street', $_REQUEST ) ?  $_REQUEST ['street'] : "";		
		$city = array_key_exists ( 'city', $_REQUEST ) ?  $_REQUEST ['city'] : "";
		$region = array_key_exists ( 'region', $_REQUEST ) ?  $_REQUEST ['region'] : "";		
		$postcode = array_key_exists ( 'postcode', $_REQUEST ) ?  $_REQUEST ['postcode'] : "";
		$telephone = array_key_exists ( 'telephone', $_REQUEST ) ?  $_REQUEST ['telephone'] : "";		 
		$_custom_address = array (
				'firstname' => $firstname,
				'lastname' => $lastname,
				'street' => array (
						'0' => $street,
						'1' => '' 
				),
				'city' => $city,
				'region_id' => '',
				'region' => $region,
				'postcode' => $postcode,
				'country_id' => 'CN', 
				'telephone' => $telephone
		);
		$customAddress = Mage::getModel('customer/address');
		$customer = Mage::getModel ( 'customer/customer' )
			->setWebsiteId ( Mage::app ()->getStore ()->getWebsiteId () )
			->LoadByEmail ( $email );
	 
		if (! $customer->getId ()) {
			$arrayData = array (
				"result" => "true",
				"resultid" => -1,
					"id" =>-1
			);
			$jsonresult = json_encode ( $arrayData );
			return $jsonresult;
		}
		
		$customAddress->setCustomerId ( $customer->getId () );
		$customAddress->setData ( $_custom_address )
			->setCustomerId ( $customer->getId () )
		     ->setIsDefaultBilling ( '0' )
		     ->setIsDefaultShipping ( '1' )
		     ->setSaveInAddressBook ( '1' );
		try {
			$customAddress->save ();
		} catch ( Exception $ex ) {
			$arrayData = array (
				"result" => "true",
				"resultid" => -2,
					"id" =>-1
			);
			$jsonresult = json_encode ( $arrayData );
			echo $jsonresult;
		}
 
		$arrayData = array (
				"result" => "true",
				"resultid" => 0,
				"id" =>$customAddress->getId()
		);
		$jsonresult = json_encode ( $arrayData );
		echo $jsonresult;
	}
	public function getAllAddressAction() {
		$email = "----";  //email='1@1.com';
		if (array_key_exists ( 'email', $_REQUEST )) {
			$email = $_REQUEST ['email'];
		}
		$customer = Mage::getModel ( 'customer/customer' )
		->setWebsiteId ( Mage::app ()->getStore ()->getWebsiteId () )
		->LoadByEmail ( $email );
		if (! $customer->getId ()) {
			$arrayData = array (
					"result" => "false",
					"primary" => null,
					'test' =>null,
					"Additional" => null
			);
			$jsonresult = json_encode ( $arrayData );
			print "$jsonresult";
			return;
		}		
		$primarycustomAddress = $customer->getPrimaryShippingAddress();
		if ($primarycustomAddress) {
 
// 			$primarycustomAddress->setIsPrimaryShipping(1);
// 			print_r($primarycustomAddress->getData());
			$primary = array(
					'id' => $primarycustomAddress->getId(),
						'username' =>$primarycustomAddress->getName(),
						'street' =>$primarycustomAddress->getStreetFull(),
						'Region' =>$primarycustomAddress->getRegion(),
					'getRegionCode' =>$primarycustomAddress->getRegionCode(),
					'getCountry' =>$primarycustomAddress->getCountry(),
					'postcode' =>$primarycustomAddress->getData('postcode'),
					'telephone' =>$primarycustomAddress->getData('telephone')
					);
		}
		
		$allcustomAddress =  $customer->getAdditionalAddresses();
		$Additionas =  array();
// 		print "<br>getAdditional has :".count($allcustomAddress)." address.<br>";
		foreach($allcustomAddress as $k=>$AdditonAddress){
 
			$aadditon = array(
					'id' => $AdditonAddress->getId(),
					'username' =>$AdditonAddress->getName(),
					'street' =>$AdditonAddress->getStreetFull(),
					'Region' =>$AdditonAddress->getRegion(),
					'getRegionCode' =>$AdditonAddress->getRegionCode(),
					'getCountry' =>$AdditonAddress->getCountry(),
					'postcode' =>$AdditonAddress->getData('postcode'),
					'telephone' =>$AdditonAddress->getData('telephone'),
		 
			);
			$Additionas [] = $aadditon;
		}
		
		$tesarray = array('11'=>'value1','33'=>'value133','222'=>'value1');
		$arrayData = array (
				"result" => "true",
				"primary" => $primary,
				'test' =>$tesarray,
				"Additional" => $Additionas
		);
		$jsonresult = json_encode ( $arrayData );
		print "$jsonresult";
		//return $jsonresult;
		
	}
 
}
?>


 

2.3 验证

http://localhost/account/address/getAllAddress?email=1@1.com

得到 json数据

{"result":"true","primary":{"id":"18","username":"web2 ","street":"streetestweb","Region":null,"getRegionCode":null,"getCountry":"CN","postcode":"3434","telephone":"12312321"},"test":{"11":"value1","33":"value133","222":"value1"},"Additional":[{"id":"9","username":"bruceaaa 2342323","street":"billi\u8857\u9053\u5730\u5740:afafasfa \nbilli\u8857\u9053\u5730\u57402:afafasfa","Region":"\u897f\u5b89","getRegionCode":"CN-SH","getCountry":"CN","postcode":"324324242","telephone":"18049526960"},{"id":"10","username":"bruceaaa 2342342","street":"23423\n23423","Region":"\u897f\u5b89","getRegionCode":"CN-SH","getCountry":"CN","postcode":"23423423","telephone":"32423432"},{"id":"11","username":"bruceaaa 33333","street":"33333\n3333333","Region":"\u897f\u5b89","getRegionCode":"CN-SH","getCountry":"CN","postcode":null,"telephone":"33333"},{"id":"12","username":"Branko Ajzele","street":"Sample address part1\nSample address part2","Region":null,"getRegionCode":null,"getCountry":"HR","postcode":"31000","telephone":"0038531555444"},{"id":"14","username":"web1 ","street":"streetestweb","Region":null,"getRegionCode":null,"getCountry":"CN","postcode":"31000","telephone":"12312321"},{"id":"15","username":"web2 ","street":"streetestweb","Region":null,"getRegionCode":null,"getCountry":"CN","postcode":"3434","telephone":"12312321"},{"id":"16","username":"web2 ","street":"streetestweb","Region":null,"getRegionCode":null,"getCountry":"CN","postcode":"3434","telephone":"12312321"},{"id":"17","username":"web2 ","street":"streetestweb","Region":null,"getRegionCode":null,"getCountry":"CN","postcode":"3434","telephone":"12312321"}]}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值