【问题标题】:magento and sugar CRM integration issuemagento 和 Sugar CRM 集成问题
【发布时间】:2013-04-03 06:18:22
【问题描述】:

我是 magento 的新手。实际上我想将 magento 与 Sugar crm 集成 我休闲这个指南http://www.magentocommerce.com/wiki/3_-_store_setup_and_management/magento-sugarcrm_integration_guide

但我无法通过此代码获取客户的名字、姓氏和电子邮件。

当客户在 magento 中创建帐户时,潜在客户会在 CRM 中创建但字段为空。 我怎样才能得到客户的名字姓氏和电子邮件。

提前致谢 这是我的代码,但仍然无法获取客户信息。 公共函数 createPostAction() {

    $session = $this->_getSession();




    if ($session->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session->setEscapeMessages(true); // prevent XSS injection in user input
    if ($this->getRequest()->isPost()) {
        $errors = array();

        if (!$customer = Mage::registry('current_customer')) {
            $customer = Mage::getModel('customer/customer')->setId(null);
        }

        /* @var $customerForm Mage_Customer_Model_Form */
      $customerForm= Mage::getModel('customer/form');
       $customerForm->setFormCode('customer_account_create')
            ->setEntity($customer);

       $customerData= $customerForm->extractData($this->getRequest());

        if ($this->getRequest()->getParam('is_subscribed', false)) {
            $customer->setIsSubscribed(1);
        }


        /**
         * Initialize customer group id
         */
        $customer->getGroupId();

        if ($this->getRequest()->getPost('create_address')) {
            /* @var $address Mage_Customer_Model_Address */
            $address = Mage::getModel('customer/address');
            /* @var $addressForm Mage_Customer_Model_Form */
            $addressForm = Mage::getModel('customer/form');
            $addressForm->setFormCode('customer_register_address')
                ->setEntity($address);

            $addressData    = $addressForm->extractData($this->getRequest(), 'address', false);
            $addressErrors  = $addressForm->validateData($addressData);
            if ($addressErrors === true) {
                $address->setId(null)
                    ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
                    ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
                $addressForm->compactData($addressData);
                $customer->addAddress($address);

                $addressErrors = $address->validate();
                if (is_array($addressErrors)) {
                    $errors = array_merge($errors, $addressErrors);
                }
            } else {
                $errors = array_merge($errors, $addressErrors);
            }




        }
        foreach (Mage::getConfig()->getFieldset('customer_account') as $code=>$node) {
                if ($node->is('create') && isset($data[$code])) {
                    switch($code) {
                        case 'email':
                            $data[$code] = trim($data[$code]);
                            echo $email = $data[$code];
                        break;
                        case 'firstname':
                           echo $firstname = $data[$code];
                        break;
                        case 'lastname':
                           echo $lastname = $data[$code];
                        break;
                    }               
                }
            }
        $options= array( "location"=>'http://addres.com/soap.php',"uri"=>'http://addres.com/',"trace"=>1 );
        $user_auth=array ("user_name"=>'name',
        "password"=>md5('password'),
        "version"=>'6.2.4'
        );


        $client=new SoapClient(Null,$options);
        $response=$client->login($user_auth,'name');
        $session_id=$response->id;

        $response=$client->set_entry($session_id,'Leads',array(
        array("name"=>'first_name',"value"=>$firstname),
        array("name"=>'last_name',"value"=>$lastname),
        array("name"=>'email',"value"=>$email)));            


        try {
            $customerErrors = $customerForm->validateData($customerData);
            if ($customerErrors !== true) {
                $errors = array_merge($customerErrors, $errors);
            } else {
                $customerForm->compactData($customerData);
                $customer->setPassword($this->getRequest()->getPost('password'));
                $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
                $customerErrors = $customer->validate();
                if (is_array($customerErrors)) {
                    $errors = array_merge($customerErrors, $errors);
                }
            }

            $validationResult = count($errors) == 0;

            if (true === $validationResult) {
                $customer->save();



                Mage::dispatchEvent('customer_register_success',
                    array('account_controller' => $this, 'customer' => $customer)
                );

                if ($customer->isConfirmationRequired()) {
                    $customer->sendNewAccountEmail(
                        'confirmation',
                        $session->getBeforeAuthUrl(),
                        Mage::app()->getStore()->getId()
                    );
                    $session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
                    $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
                    return;
                } else {
                    $session->setCustomerAsLoggedIn($customer);
                    $url = $this->_welcomeCustomer($customer);
                    $this->_redirectSuccess($url);
                    return;
                }
            } else {
                $session->setCustomerFormData($this->getRequest()->getPost());
                if (is_array($errors)) {
                    foreach ($errors as $errorMessage) {
                        $session->addError($errorMessage);
                    }
                } else {
                    $session->addError($this->__('Invalid customer data'));
                }
            }
        } catch (Mage_Core_Exception $e) {
            $session->setCustomerFormData($this->getRequest()->getPost());
            if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                $url = Mage::getUrl('customer/account/forgotpassword');
                $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
                $session->setEscapeMessages(false);
            } else {
                $message = $e->getMessage();
            }
            $session->addError($message);
        } catch (Exception $e) {
            $session->setCustomerFormData($this->getRequest()->getPost())
                ->addException($e, $this->__('Cannot save the customer.'));
        }
    } 


    $this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));



}

【问题讨论】:

  • 请标记为正确答案或添加您自己的答案并标记为正确..

标签: magento integration sugarcrm


【解决方案1】:

您是否尝试过使用

$this->getRequest()->getPost()

我知道对于 magento 的联系表单,它会生成一个看起来像这样的数组 - 可能与创建帐户非常相似。

(
    [name] => test
    [email] => test@test.com
    [telephone] => 
    [comment] => test
    [captcha] => Array
        (
            [contact_form] => tttt
        )

    [hideit] => 
)

【讨论】:

    【解决方案2】:

    这对我有用:

    // Pass customer information from Magento to SugarCRM
    $customer_info = array(array('name' => 'first_name', 'value' => $this->getRequest()->getPost('firstname')),
                       array('name' => 'last_name', 'value' => $this->getRequest()->getPost('lastname')),
                       array('name' => 'email2', 'value' => $this->getRequest()->getPost('email')));
    
    $response = $client->set_entry($session_id, 'Leads', $customer_info);
    

    【讨论】:

      【解决方案3】:

      您还可以使用 magento 事件处理,而不是覆盖控制器文件。

      customer_save_after 是新客户注册后调用的事件。

      http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/events

      <events>
          <customer_save_after>
              <observers>
                  <yournamespace_yourmodule_order_observer>
                      <type>singleton</type>
                      <class>Yournamespace_Yourmodule_Model_Observer</class>
                      <method>sendemail</method>
                  </yournamespace_yourmodule_order_observer>
              </observers>
          </customer_save_after>
      </events>
      

      这是要包含在模块配置文件中的示例代码到文件中

      现在在你的模型文件夹中创建一个名为 Observer.php 的文件并输入以下代码:

      <?php
      class Yournamespace_Yourmodule_Model_Observer
      {
          public function __construct()
          {
              //Just constrcutor for fun :-)
          }
      
          public function sendemail($observer)
          {
              //$observer contains the object returns in the event.
              $event = $observer->getEvent();
              $order = $event->getOrder();
              mage::log($order->getId());
              //Write code to send email
              return $this;
           }
      

      }

      现在,只要您从 magento 商店的任何地方下订单,您的 sendemail 函数就会被执行。

      【讨论】:

        【解决方案4】:

        您可以覆盖Mage_Customer_AccountController 并更改createPostAction,您可以在其中使用以下代码获取客户字段:

        foreach (Mage::getConfig()->getFieldset('customer_account') as $code=>$node) { if ($node->is('create') && isset($data[$code])) { 开关($代码){ 案例“电子邮件”: $data[$code] = trim($data[$code]); $email = $data[$code]; 休息; 案例“名字”: $firstname = $data[$code]; 休息; 案例“姓氏”: $lastname = $data[$code]; 休息; } } }

        您可以将派生变量传递到您的方法中以进一步处理。

        【讨论】:

        • 感谢您的回复,这是我的职责。将您的代码添加到函数中,但仍然无法获取和传递客户信息
        • 您是否检查过$email、$firstname 和$lastname 是否包含所需的值?