覆盖自定义模块中的客户地址模型

时间:2014-05-02 09:22:09

标签: php magento

我想在abstract.php中添加一个自定义函数,该函数位于app/code/core/Mage/Customer/Model/Address

public function getCorrectAddress(){
    $post_data['strret'] = '18334 W. Purdue Ave.';
    $post_data['city'] = 'Waddell';
    $post_data['state'] = 'AZ';
    $post_data['zip'] = '85355';

    //traverse array and prepare data for posting (key1=value1)
    foreach ( $post_data as $key => $value) {
        $post_items[] = $key . '=' . $value;
    }

    //create the final string to be posted using implode()
    $post_string = implode ('&', $post_items);

    echo "post string=$post_string";
    echo"<br>";
    //create cURL connection
    $curl_connection = curl_init("http://www.example.com/.urlencode(json_encode($post_string))");

    //set options
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type:text/html;charset=utf-8',
      'Content-Length: '.strlen($post_string)
    ));
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

    //set data to be posted
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

    //perform our request
    $result = curl_exec($curl_connection);

    //show information regarding the request
    print_r(curl_getinfo($curl_connection));
    echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);

    //close the connection
    curl_close($curl_connection);
    return $result;
}

并且该结果显示在以下textfeild中的edit.phtml页面中。

<table>
    <tr>
        <td width="30%" valign="top">Street</td>
        <td width="2%">:</td>
        <td width="68%"><input name="Street" type="text" id="correctStreet" size="50" </td>
    </tr>           
    <tr>
        <td valign="top">City</td>
        <td valign="top">:</td>
        <td><input name="City" type="text" id="correctCity" size="50"></td>
    </tr>
    <tr>
        <td valign="top">State</td>
        <td>:</td>
        <td><input name="State" type="text" id="correctState" size="44"></td>
    </tr>
    <tr>
        <td valign="top">Zip</td>
        <td>:</td>
        <td><input name="Zip" cols="32" rows="4" id="correctZip"></textarea></td>
    </tr>
</table>
<input type="text" value="xyz" id="newcity">
<input type="submit" name="submit" value="This will real submit the form" />

但我对如何做到这一点很困惑? <{1}}函数应在重新加载getCorrectAddress()时从表单中获取地址。

1 个答案:

答案 0 :(得分:0)

这个问题已经回答here

  

实现此目的的最佳方法是覆盖核心模型。   为此,您可以在模块的config.xml中添加几个节点:

<?xml version="1.0"?>
<config>
    ...
    <global>
        <models>
            <customer>
                <address>Your_Module_Model_Customer_Address</address>
            </customer>
        </models>
    </global>
    ...
</config>

然后你可以在你的Magento结构中创建一个类:

应用程序/代码/本地/你/模块/型号/客户/ Address.php

<?php

class Your_Module_Model_Customer_Address extends Mage_Customer_Model_Address
{

    public function getCorrectAddress()
    {
        /**
         * Your custom code goes here...
         */
    }

}

谢谢

Tiago Sampaio