0

I would like to know how to cut an ajax response specifically on the one page checkout saveBilling process (ajax) and redirect to the login page.

I am currently using this code

$this->_redirect('customer/account/login');

but it only produces a ajax GET response.

public function checkGuestIfRegistered($email){

    $customer = Mage::getModel('customer/customer')->getCollection()
                ->addAttributeToSelect(array('id', 'email'))
                ->addFieldToFilter("email", array("eq" => $email));
    if($customer->count() >= 1){

        $result['error'] = $this->__('Please log in');
        $result['redirect'] = Mage::getUrl('customer/account/login');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

    }
}

saveBilling

public function saveBillingAction(){
    if ($this->_expireAjax()) {
        return;
    }
    //print_r($this->getRequest()->getPost('billing')['email']);
    $this->checkGuestIfRegistered($this->getRequest()->getPost('billing')['email']);

    if ($this->getRequest()->isPost()) {
        $data = $this->getRequest()->getPost('billing', array());
        $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

        if (isset($data['email'])) {
            $data['email'] = trim($data['email']);
        }
        $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

        if (!isset($result['error'])) {
            if ($this->getOnepage()->getQuote()->isVirtual()) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );
            } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                $result['goto_section'] = 'shipping_method';
                $result['update_section'] = array(
                    'name' => 'shipping-method',
                    'html' => $this->_getShippingMethodsHtml()
                );

                $result['allow_sections'] = array('shipping');
                $result['duplicateBillingInfo'] = 'true';
            } else {
                $result['goto_section'] = 'shipping';
            }
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }
}

1 Answer 1

1

Since your ajax response need to be process on the client side you can not use server side redirect.

Try

$result['error'] = -1;
$result['message'] = $this->__('Please log in');
$result['redirect'] = Mage::getUrl('customer/account/login');
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

Update

public function checkGuestIfRegistered($email){

    $customer = Mage::getModel('customer/customer')->getCollection()
                ->addAttributeToSelect(array('id', 'email'))
                ->addFieldToFilter("email", array("eq" => $email));
    if($customer->count() >= 1){

        $result['error'] = -1;
        $result['message'] = $this->__('Please log in');
        $result['redirect'] = Mage::getUrl('customer/account/login');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        return true;
    }

    return false;
}

Then

if($this->checkGuestIfRegistered($this->getRequest()->getPost('billing')['email'])){
    return;
 }

You may want to refactor the code and move the setBody out of checkGuestIfRegistered and return an empty array or $result

16
  • this just pushes thru and processes additional ajax requests. it doesn't redirect me here is my complete code. I've already check it it passes the condition
    – tom_cruz
    Aug 19, 2015 at 14:19
  • Where is checkGuestIfRegistered($email) called from. Also can you check the response content of the ajax request Aug 19, 2015 at 14:46
  • On the saveBilling action OnepageController
    – tom_cruz
    Aug 19, 2015 at 14:55
  • Can you add that function.. try adding die() and then check the response Aug 19, 2015 at 14:57
  • I will add it above
    – tom_cruz
    Aug 19, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.