Recently I asked a question on Stackoverflow about a weird issue when using Paypal Express in Magento. If the customer is registered during the payment process, when the order is completed, the customer is not assigned to any group and no taxes are applied to the order.

I finally found the solution myself and like to post the fix in here. It is necessary to rewrite the class app/code/core/Mage/Paypal/Model/Express/Checkout.php and modify the method _prepareNewCustomerQuote() so that it assigns the customer to the correct group.

  • Class rewrite

Create a new module in local pool. The config.xml should look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <YourModule_Paypal>
            <version>1.0.0</version>
        </YourModule_Paypal>
    </modules>
    <global>
        <models>
            <paypal>
                <rewrite>
                    <express_checkout>YourModule_Paypal_Model_Express_Checkout</express_checkout>
                </rewrite>
            </paypal>
        </models>
    </global>
</config>
  • Method modification

Here you have the whole code of how the class should be now. Please verify there are no new lines on the original class. This might differ depending on the Magento version (this comes from 1.7.0.2). Note that it’s only necessary to force the group assignation:

class YourModule_Paypal_Model_Express_Checkout extends Mage_Paypal_Model_Express_Checkout
{
    /**
     * REWRITE: We force the new customer assignation to the general group (1)
     * Prepare quote for customer registration and customer order submit
     * and restore magento customer data from quote
     *
     * @return Mage_Paypal_Model_Express_Checkout
     */
    protected function _prepareNewCustomerQuote()
    {
        $quote      = $this->_quote;
        $billing    = $quote->getBillingAddress();
        $shipping   = $quote->isVirtual() ? null : $quote->getShippingAddress();

        $customer = $quote->getCustomer();
        /** @var $customer Mage_Customer_Model_Customer */
        $customerBilling = $billing->exportCustomerAddress();
        $customer->addAddress($customerBilling);
        $billing->setCustomerAddress($customerBilling);
        $customerBilling->setIsDefaultBilling(true);
        if ($shipping && !$shipping->getSameAsBilling()) {
            $customerShipping = $shipping->exportCustomerAddress();
            $customer->addAddress($customerShipping);
            $shipping->setCustomerAddress($customerShipping);
            $customerShipping->setIsDefaultShipping(true);
        } elseif ($shipping) {
            $customerBilling->setIsDefaultShipping(true);
        }
        /**
         * @todo integration with dynamica attributes customer_dob, customer_taxvat, customer_gender
         */
        if ($quote->getCustomerDob() && !$billing->getCustomerDob()) {
            $billing->setCustomerDob($quote->getCustomerDob());
        }

        if ($quote->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) {
            $billing->setCustomerTaxvat($quote->getCustomerTaxvat());
        }

        if ($quote->getCustomerGender() && !$billing->getCustomerGender()) {
            $billing->setCustomerGender($quote->getCustomerGender());
        }

        Mage::helper('core')->copyFieldset('checkout_onepage_billing', 'to_customer', $billing, $customer);
        $customer->setEmail($quote->getCustomerEmail());
        $customer->setPrefix($quote->getCustomerPrefix());
        $customer->setFirstname($quote->getCustomerFirstname());
        $customer->setMiddlename($quote->getCustomerMiddlename());
        $customer->setLastname($quote->getCustomerLastname());
        $customer->setSuffix($quote->getCustomerSuffix());
        $customer->setGroupId(Mage::getStoreConfig('customer/create_account/default_group')); //Force customer assignation to default group
        $customer->setPassword($customer->decryptPassword($quote->getPasswordHash()));
        $customer->setPasswordHash($customer->hashPassword($customer->getPassword()));
        $customer->save();
        $quote->setCustomer($customer);

        return $this;
    }
}

 


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.