在Magento中更新客户密码

时间:2014-11-26 06:20:04

标签: android magento

我正在尝试更新客户密码但未更新,其他客户相关信息正在更新,如用户名和电子邮件。更新密码后,我在响应中变为真实。下面是代码。

env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
SoapObject customerEntity = new SoapObject(NAMESPACE, "customerCustomerEntityToCreate");
customerEntity.addProperty("email",email);
customerEntity.addProperty("customer_id",customerId);
customerEntity.addProperty("firstname",firstName);
customerEntity.addProperty("lastname",lastName);
customerEntity.addProperty("password",password);
request = new SoapObject(NAMESPACE, "customerCustomerUpdate");
request.addProperty("sessionId", sessionId);
request.addProperty("customerId", customerId);
request.addProperty("customerData", customerEntity);

env.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
(new MarshalHashtable()).register(env);
try {
    androidHttpTransport.call("", env);
} catch (IOException | XmlPullParserException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    result = env.getResponse();
    System.out.println(result);
} catch (SoapFault e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}                                                                                     

1 个答案:

答案 0 :(得分:1)

我已经尝试了这个,然后我研究了这个,据我所知,它无法从API为客户设置密码。因为在magento中他们在客户内部使用过滤器创建方法,如$data = $validator->filter($data);。所以这里我们发送的密码被过滤掉了。为避免这种情况,我在过滤前获取密码,并在过滤器之后设置密码,如下所示。我正在使用REST Api我认为你在SOAP Api中遇到同样的问题,只需像这样改变magento中的SOAP Api customer create method/customer update method,它可以解决你的问题。

protected function _create(array $data)
    {
        $password = $data['password'];
        $validator = Mage::getResourceModel('api2/validator_eav', array('resource' => $this));
        $data = $validator->filter($data);
        $data['password'] = $password;
        if (!$validator->isValidData($data))
        {
            foreach ($validator->getErrors() as $error)
            {
                $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
            }
            $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
        }

        $customer = Mage::getModel('customer/customer');
        $customer->setData($data);

        try
        {
            $customer->setPassword($data['password']);      //added
            $customer->setWebsiteId(1)->save(); 

        }
        catch (Mage_Core_Exception $e)
        {
            $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
        }
        catch (Exception $e)
        {
            $this->_critical(self::RESOURCE_INTERNAL_ERROR);
        }
    }