为客户创建一个Magento自动识别性别的插件。插件基于Rapleaf的个性化API。Rapleaf公司提供人口统计和消费者个人数据(年龄,性别,婚姻状况,收入等),不能用作商业电子邮件。他们的合作伙伴,一些大(小)型数据公司将数据汇总并绑定到邮箱地址。你可以创建一个免费的Rapleaf账户来获取API。 让我们开始吧。 Alwayly_Autogender.xml首先,我们要在/app/etc/modules文件夹里创建Alwayly_Autogender.xml <?xml version="1.0"?> <config> <modules> <Alwayly_Autogender> <active>true</active> <codePool>local</codePool> </Alwayly_Autogender> </modules> </config> config.xml第二步是在 /app/code/local/Alwayly/Autogender/etc文件夹里创建config.xml <?xml version="1.0"?> <config> <modules> <Alwayly_Autogender> <version>0.1.0</version> </Alwayly_Autogender> </modules> <global> <models> <alwayly_autogender> <class>Alwayly_Autogender_Model</class> </alwayly_autogender> </models> <events> <customer_register_success> <observers> <alwayly_autogender_add_email> <class>alwayly_autogender/observer</class> <method>getGender</method> </alwayly_autogender_add_email> </observers> </customer_register_success> </events> </global> </config> Observer.php最后一步是/app/code/local/Alwayly/Autogender/Model文件夹里创建Observer.php <?php class Alwayly_Autogender_Model_Observer { public function getGender($observer = null) { if($observer) { try { $customer = $observer->getCustomer(); $api_key = '... your api key ...'; $client = new Zend_Http_Client(); $client->setUri('https://personalize.rapleaf.com/v4/dr?api_key=' . $api_key . '&email=' . urlencode($customer->getEmail()) . '&first=' . urlencode($customer->getFirstname()) . '&last=' . urlencode($customer->getLastname()) ); $client->setConfig(array('maxredirects' => 0, 'timeout' => 2)); $response = $client->request(); if ($response->getStatus() < 200 || $response->getStatus() >= 300) { Mage::log( sprintf("Rapleaf query failed. (status: %s), (message: %s)", $response->getStatus(), strip_tags($response->getBody())), null, 'rapleaf_api.log', false); } else { $data = json_decode($response->getBody(), true); if(array_key_exists('gender', $data)) { $customer->setGender( Mage::getResourceSingleton('customer/customer') ->getAttribute('gender') ->getSource() ->getOptionId($data['gender']) ); } } } catch (Exception $e) { Mage::log( sprintf("Exception in Rapleaf query. (message: %s)", strip_tags($e->getMessage())), null, 'rapleaf_api.log', false); } } } }(责任编辑:最模板) |