Magento – proceed to checkout button is only show for login users

checkoutmagento-1.8payment

I want to make a function in which only login user can see the proceed to checkout button at checkout page if non registered user will come at checkout page then they will see checkout button disabled

Best Answer

First of all I want to say that this may be bad for business.
It's not a good idea to disable the button.
If you don't want guest users to be able to order just disable the guest checkout from System->Configuration->Checkout->Checkout Options->Allow Guest Checkout.
Then, the users will see the checkout page but they will be forced to login or create an account to perform the checkout process.

But if you insist here is how you can do it.
You can override the method: Mage_Checkout_Block_Onepage_Link::isDisabled.
Add this in the config.xml of one of your modules under the <global> tag.

<blocks>
    <checkout>
        <rewrite>
            <onepage_link>[Namespace]_[Module]_Block_Onepage_Link</onepage_link>
        </rewrite>
    </checkout>
</blocks>

Then create the file [Namespace]/[Module]/Block/Onepage/Link.php with this content

<?php 
class [Namespace]_[Module]_Block_Onepage_Link extends Mage_Checkout_Block_Onepage_Link {
    public function isDisabled() {
        if (parent::isDisabled()) {
            return true;
        }
        return !Mage::getSingleton('customer/session')->isLoggedIn();
    }
}

Now you have to make sure that only logged in customer can access the checkout page. Because if you only disable the button they will be able to use the direct link.

observe the event controller_action_predispatch_checkout_onepage_index. You can do that by adding this in the same config.xml as above inside the <frontend> tag:

<events>
   <controller_action_predispatch_checkout_onepage_index>
       <observers>
           <[namespace]_[module]>
               <class>[Namespace]_[Module]_Model_Observer</class>
               <method>checkCustomerLogin</method>
           </[namespace]_[module]>
       </observers>
   </controller_action_predispatch_checkout_onepage_index>
</events>

And create the following file [Namespace]/[Module]/Model/Observer.php:

<?php 
class [Namespace]_[Module]_Model_Observer {
    public function checkCustomerLogin() {
        if(! Mage::helper('customer')->isLoggedIn()){
            Mage::getSingleton('checkout/session')->addError(Mage::helper('checkout')->__('Only logged in customers can checkout'));
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart/'));
        }
    }
}
Related Topic