Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tlan16/78765ce63ddb821686ca to your computer and use it in GitHub Desktop.
Save tlan16/78765ce63ddb821686ca to your computer and use it in GitHub Desktop.
Bind Validation to “Process to checkout” button in Magento

As per request by a customer, they need to add a customised validation in the checkout process. The customer cannot proceed to checkout in some circumstances:

The grand total of the shopping cart exceed a certain amount There is at least one product in the shopping cart belongs to a certain category If all of the conditions above is met, then the customer cannot checkout. In this case, I just disabled the “process to checkout” button.

The file been overloaded is

app\design\frontend\base\default\template\checkout\onepage\link.phtml

Original magento code is

<?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?>

I put some overhead to grab the grand total and the category (categories) for each product in the shopping cart. Later on, I put my flag in as part of the magento’s if statement.

<?php if ($this->isPossibleOnepageCheckout()):?>
	<?php
		$grandTotalLimit = floatval(200);
		$freeGiftCategoryId = 4;
		$canCheckout = true;
		
		$cart = Mage::getModel('checkout/cart')->getQuote();
		$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
// 		echo 'grandtotal = ' . $grandTotal;
		foreach ($cart->getAllItems() as $item) 
		{
			$productName = $item->getProduct()->getName();
			$productPrice = $item->getProduct()->getPrice();
			$productCategoryIds = $item->getProduct()->getCategoryIds();
			foreach ($productCategoryIds as $productCategoryId)
			{
				if($canCheckout === true && (doubleval($grandTotal) < doubleval($grandTotalLimit)) && (intval($productCategoryId) === intval($freeGiftCategoryId)))
					$canCheckout = false;
			}
		}
	?>
    <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() || $canCheckout !== true):?> disabled="disabled"<?php endif; ?> onclick="window.location='<?php echo $this->getCheckoutUrl() ?>';"><span><span><?php echo $this->__('Proceed to Checkout') ?></span></span></button>
<?php endif?>

The result is successful. This could be a bad practice when modifying magento, as well as the customer does not get any reasoning hence may be confused by the disabled “process to checkout” button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment