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
2 Answers
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/'));
}
}
}
You will need to edit the checkout/onepage/link.phtml
template. If it's not already in your theme copy it from the base/default
theme.
Now on line 27
through 29
you will find the following piece of code
<?php if ($this->isPossibleOnepageCheckout()):?>
<button type="button" title="<?php echo $this->__('Proceed to Checkout') ?>" class="button btn-proceed-checkout btn-checkout<?php if ($this->isDisabled()):?> no-checkout<?php endif; ?>"<?php if ($this->isDisabled()):?> disabled="disabled"<?php endif; ?> onclick="window.location='<?php echo $this->getCheckoutUrl() ?>';"><span><span><?php echo $this->__('Proceed to Checkout') ?></span></span></button>
<?php endif?>
To the first line add the following
<?php if ($this->isPossibleOnepageCheckout() && Mage::getSingleton('customer/session')->isLoggedIn()):?>
That should do the trick