通过PayPal购买虚拟产品的新客户将其计费地址传达给Magento。该地址附在订单上,但没有附加到客户。将其连接到客户并将其设置为其帐户上的默认计费地址的最佳方法是什么?

有帮助吗?

解决方案

您将要利用 predispatch 事件 Mage_Paypal_IpnController 班级:

在您的自定义模块中创建XML,以处理倾向的观察者:

<global>
    <events>
        <controller_action_predispatch_paypal_ipn_index>
            <observers>
                <mymodule_paypal_ipn_index>
                    <type>singleton</type>
                    <class>yourmodel/observer</class>
                    <method>ipnPreDispatch</method>
                </mymodule_paypal_ipn_index>
            </observers>
        </controller_action_predispatch_paypal_ipn_index>
    </events>
</global>

observer.php:

<?php

class YourCompany_YourModule_Model_Observer
{

    public function ipnPreDispatch($observer)
    {
        //get the request object and post data
        $data = $observer->getControllerAction()->getRequest()->getPost();

        //get the email address
        $email = $data['email_address'];

        //get the customer via the email
        $customer = Mage::getModel("customer/customer")->loadByEmail($email);

        //set the customer default address
        $default_address = array (
            'firstname' => $data['first'],
            'lastname' => $data['last'],
            'street' => array (
                '0' => $data['street1'],
                '1' => $data['street2'],
            ),
            'city' => $data['city'],
            'region_id' => $data['state'],
            'postcode' => $data['zipcode'],
            'country_id' => $data['country'],
            'telephone' => $data['telephone'],
        );
        $address = Mage::getModel('customer/address');

        $address->setData($default_address)
                    ->setCustomerId($customer->getId())
                    ->setIsDefaultBilling('1')
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');
        try {
            $address->save();
        } catch(Exception $e) {
            Mage::logExeption($e);
        }

    }

}

我是 不确定IPN的格式, ,所以 $data 在上面的观察者中 可能需要更改 调整。另外,我 不确定业务规则, ,所以目前它设定 所有IPN客户地址 默认为他们最近的订单。这是 不准备生产, ,但可以让您达到90%的途径。

许可以下: CC-BY-SA归因
scroll top